Move edit_entry_with_cmd() to libimagutil

This commit is contained in:
Matthias Beyer 2016-09-08 17:44:21 +02:00
parent 501d1f38a2
commit 027fffb5b5
7 changed files with 59 additions and 41 deletions

View file

@ -4,7 +4,6 @@ version = "0.2.0"
authors = ["Matthias Beyer <mail@beyermatthias.de>"]
[dependencies]
tempfile = "2.1.1"
[dependencies.libimagerror]
path = "../libimagerror"
@ -15,3 +14,6 @@ path = "../libimagrt"
[dependencies.libimagstore]
path = "../libimagstore"
[dependencies.libimagutil]
path = "../libimagutil"

View file

@ -39,44 +39,20 @@ impl<'a> Edit for FileLockEntry<'a> {
}
pub fn edit_in_tmpfile(rt: &Runtime, s: &mut String) -> Result<()> {
edit_in_tmpfile_with_command(rt.editor(), s)
}
use libimagutil::edit::edit_in_tmpfile_with_command;
pub fn edit_in_tmpfile_with_command(cmd: Command, s: &mut String) -> Result<()> {
use tempfile::NamedTempFile;
use std::io::Seek;
use std::io::Read;
use std::io::SeekFrom;
use std::io::Write;
let file = try!(NamedTempFile::new().map_err_into(EditErrorKind::IOError));
let file_path = file.path();
let mut file = try!(file.reopen().map_err_into(EditErrorKind::IOError));
try!(file.write_all(&s.clone().into_bytes()[..]).map_err_into(EditErrorKind::IOError));
try!(file.sync_data().map_err_into(EditErrorKind::IOError));
if let Some(mut editor) = rt.editor() {
let exit_status = editor.arg(file_path).status();
match exit_status.map(|s| s.success()).map_err(Box::new) {
Ok(true) => {
file.sync_data()
.and_then(|_| file.seek(SeekFrom::Start(0)))
.and_then(|_| {
let mut new_s = String::new();
let res = file.read_to_string(&mut new_s);
*s = new_s;
res
})
.map(|_| ())
rt.editor()
.ok_or(EditErrorKind::NoEditor.into_error())
.and_then(|editor| {
edit_in_tmpfile_with_command(editor, s)
.map_err_into(EditErrorKind::IOError)
},
Ok(false) => Err(EditErrorKind::ProcessExitFailure.into()),
Err(e) => Err(EditErrorKind::IOError.into_error_with_cause(e)),
}
.and_then(|worked| {
if !worked {
Err(EditErrorKind::ProcessExitFailure.into())
} else {
Err(EditErrorKind::InstantiateError.into())
Ok(())
}
})
})
}

View file

@ -1,6 +1,7 @@
generate_error_module!(
generate_error_types!(EditError, EditErrorKind,
IOError => "IO Error",
NoEditor => "No editor set",
ProcessExitFailure => "Process did not exit properly",
InstantiateError => "Instantation error"
);

View file

@ -1,7 +1,7 @@
#[macro_use] extern crate libimagerror;
extern crate libimagstore;
extern crate libimagrt;
extern crate tempfile;
extern crate libimagutil;
pub mod edit;
pub mod error;

View file

@ -7,4 +7,5 @@ authors = ["Matthias Beyer <mail@beyermatthias.de>"]
lazy_static = "0.1.15"
log = "0.3"
regex = "0.1"
tempfile = "2.1.1"

36
libimagutil/src/edit.rs Normal file
View file

@ -0,0 +1,36 @@
use std::io::Read;
use std::io::Seek;
use std::io::SeekFrom;
use std::io::Write;
use std::process::Command;
use std::io::Error as IOError;
use tempfile::NamedTempFile;
pub fn edit_in_tmpfile_with_command(mut cmd: Command, s: &mut String) -> Result<bool, IOError> {
let file = try!(NamedTempFile::new());
let file_path = file.path();
let mut file = try!(file.reopen());
try!(file.write_all(&s.clone().into_bytes()[..]));
try!(file.sync_data());
cmd.arg(file_path)
.status()
.and_then(|status| {
if status.success() {
file.sync_data()
.and_then(|_| file.seek(SeekFrom::Start(0)))
.and_then(|_| {
let mut new_s = String::new();
let res = file.read_to_string(&mut new_s);
*s = new_s;
res
})
.map(|_| true)
} else {
Ok(false)
}
})
}

View file

@ -16,9 +16,11 @@
#[macro_use] extern crate lazy_static;
#[macro_use] extern crate log;
extern crate regex;
extern crate tempfile;
#[macro_use] mod log_result;
pub mod debug_result;
pub mod edit;
pub mod info_result;
pub mod ismatch;
pub mod iter;