diff --git a/bin/domain/imag-bookmark/src/lib.rs b/bin/domain/imag-bookmark/src/lib.rs index 98f50fca..6782ed59 100644 --- a/bin/domain/imag-bookmark/src/lib.rs +++ b/bin/domain/imag-bookmark/src/lib.rs @@ -81,6 +81,7 @@ impl ImagApplication for ImagBookmark { "add" => add(&rt), "list" => list(&rt), "remove" => remove(&rt), + "find" => find(&rt), other => { debug!("Unknown command"); if rt.handle_unknown_subcommand("imag-bookmark", other, rt.cli())?.success() { @@ -160,3 +161,35 @@ fn remove(rt: &Runtime) -> Result<()> { .collect() } +fn find(rt: &Runtime) -> Result<()> { + let substr = rt.cli().subcommand_matches("find").unwrap().value_of("substr").unwrap(); + + if let Some(ids) = rt.ids::()? { + ids.into_iter() + .map(Ok) + .into_get_iter(rt.store()) + } else { + rt.store() + .all_bookmarks()? + .into_get_iter() + } + .map_inner_ok_or_else(|| err_msg("Did not find one entry")) + .and_then_ok(|fle| { + if fle.is_bookmark()? { + let url = fle + .get_url()? + .ok_or_else(|| format_err!("Failed to retrieve URL for {}", fle.get_location()))?; + if url.as_str().contains(substr) { + if !rt.output_is_pipe() { + writeln!(rt.stdout(), "{}", url)?; + } + rt.report_touched(fle.get_location()).map_err(Error::from) + } else { + Ok(()) + } + } else { + Ok(()) + } + }) + .collect() +} diff --git a/bin/domain/imag-bookmark/src/ui.rs b/bin/domain/imag-bookmark/src/ui.rs index e248a420..d1024764 100644 --- a/bin/domain/imag-bookmark/src/ui.rs +++ b/bin/domain/imag-bookmark/src/ui.rs @@ -79,6 +79,27 @@ pub fn build_ui<'a>(app: App<'a, 'a>) -> App<'a, 'a> { .value_name("ID") .help("IDs of bookmarks to list")) ) + + .subcommand(SubCommand::with_name("find") + .about("Find a bookmark by substring of URL") + .version("0.1") + + .arg(Arg::with_name("substr") + .index(1) + .takes_value(true) + .required(true) + .multiple(false) + .value_name("str") + .help("Substring to search in the URL.")) + + .arg(Arg::with_name("ids") + .index(2) + .takes_value(true) + .required(false) + .multiple(true) + .value_name("IDs") + .help("IDs to search in (if not passed, searches all bookmarks. Can also be provided via STDIN")) + ) } pub struct PathProvider; @@ -102,6 +123,7 @@ impl IdPathProvider for PathProvider { ("add", _) => no_ids_error(), ("remove", Some(subm)) => get_id_paths("ids", subm), ("list", Some(subm)) => get_id_paths("ids", subm), + ("find", Some(subm)) => get_id_paths("ids", subm), (other, _) => Err(format_err!("Not a known command: {}", other)), } }