From 8ae6e735e7c94ee6f4cae5a3e2c7fa51cbd18fc1 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Mon, 12 Mar 2018 19:47:27 +0100 Subject: [PATCH 1/2] Allow reading store ids from stdin --- bin/core/imag-edit/src/main.rs | 52 ++++++++++++++++++++++++---------- bin/core/imag-edit/src/ui.rs | 15 ++++++++-- 2 files changed, 50 insertions(+), 17 deletions(-) diff --git a/bin/core/imag-edit/src/main.rs b/bin/core/imag-edit/src/main.rs index c69c341a..da84abb2 100644 --- a/bin/core/imag-edit/src/main.rs +++ b/bin/core/imag-edit/src/main.rs @@ -42,12 +42,15 @@ extern crate libimagstore; extern crate libimagutil; use std::path::PathBuf; +use std::io::Read; -use libimagentryedit::edit::*; -use libimagentryedit::error::EditError as EE; use libimagerror::trace::MapErrTrace; +use libimagerror::iter::TraceIterator; +use libimagentryedit::edit::Edit; use libimagrt::setup::generate_runtime_setup; use libimagstore::storeid::IntoStoreId; +use libimagstore::storeid::StoreIdIterator; +use libimagstore::iter::get::StoreIdGetIteratorExtension; mod ui; @@ -58,18 +61,28 @@ fn main() { "Edit store entries with $EDITOR", ui::build_ui); - let mut entry = { - let path = rt.cli() - .value_of("entry") - .unwrap(); // safe by clap + let sids = match rt.cli().value_of("entry") { + Some(path) => vec![PathBuf::from(path).into_storeid().map_err_trace_exit_unwrap(1)], + None => if rt.cli().is_present("entries-from-stdin") { + let stdin = rt.stdin().unwrap_or_else(|| { + error!("Cannot get handle to stdin"); + ::std::process::exit(1) + }); - let sid = PathBuf::from(path).into_storeid().map_err_trace_exit_unwrap(1); + let mut buf = String::new(); + let _ = stdin.lock().read_to_string(&mut buf).unwrap_or_else(|_| { + error!("Failed to read from stdin"); + ::std::process::exit(1) + }); - rt.store() - .get(sid) - .map_err_trace_exit_unwrap(1) - .ok_or(EE::from(format!("Entry {} does not exist", path))) - .map_err_trace_exit_unwrap(1) + buf.lines() + .map(PathBuf::from) + .map(|p| p.into_storeid().map_err_trace_exit_unwrap(1)) + .collect() + } else { + error!("Something weird happened. I was not able to find the path of the entries to edit"); + ::std::process::exit(1) + } }; if rt.cli().is_present("edit-header") { @@ -78,8 +91,17 @@ fn main() { ::std::process::exit(1); } - let _ = entry - .edit_content(&rt) - .map_err_trace_exit_unwrap(1); + StoreIdIterator::new(Box::new(sids.into_iter())) + .into_get_iter(rt.store()) + .trace_unwrap_exit(1) + .map(|o| o.unwrap_or_else(|| { + error!("Did not find one entry"); + ::std::process::exit(1) + })) + .for_each(|mut entry| { + 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 630fecb9..dbf0b85b 100644 --- a/bin/core/imag-edit/src/ui.rs +++ b/bin/core/imag-edit/src/ui.rs @@ -17,17 +17,28 @@ // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA // -use clap::{Arg, App}; +use clap::{Arg, ArgGroup, App}; pub fn build_ui<'a>(app: App<'a, 'a>) -> App<'a, 'a> { app .arg(Arg::with_name("entry") .index(1) .takes_value(true) - .required(true) + .required(false) .multiple(true) .help("The entry/entries to edit") .value_name("ENTRY")) + .arg(Arg::with_name("entries-from-stdin") + .long("ids-from-stdin") + .short("I") + .takes_value(false) + .required(false) + .multiple(false) + .help("The entry/entries are piped in via stdin")) + .group(ArgGroup::with_name("input-method") + .args(&["entry", "entries-from-stdin"]) + .required(true)) + .arg(Arg::with_name("edit-header") .long("header") .short("H") From 03dbb3cf1722ce3fcf66de6b8b4c82e6905643fb Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Mon, 12 Mar 2018 19:47:37 +0100 Subject: [PATCH 2/2] Add changelog entry for imag-edit reading ids from stdin --- doc/src/09020-changelog.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/doc/src/09020-changelog.md b/doc/src/09020-changelog.md index 42577032..3ef4ab64 100644 --- a/doc/src/09020-changelog.md +++ b/doc/src/09020-changelog.md @@ -39,6 +39,8 @@ This section contains the changelog from the last release to the next release. address and either shows or lists the found contacts * `imag-contact list` and `imag-contact find` is now able to print the output as JSON. + * `imag-edit` can now read store ids from stdin, sorts + `imag ids | fzf | imag edit -I` is not a thing. * Minor changes * A license-checker was included into the CI setup, which checks whether all ".rs"-files have the license header at the top of the file