Add imag-tag-present command for filtering entries

Signed-off-by: Matthias Beyer <mail@beyermatthias.de>
This commit is contained in:
Matthias Beyer 2019-11-10 22:39:08 +01:00
parent 9e532f79dd
commit cec4d93f3e
2 changed files with 51 additions and 0 deletions

View file

@ -66,10 +66,12 @@ use failure::Error;
use failure::err_msg; use failure::err_msg;
use resiter::AndThen; use resiter::AndThen;
use resiter::Map; use resiter::Map;
use resiter::FilterMap;
use libimagrt::runtime::Runtime; use libimagrt::runtime::Runtime;
use libimagrt::application::ImagApplication; use libimagrt::application::ImagApplication;
use libimagentrytag::tagable::Tagable; use libimagentrytag::tagable::Tagable;
use libimagentrytag::tag::is_tag_str;
use libimagentrytag::tag::Tag; use libimagentrytag::tag::Tag;
use libimagstore::storeid::StoreId; use libimagstore::storeid::StoreId;
@ -106,6 +108,45 @@ impl ImagApplication for ImagTag {
alter(&rt, id, add, rem) alter(&rt, id, add, rem)
}).collect(), }).collect(),
("present", Some(scmd)) => {
let must_be_present = scmd
.values_of("present-tag")
.unwrap()
.map(String::from)
.collect::<Vec<String>>();
must_be_present.iter().map(|t| is_tag_str(t)).collect::<Result<Vec<_>>>()?;
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::<Result<Vec<_>>>()
.map(|_| ())
},
(other, _) => { (other, _) => {
debug!("Unknown command"); debug!("Unknown command");
if rt.handle_unknown_subcommand("imag-tag", other, rt.cli())?.success() { if rt.handle_unknown_subcommand("imag-tag", other, rt.cli())?.success() {

View file

@ -101,6 +101,16 @@ pub fn build_ui<'a>(app: App<'a, 'a>) -> App<'a, 'a> {
.required(true)) .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; pub struct PathProvider;