From f4e1c0864c4fd0f8da1d013f146a56d3ac677dc4 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Sun, 29 Apr 2018 14:37:50 +0200 Subject: [PATCH] Implement 'where' subcommand --- bin/core/imag-ids/src/main.rs | 23 +++++++++++++++++++++++ bin/core/imag-ids/src/ui.rs | 12 +++++++++++- 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/bin/core/imag-ids/src/main.rs b/bin/core/imag-ids/src/main.rs index 2af2b4d3..4ce91578 100644 --- a/bin/core/imag-ids/src/main.rs +++ b/bin/core/imag-ids/src/main.rs @@ -40,6 +40,7 @@ extern crate libimagstore; #[macro_use] extern crate libimagrt; use std::io::Write; +use std::process::exit; use filters::filter::Filter; @@ -70,12 +71,34 @@ fn main() { .map(|v| v.collect::>()); let collection_filter = IsInCollectionsFilter::new(values); + let query_filter : Option = rt + .cli() + .subcommand_matches("where") + .map(|matches| { + let query = matches.value_of("where-filter").unwrap(); // safe by clap + id_filters::header_filter_lang::parse(&query) + }); rt.store() .entries() .map_err_trace_exit_unwrap(1) .trace_unwrap_exit(1) .filter(|id| collection_filter.filter(id)) + .filter(|id| match query_filter.as_ref() { + None => true, + Some(qf) => { + let entry = rt + .store() + .get(id.clone()) + .map_err_trace_exit_unwrap(1) + .unwrap_or_else(|| { + error!("Tried to get '{}', but it does not exist!", id); + exit(1) + }); + + qf.filter(&entry) + } + }) .map(|id| if print_storepath { id } else { diff --git a/bin/core/imag-ids/src/ui.rs b/bin/core/imag-ids/src/ui.rs index 000539dd..c4080c8d 100644 --- a/bin/core/imag-ids/src/ui.rs +++ b/bin/core/imag-ids/src/ui.rs @@ -17,7 +17,7 @@ // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA // -use clap::{Arg, App}; +use clap::{Arg, App, SubCommand}; pub fn build_ui<'a>(app: App<'a, 'a>) -> App<'a, 'a> { app @@ -36,5 +36,15 @@ pub fn build_ui<'a>(app: App<'a, 'a>) -> App<'a, 'a> { .multiple(true) .value_names(&["COLLECTION"]) .help("Filter for ids which are only in these collections")) + + .subcommand(SubCommand::with_name("where") + .arg(Arg::with_name("where-filter") + .index(1) + .required(true) + .takes_value(true) + .multiple(false) + .value_names(&["QUERY"]) + .help("Query the header of the entries and filter them")) + ) }