From c5f0dc7b14f020a818eb0575403141399c62d2aa Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Sun, 10 Nov 2019 22:39:08 +0100 Subject: [PATCH] Add imag-tag-missing command for filtering entries Signed-off-by: Matthias Beyer --- bin/core/imag-tag/src/lib.rs | 39 ++++++++++++++++++++++++++++++++++++ bin/core/imag-tag/src/ui.rs | 10 +++++++++ 2 files changed, 49 insertions(+) diff --git a/bin/core/imag-tag/src/lib.rs b/bin/core/imag-tag/src/lib.rs index d1a7d447..874ebf5a 100644 --- a/bin/core/imag-tag/src/lib.rs +++ b/bin/core/imag-tag/src/lib.rs @@ -147,6 +147,45 @@ impl ImagApplication for ImagTag { .map(|_| ()) }, + ("missing", Some(scmd)) => { + let must_be_missing = scmd + .values_of("missing-tag") + .unwrap() + .map(String::from) + .collect::>(); + + must_be_missing.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_missing.iter().all(|miss| !entry_tags.contains(miss)) { + 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 54d4e7aa..a4a00f21 100644 --- a/bin/core/imag-tag/src/ui.rs +++ b/bin/core/imag-tag/src/ui.rs @@ -111,6 +111,16 @@ pub fn build_ui<'a>(app: App<'a, 'a>) -> App<'a, 'a> { .help("Tag to list entries for")) ) + .subcommand(SubCommand::with_name("missing") + .about("List entries miss a certain tag") + .version("0.1") + .arg(Arg::with_name("missing-tag") + .index(1) + .required(true) + .multiple(true) + .help("Tag which should be missing in the entries")) + ) + } pub struct PathProvider;