Merge pull request #1386 from matthiasbeyer/imag-ids/filter-for-collection
imag-ids: Add functionality to filter for a collection
This commit is contained in:
commit
7d96ee5e9b
2 changed files with 36 additions and 0 deletions
|
@ -22,6 +22,8 @@ is-it-maintained-open-issues = { repository = "matthiasbeyer/imag" }
|
||||||
maintenance = { status = "actively-developed" }
|
maintenance = { status = "actively-developed" }
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
filters = "0.2"
|
||||||
|
|
||||||
libimagstore = { version = "0.7.0", path = "../../../lib/core/libimagstore" }
|
libimagstore = { version = "0.7.0", path = "../../../lib/core/libimagstore" }
|
||||||
libimagrt = { version = "0.7.0", path = "../../../lib/core/libimagrt" }
|
libimagrt = { version = "0.7.0", path = "../../../lib/core/libimagrt" }
|
||||||
libimagerror = { version = "0.7.0", path = "../../../lib/core/libimagerror" }
|
libimagerror = { version = "0.7.0", path = "../../../lib/core/libimagerror" }
|
||||||
|
|
|
@ -33,6 +33,7 @@
|
||||||
)]
|
)]
|
||||||
|
|
||||||
extern crate clap;
|
extern crate clap;
|
||||||
|
extern crate filters;
|
||||||
|
|
||||||
extern crate libimagerror;
|
extern crate libimagerror;
|
||||||
extern crate libimagstore;
|
extern crate libimagstore;
|
||||||
|
@ -40,12 +41,14 @@ extern crate libimagstore;
|
||||||
|
|
||||||
use std::io::Write;
|
use std::io::Write;
|
||||||
|
|
||||||
|
use filters::filter::Filter;
|
||||||
use clap::{Arg, App};
|
use clap::{Arg, App};
|
||||||
|
|
||||||
use libimagrt::setup::generate_runtime_setup;
|
use libimagrt::setup::generate_runtime_setup;
|
||||||
use libimagerror::trace::MapErrTrace;
|
use libimagerror::trace::MapErrTrace;
|
||||||
use libimagerror::exit::ExitUnwrap;
|
use libimagerror::exit::ExitUnwrap;
|
||||||
use libimagerror::io::ToExitCode;
|
use libimagerror::io::ToExitCode;
|
||||||
|
use libimagstore::storeid::StoreId;
|
||||||
|
|
||||||
|
|
||||||
/// No special CLI
|
/// No special CLI
|
||||||
|
@ -57,6 +60,29 @@ pub fn build_ui<'a>(app: App<'a, 'a>) -> App<'a, 'a> {
|
||||||
.required(false)
|
.required(false)
|
||||||
.multiple(false)
|
.multiple(false)
|
||||||
.help("Print the storepath for each id"))
|
.help("Print the storepath for each id"))
|
||||||
|
|
||||||
|
.arg(Arg::with_name("in-collection-filter")
|
||||||
|
.long("in-collection")
|
||||||
|
.short("c")
|
||||||
|
.required(false)
|
||||||
|
.takes_value(true)
|
||||||
|
.multiple(true)
|
||||||
|
.value_names(&["COLLECTION"])
|
||||||
|
.help("Filter for ids which are only in these collections"))
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct IsInCollectionsFilter<'a, A>(Option<A>, ::std::marker::PhantomData<&'a str>)
|
||||||
|
where A: AsRef<[&'a str]>;
|
||||||
|
|
||||||
|
impl<'a, A> Filter<StoreId> for IsInCollectionsFilter<'a, A>
|
||||||
|
where A: AsRef<[&'a str]> + 'a
|
||||||
|
{
|
||||||
|
fn filter(&self, sid: &StoreId) -> bool {
|
||||||
|
match self.0 {
|
||||||
|
Some(ref colls) => sid.is_in_collection(colls),
|
||||||
|
None => true,
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
|
@ -68,9 +94,17 @@ fn main() {
|
||||||
|
|
||||||
let print_storepath = rt.cli().is_present("print-storepath");
|
let print_storepath = rt.cli().is_present("print-storepath");
|
||||||
|
|
||||||
|
let values = rt
|
||||||
|
.cli()
|
||||||
|
.values_of("in-collection-filter")
|
||||||
|
.map(|v| v.collect::<Vec<&str>>());
|
||||||
|
|
||||||
|
let collection_filter = IsInCollectionsFilter(values, ::std::marker::PhantomData);
|
||||||
|
|
||||||
rt.store()
|
rt.store()
|
||||||
.entries()
|
.entries()
|
||||||
.map_err_trace_exit_unwrap(1)
|
.map_err_trace_exit_unwrap(1)
|
||||||
|
.filter(|id| collection_filter.filter(id))
|
||||||
.map(|id| if print_storepath {
|
.map(|id| if print_storepath {
|
||||||
id
|
id
|
||||||
} else {
|
} else {
|
||||||
|
|
Loading…
Reference in a new issue