summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMatthias Beyer <mail@beyermatthias.de>2018-03-23 19:26:54 +0100
committerGitHub <noreply@github.com>2018-03-23 19:26:54 +0100
commit96b6beff5d721a65385f9e9242fe083043f5965c (patch)
tree4dc4fc787d34a693fac4faf3eb2b39281bb59314
parented41922c2e44061c1e2c045257daf6d554b96059 (diff)
parentf627d22057b4550a96329a4045730f122d6f36cb (diff)
downloadimag-96b6beff5d721a65385f9e9242fe083043f5965c.zip
imag-96b6beff5d721a65385f9e9242fe083043f5965c.tar.gz
Merge pull request #1365 from matthiasbeyer/libimagentryedit/edit-header
imag-edit / libimagentryedit: edit header support
-rw-r--r--bin/core/imag-edit/src/main.rs24
-rw-r--r--bin/core/imag-edit/src/ui.rs6
-rw-r--r--lib/entry/libimagentryedit/Cargo.toml1
-rw-r--r--lib/entry/libimagentryedit/src/edit.rs23
-rw-r--r--lib/entry/libimagentryedit/src/error.rs10
-rw-r--r--lib/entry/libimagentryedit/src/lib.rs1
6 files changed, 57 insertions, 8 deletions
diff --git a/bin/core/imag-edit/src/main.rs b/bin/core/imag-edit/src/main.rs
index da84abb..4b6387d 100644
--- a/bin/core/imag-edit/src/main.rs
+++ b/bin/core/imag-edit/src/main.rs
@@ -47,6 +47,7 @@ use std::io::Read;
use libimagerror::trace::MapErrTrace;
use libimagerror::iter::TraceIterator;
use libimagentryedit::edit::Edit;
+use libimagentryedit::edit::EditHeader;
use libimagrt::setup::generate_runtime_setup;
use libimagstore::storeid::IntoStoreId;
use libimagstore::storeid::StoreIdIterator;
@@ -85,11 +86,8 @@ fn main() {
}
};
- if rt.cli().is_present("edit-header") {
- // TODO: support editing of header
- warn!("Editing header is not yet supported by imag-edit");
- ::std::process::exit(1);
- }
+ let edit_header = rt.cli().is_present("edit-header");
+ let edit_header_only = rt.cli().is_present("edit-header-only");
StoreIdIterator::new(Box::new(sids.into_iter()))
.into_get_iter(rt.store())
@@ -99,9 +97,19 @@ fn main() {
::std::process::exit(1)
}))
.for_each(|mut entry| {
- let _ = entry
- .edit_content(&rt)
- .map_err_trace_exit_unwrap(1);
+ if edit_header {
+ let _ = entry
+ .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);
+ }
});
}
diff --git a/bin/core/imag-edit/src/ui.rs b/bin/core/imag-edit/src/ui.rs
index dbf0b85..2f3e8f6 100644
--- a/bin/core/imag-edit/src/ui.rs
+++ b/bin/core/imag-edit/src/ui.rs
@@ -47,5 +47,11 @@ pub fn build_ui<'a>(app: App<'a, 'a>) -> App<'a, 'a> {
.multiple(false)
.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"))
}
diff --git a/lib/entry/libimagentryedit/Cargo.toml b/lib/entry/libimagentryedit/Cargo.toml
index 57690db..865d626 100644
--- a/lib/entry/libimagentryedit/Cargo.toml
+++ b/lib/entry/libimagentryedit/Cargo.toml
@@ -21,6 +21,7 @@ maintenance = { status = "actively-developed" }
[dependencies]
error-chain = "0.11"
+toml = "0.4"
libimagerror = { version = "0.7.0", path = "../../../lib/core/libimagerror" }
libimagrt = { version = "0.7.0", path = "../../../lib/core/libimagrt" }
diff --git a/lib/entry/libimagentryedit/src/edit.rs b/lib/entry/libimagentryedit/src/edit.rs
index e84e3b7..6802eaa 100644
--- a/lib/entry/libimagentryedit/src/edit.rs
+++ b/lib/entry/libimagentryedit/src/edit.rs
@@ -29,6 +29,11 @@ pub trait Edit {
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 {
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<()> {
use libimagutil::edit::edit_in_tmpfile_with_command;
diff --git a/lib/entry/libimagentryedit/src/error.rs b/lib/entry/libimagentryedit/src/error.rs
index 094744b..039dd0c 100644
--- a/lib/entry/libimagentryedit/src/error.rs
+++ b/lib/entry/libimagentryedit/src/error.rs
@@ -22,6 +22,16 @@ error_chain! {
EditError, EditErrorKind, ResultExt, Result;
}
+ links {
+ StoreError(::libimagstore::error::StoreError, ::libimagstore::error::StoreErrorKind);
+ }
+
+ foreign_links {
+ TomlSerError(::toml::ser::Error);
+ TomlDeserError(::toml::de::Error);
+ }
+
+
errors {
IOError {
description("IO Error")
diff --git a/lib/entry/libimagentryedit/src/lib.rs b/lib/entry/libimagentryedit/src/lib.rs
index bcc2491..29a0907 100644
--- a/lib/entry/libimagentryedit/src/lib.rs
+++ b/lib/entry/libimagentryedit/src/lib.rs
@@ -40,6 +40,7 @@ extern crate libimagstore;
extern crate libimagrt;
extern crate libimagutil;
#[macro_use] extern crate error_chain;
+extern crate toml;
pub mod edit;
pub mod error;