From cec4d93f3eccf1c1f37c9073bd596c603f11999f Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Sun, 10 Nov 2019 22:39:08 +0100 Subject: Add imag-tag-present command for filtering entries Signed-off-by: Matthias Beyer --- bin/core/imag-tag/src/lib.rs | 41 +++++++++++++++++++++++++++++++++++++++++ bin/core/imag-tag/src/ui.rs | 10 ++++++++++ 2 files changed, 51 insertions(+) diff --git a/bin/core/imag-tag/src/lib.rs b/bin/core/imag-tag/src/lib.rs index 3c8767c9..d1a7d447 100644 --- a/bin/core/imag-tag/src/lib.rs +++ b/bin/core/imag-tag/src/lib.rs @@ -66,10 +66,12 @@ use failure::Error; use failure::err_msg; use resiter::AndThen; use resiter::Map; +use resiter::FilterMap; use libimagrt::runtime::Runtime; use libimagrt::application::ImagApplication; use libimagentrytag::tagable::Tagable; +use libimagentrytag::tag::is_tag_str; use libimagentrytag::tag::Tag; use libimagstore::storeid::StoreId; @@ -106,6 +108,45 @@ impl ImagApplication for ImagTag { alter(&rt, id, add, rem) }).collect(), + ("present", Some(scmd)) => { + let must_be_present = scmd + .values_of("present-tag") + .unwrap() + .map(String::from) + .collect::>(); + + must_be_present.iter().map(|t| is_tag_str(t)).collect::>>()?; + + iter.filter_map_ok(|id| { + match rt.store().get(id.clone()) { + Err(e) => Some(Err(e)), + Ok(None) => Some(Err(format_err!("No entry for id {}", id))), + Ok(Some(entry)) => { + let entry_tags = match entry.get_tags() { + Err(e) => return Some(Err(e)), + Ok(e) => e, + }; + + if must_be_present.iter().all(|pres| entry_tags.contains(pres)) { + Some(Ok(entry)) + } else { + None + } + } + } + }) + .flatten() + .and_then_ok(|e| { + if !rt.output_is_pipe() { + writeln!(rt.stdout(), "{}", e.get_location())?; + } + Ok(e) + }) + .and_then_ok(|e| rt.report_touched(e.get_location()).map_err(Error::from)) + .collect::>>() + .map(|_| ()) + }, + (other, _) => { debug!("Unknown command"); if rt.handle_unknown_subcommand("imag-tag", other, rt.cli())?.success() { diff --git a/bin/core/imag-tag/src/ui.rs b/bin/core/imag-tag/src/ui.rs index ba3a7859..54d4e7aa 100644 --- a/bin/core/imag-tag/src/ui.rs +++ b/bin/core/imag-tag/src/ui.rs @@ -101,6 +101,16 @@ pub fn build_ui<'a>(app: App<'a, 'a>) -> App<'a, 'a> { .required(true)) ) + .subcommand(SubCommand::with_name("present") + .about("List entries that have a certain tag") + .version("0.1") + .arg(Arg::with_name("present-tag") + .index(1) + .required(true) + .multiple(true) + .help("Tag to list entries for")) + ) + } pub struct PathProvider; -- cgit v1.2.3