From a805db2a88beffc77ee43a3aad7ce67848db2e8c Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Sat, 29 Sep 2018 17:44:36 +0200 Subject: [PATCH] Move imag-edit to ID provider infrastructure --- bin/core/imag-edit/src/main.rs | 30 ++-------------------------- bin/core/imag-edit/src/ui.rs | 36 +++++++++++++++++++++++----------- 2 files changed, 27 insertions(+), 39 deletions(-) diff --git a/bin/core/imag-edit/src/main.rs b/bin/core/imag-edit/src/main.rs index 46c4c3f9..6d4ac384 100644 --- a/bin/core/imag-edit/src/main.rs +++ b/bin/core/imag-edit/src/main.rs @@ -41,15 +41,11 @@ extern crate libimagerror; extern crate libimagstore; extern crate libimagutil; -use std::path::PathBuf; -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; use libimagstore::iter::get::StoreIdGetIteratorExtension; @@ -62,33 +58,11 @@ fn main() { "Edit store entries with $EDITOR", ui::build_ui); - 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 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) - }); - - 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) - } - }; - let edit_header = rt.cli().is_present("edit-header"); let edit_header_only = rt.cli().is_present("edit-header-only"); + let sids = rt.ids::<::ui::PathProvider>().map_err_trace_exit_unwrap(1); + StoreIdIterator::new(Box::new(sids.into_iter().map(Ok))) .into_get_iter(rt.store()) .trace_unwrap_exit(1) diff --git a/bin/core/imag-edit/src/ui.rs b/bin/core/imag-edit/src/ui.rs index 2f3e8f66..fbb1d5e3 100644 --- a/bin/core/imag-edit/src/ui.rs +++ b/bin/core/imag-edit/src/ui.rs @@ -17,7 +17,14 @@ // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA // -use clap::{Arg, ArgGroup, App}; +use std::path::PathBuf; + +use clap::{Arg, ArgMatches, App}; + +use libimagstore::storeid::IntoStoreId; +use libimagstore::storeid::StoreId; +use libimagrt::runtime::IdPathProvider; +use libimagerror::trace::MapErrTrace; pub fn build_ui<'a>(app: App<'a, 'a>) -> App<'a, 'a> { app @@ -28,16 +35,6 @@ pub fn build_ui<'a>(app: App<'a, 'a>) -> App<'a, 'a> { .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") @@ -55,3 +52,20 @@ pub fn build_ui<'a>(app: App<'a, 'a>) -> App<'a, 'a> { .help("Only edit the header")) } +pub struct PathProvider; +impl IdPathProvider for PathProvider { + fn get_ids(matches: &ArgMatches) -> Vec { + matches + .values_of("entry") + .ok_or_else(|| { + error!("No StoreId found"); + ::std::process::exit(1) + }) + .unwrap() + .into_iter() + .map(PathBuf::from) + .map(|pb| pb.into_storeid()) + .collect::, _>>() + .map_err_trace_exit_unwrap(1) + } +}