[CHERRY-PICK] Move edit_entry_with_cmd() to libimagutil
This commit is contained in:
parent
8f26b20830
commit
05d38bdf49
7 changed files with 59 additions and 41 deletions
|
@ -4,7 +4,6 @@ version = "0.2.0"
|
||||||
authors = ["Matthias Beyer <mail@beyermatthias.de>"]
|
authors = ["Matthias Beyer <mail@beyermatthias.de>"]
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
tempfile = "2.1.1"
|
|
||||||
|
|
||||||
[dependencies.libimagerror]
|
[dependencies.libimagerror]
|
||||||
path = "../libimagerror"
|
path = "../libimagerror"
|
||||||
|
@ -15,3 +14,6 @@ path = "../libimagrt"
|
||||||
[dependencies.libimagstore]
|
[dependencies.libimagstore]
|
||||||
path = "../libimagstore"
|
path = "../libimagstore"
|
||||||
|
|
||||||
|
[dependencies.libimagutil]
|
||||||
|
path = "../libimagutil"
|
||||||
|
|
||||||
|
|
|
@ -39,44 +39,20 @@ impl<'a> Edit for FileLockEntry<'a> {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn edit_in_tmpfile(rt: &Runtime, s: &mut String) -> Result<()> {
|
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<()> {
|
rt.editor()
|
||||||
use tempfile::NamedTempFile;
|
.ok_or(EditErrorKind::NoEditor.into_error())
|
||||||
use std::io::Seek;
|
.and_then(|editor| {
|
||||||
use std::io::Read;
|
edit_in_tmpfile_with_command(editor, s)
|
||||||
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(|_| ())
|
|
||||||
.map_err_into(EditErrorKind::IOError)
|
.map_err_into(EditErrorKind::IOError)
|
||||||
},
|
.and_then(|worked| {
|
||||||
Ok(false) => Err(EditErrorKind::ProcessExitFailure.into()),
|
if !worked {
|
||||||
Err(e) => Err(EditErrorKind::IOError.into_error_with_cause(e)),
|
Err(EditErrorKind::ProcessExitFailure.into())
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
Err(EditErrorKind::InstantiateError.into())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
})
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
generate_error_module!(
|
generate_error_module!(
|
||||||
generate_error_types!(EditError, EditErrorKind,
|
generate_error_types!(EditError, EditErrorKind,
|
||||||
IOError => "IO Error",
|
IOError => "IO Error",
|
||||||
|
NoEditor => "No editor set",
|
||||||
ProcessExitFailure => "Process did not exit properly",
|
ProcessExitFailure => "Process did not exit properly",
|
||||||
InstantiateError => "Instantation error"
|
InstantiateError => "Instantation error"
|
||||||
);
|
);
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
#[macro_use] extern crate libimagerror;
|
#[macro_use] extern crate libimagerror;
|
||||||
extern crate libimagstore;
|
extern crate libimagstore;
|
||||||
extern crate libimagrt;
|
extern crate libimagrt;
|
||||||
extern crate tempfile;
|
extern crate libimagutil;
|
||||||
|
|
||||||
pub mod edit;
|
pub mod edit;
|
||||||
pub mod error;
|
pub mod error;
|
||||||
|
|
|
@ -7,4 +7,5 @@ authors = ["Matthias Beyer <mail@beyermatthias.de>"]
|
||||||
lazy_static = "0.1.15"
|
lazy_static = "0.1.15"
|
||||||
log = "0.3"
|
log = "0.3"
|
||||||
regex = "0.1"
|
regex = "0.1"
|
||||||
|
tempfile = "2.1.1"
|
||||||
|
|
||||||
|
|
36
libimagutil/src/edit.rs
Normal file
36
libimagutil/src/edit.rs
Normal 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)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
|
@ -16,9 +16,11 @@
|
||||||
#[macro_use] extern crate lazy_static;
|
#[macro_use] extern crate lazy_static;
|
||||||
#[macro_use] extern crate log;
|
#[macro_use] extern crate log;
|
||||||
extern crate regex;
|
extern crate regex;
|
||||||
|
extern crate tempfile;
|
||||||
|
|
||||||
#[macro_use] mod log_result;
|
#[macro_use] mod log_result;
|
||||||
pub mod debug_result;
|
pub mod debug_result;
|
||||||
|
pub mod edit;
|
||||||
pub mod info_result;
|
pub mod info_result;
|
||||||
pub mod ismatch;
|
pub mod ismatch;
|
||||||
pub mod iter;
|
pub mod iter;
|
||||||
|
|
Loading…
Reference in a new issue