summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMatthias Beyer <mail@beyermatthias.de>2019-12-08 14:00:31 +0100
committerMatthias Beyer <mail@beyermatthias.de>2019-12-08 14:00:32 +0100
commit401e900936a38ce492076b53b551dce0af42f824 (patch)
tree7a1f94034b4cbe7e509eb3bbedced1b27c511510
parent4f2ea236af73d9fc03819933c78d46fbd39e9820 (diff)
downloadimag-401e900936a38ce492076b53b551dce0af42f824.tar.gz
imag-401e900936a38ce492076b53b551dce0af42f824.tar.xz
-rw-r--r--bin/domain/imag-bookmark/src/lib.rs33
-rw-r--r--bin/domain/imag-bookmark/src/ui.rs22
2 files changed, 55 insertions, 0 deletions
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::<crate::ui::PathProvider>()? {
+ 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)),
}
}