Implement 'where' subcommand

This commit is contained in:
Matthias Beyer 2018-04-29 14:37:50 +02:00
parent d518322401
commit f4e1c0864c
2 changed files with 34 additions and 1 deletions

View File

@ -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::<Vec<&str>>());
let collection_filter = IsInCollectionsFilter::new(values);
let query_filter : Option<id_filters::header_filter_lang::Query> = 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 {

View File

@ -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"))
)
}