Merge pull request #727 from matthiasbeyer/libimagentryedit/util-to-libimagutil
libimagentryedit/util to libimagutil
This commit is contained in:
commit
a00cbb9e43
7 changed files with 59 additions and 36 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,39 +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<()> {
|
||||||
use tempfile::NamedTempFile;
|
use libimagutil::edit::edit_in_tmpfile_with_command;
|
||||||
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));
|
rt.editor()
|
||||||
let file_path = file.path();
|
.ok_or(EditErrorKind::NoEditor.into_error())
|
||||||
let mut file = try!(file.reopen().map_err_into(EditErrorKind::IOError));
|
.and_then(|editor| {
|
||||||
|
edit_in_tmpfile_with_command(editor, s)
|
||||||
try!(file.write_all(&s.clone().into_bytes()[..]).map_err_into(EditErrorKind::IOError));
|
.map_err_into(EditErrorKind::IOError)
|
||||||
try!(file.sync_data().map_err_into(EditErrorKind::IOError));
|
.and_then(|worked| {
|
||||||
|
if !worked {
|
||||||
if let Some(mut editor) = rt.editor() {
|
Err(EditErrorKind::ProcessExitFailure.into())
|
||||||
let exit_status = editor.arg(file_path).status();
|
} else {
|
||||||
|
Ok(())
|
||||||
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)
|
|
||||||
},
|
|
||||||
Ok(false) => Err(EditErrorKind::ProcessExitFailure.into()),
|
|
||||||
Err(e) => Err(EditErrorKind::IOError.into_error_with_cause(e)),
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
Err(EditErrorKind::InstantiateError.into())
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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