Merge pull request #1365 from matthiasbeyer/libimagentryedit/edit-header

imag-edit / libimagentryedit: edit header support
This commit is contained in:
Matthias Beyer 2018-03-23 19:26:54 +01:00 committed by GitHub
commit 96b6beff5d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 57 additions and 8 deletions

View file

@ -47,6 +47,7 @@ use std::io::Read;
use libimagerror::trace::MapErrTrace; use libimagerror::trace::MapErrTrace;
use libimagerror::iter::TraceIterator; use libimagerror::iter::TraceIterator;
use libimagentryedit::edit::Edit; use libimagentryedit::edit::Edit;
use libimagentryedit::edit::EditHeader;
use libimagrt::setup::generate_runtime_setup; use libimagrt::setup::generate_runtime_setup;
use libimagstore::storeid::IntoStoreId; use libimagstore::storeid::IntoStoreId;
use libimagstore::storeid::StoreIdIterator; use libimagstore::storeid::StoreIdIterator;
@ -85,11 +86,8 @@ fn main() {
} }
}; };
if rt.cli().is_present("edit-header") { let edit_header = rt.cli().is_present("edit-header");
// TODO: support editing of header let edit_header_only = rt.cli().is_present("edit-header-only");
warn!("Editing header is not yet supported by imag-edit");
::std::process::exit(1);
}
StoreIdIterator::new(Box::new(sids.into_iter())) StoreIdIterator::new(Box::new(sids.into_iter()))
.into_get_iter(rt.store()) .into_get_iter(rt.store())
@ -99,9 +97,19 @@ fn main() {
::std::process::exit(1) ::std::process::exit(1)
})) }))
.for_each(|mut entry| { .for_each(|mut entry| {
let _ = entry if edit_header {
.edit_content(&rt) let _ = entry
.map_err_trace_exit_unwrap(1); .edit_header_and_content(&rt)
.map_err_trace_exit_unwrap(1);
} else if edit_header_only {
let _ = entry
.edit_header(&rt)
.map_err_trace_exit_unwrap(1);
} else {
let _ = entry
.edit_content(&rt)
.map_err_trace_exit_unwrap(1);
}
}); });
} }

View file

@ -47,5 +47,11 @@ pub fn build_ui<'a>(app: App<'a, 'a>) -> App<'a, 'a> {
.multiple(false) .multiple(false)
.help("Also edit the header")) .help("Also edit the header"))
.arg(Arg::with_name("edit-header-only")
.long("header-only")
.takes_value(false)
.required(false)
.multiple(false)
.help("Only edit the header"))
} }

View file

@ -21,6 +21,7 @@ maintenance = { status = "actively-developed" }
[dependencies] [dependencies]
error-chain = "0.11" error-chain = "0.11"
toml = "0.4"
libimagerror = { version = "0.7.0", path = "../../../lib/core/libimagerror" } libimagerror = { version = "0.7.0", path = "../../../lib/core/libimagerror" }
libimagrt = { version = "0.7.0", path = "../../../lib/core/libimagrt" } libimagrt = { version = "0.7.0", path = "../../../lib/core/libimagrt" }

View file

@ -29,6 +29,11 @@ pub trait Edit {
fn edit_content(&mut self, rt: &Runtime) -> Result<()>; fn edit_content(&mut self, rt: &Runtime) -> Result<()>;
} }
pub trait EditHeader : Edit {
fn edit_header(&mut self, rt: &Runtime) -> Result<()>;
fn edit_header_and_content(&mut self, rt: &Runtime) -> Result<()>;
}
impl Edit for String { impl Edit for String {
fn edit_content(&mut self, rt: &Runtime) -> Result<()> { fn edit_content(&mut self, rt: &Runtime) -> Result<()> {
@ -46,6 +51,24 @@ impl Edit for Entry {
} }
impl EditHeader for Entry {
fn edit_header(&mut self, rt: &Runtime) -> Result<()> {
let mut header = ::toml::ser::to_string_pretty(self.get_header())?;
let _ = edit_in_tmpfile(rt, &mut header)?;
let header = ::toml::de::from_str(&header)?;
*self.get_header_mut() = header;
Ok(())
}
fn edit_header_and_content(&mut self, rt: &Runtime) -> Result<()> {
let mut header_and_content = self.to_str();
let _ = edit_in_tmpfile(rt, &mut header_and_content)?;
self.replace_from_buffer(&header_and_content).map_err(EE::from)
}
}
pub fn edit_in_tmpfile(rt: &Runtime, s: &mut String) -> Result<()> { pub fn edit_in_tmpfile(rt: &Runtime, s: &mut String) -> Result<()> {
use libimagutil::edit::edit_in_tmpfile_with_command; use libimagutil::edit::edit_in_tmpfile_with_command;

View file

@ -22,6 +22,16 @@ error_chain! {
EditError, EditErrorKind, ResultExt, Result; EditError, EditErrorKind, ResultExt, Result;
} }
links {
StoreError(::libimagstore::error::StoreError, ::libimagstore::error::StoreErrorKind);
}
foreign_links {
TomlSerError(::toml::ser::Error);
TomlDeserError(::toml::de::Error);
}
errors { errors {
IOError { IOError {
description("IO Error") description("IO Error")

View file

@ -40,6 +40,7 @@ extern crate libimagstore;
extern crate libimagrt; extern crate libimagrt;
extern crate libimagutil; extern crate libimagutil;
#[macro_use] extern crate error_chain; #[macro_use] extern crate error_chain;
extern crate toml;
pub mod edit; pub mod edit;
pub mod error; pub mod error;