diff options
Diffstat (limited to 'bin/domain/imag-bookmark/src/lib.rs')
-rw-r--r-- | bin/domain/imag-bookmark/src/lib.rs | 157 |
1 files changed, 70 insertions, 87 deletions
diff --git a/bin/domain/imag-bookmark/src/lib.rs b/bin/domain/imag-bookmark/src/lib.rs index 537c6f4d..6782ed59 100644 --- a/bin/domain/imag-bookmark/src/lib.rs +++ b/bin/domain/imag-bookmark/src/lib.rs @@ -37,6 +37,8 @@ extern crate clap; #[macro_use] extern crate log; extern crate toml; +extern crate url; +extern crate uuid; extern crate toml_query; #[macro_use] extern crate failure; extern crate resiter; @@ -44,25 +46,27 @@ extern crate resiter; extern crate libimagbookmark; extern crate libimagrt; extern crate libimagerror; +extern crate libimagstore; extern crate libimagutil; -extern crate libimagentrylink; +extern crate libimagentryurl; use std::io::Write; -use std::ops::DerefMut; -use toml_query::read::TomlValueReadTypeExt; use failure::Error; use failure::err_msg; use failure::Fallible as Result; use resiter::AndThen; +use resiter::IterInnerOkOrElse; use clap::App; +use url::Url; use libimagrt::runtime::Runtime; use libimagrt::application::ImagApplication; -use libimagbookmark::collection::BookmarkCollection; -use libimagbookmark::collection::BookmarkCollectionStore; -use libimagbookmark::link::Link as BookmarkLink; -use libimagentrylink::linkable::Linkable; +use libimagstore::iter::get::StoreIdGetIteratorExtension; +use libimagbookmark::store::BookmarkStore; +use libimagbookmark::bookmark::Bookmark; +use libimagentryurl::link::Link; + mod ui; @@ -75,9 +79,9 @@ impl ImagApplication for ImagBookmark { fn run(rt: Runtime) -> Result<()> { match rt.cli().subcommand_name().ok_or_else(|| err_msg("No subcommand called"))? { "add" => add(&rt), - "collection" => collection(&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() { @@ -108,105 +112,84 @@ impl ImagApplication for ImagBookmark { fn add(rt: &Runtime) -> Result<()> { let scmd = rt.cli().subcommand_matches("add").unwrap(); - let coll = get_collection_name(rt, "add", "collection")?; - - let mut collection = BookmarkCollectionStore::get(rt.store(), &coll)? - .ok_or_else(|| format_err!("No bookmark collection '{}' found", coll))?; - - rt.report_touched(collection.get_location())?; - scmd.values_of("urls") .unwrap() .into_iter() - .map(|url| { - let new_ids = BookmarkCollection::add_link(collection.deref_mut(), rt.store(), BookmarkLink::from(url))?; - rt.report_all_touched(new_ids.into_iter()).map_err(Error::from) + .map(|s| Url::parse(s).map_err(Error::from)) + .and_then_ok(|url| { + let (uuid, fle) = rt.store().add_bookmark(url.clone())?; + debug!("Created entry for url '{}' with uuid '{}'", url, uuid); + info!("{} = {}", url, uuid); + rt.report_touched(fle.get_location()).map_err(Error::from) }) .collect() } -fn collection(rt: &Runtime) -> Result<()> { - let scmd = rt.cli().subcommand_matches("collection").unwrap(); - - if scmd.is_present("add") { // adding a new collection - let name = scmd.value_of("add").unwrap(); - let id = BookmarkCollectionStore::new(rt.store(), &name)?; - rt.report_touched(id.get_location())?; - info!("Created: {}", name); - } - - if scmd.is_present("remove") { // remove a collection - let name = scmd.value_of("remove").unwrap(); - - { // remove all links - BookmarkCollectionStore::get(rt.store(), &name)? - .ok_or_else(|| format_err!("Collection does not exist: {}", name))? - .unlink(rt.store())?; - } - - BookmarkCollectionStore::delete(rt.store(), &name)?; - info!("Deleted: {}", name); - } - - Ok(()) -} - fn list(rt: &Runtime) -> Result<()> { - let coll = get_collection_name(rt, "list", "collection")?; - - let collection = BookmarkCollectionStore::get(rt.store(), &coll)? - .ok_or_else(|| format_err!("No bookmark collection '{}' found", coll))?; - - rt.report_touched(collection.get_location())?; - - let mut i = 0; // poor mans enumerate() + rt.store() + .all_bookmarks()? + .into_get_iter() + .map_inner_ok_or_else(|| err_msg("Did not find one entry")) + .and_then_ok(|entry| { + if entry.is_bookmark()? { + let url = entry.get_url()? + .ok_or_else(|| format_err!("Failed to retrieve URL for {}", entry.get_location()))?; + if !rt.output_is_pipe() { + writeln!(rt.stdout(), "{}", url)?; + } - collection - .get_links(rt.store())? - .and_then_ok(|link| { - let r = writeln!(rt.stdout(), "{: >3}: {}", i, link).map_err(Error::from); - i += 1; - r + rt.report_touched(entry.get_location()).map_err(Error::from) + } else { + Ok(()) + } }) .collect() } fn remove(rt: &Runtime) -> Result<()> { - let scmd = rt.cli().subcommand_matches("remove").unwrap(); - let coll = get_collection_name(rt, "list", "collection")?; - - let mut collection = BookmarkCollectionStore::get(rt.store(), &coll)? - .ok_or_else(|| format_err!("No bookmark collection '{}' found", coll))?; - - rt.report_touched(collection.get_location())?; - - scmd.values_of("urls") - .unwrap() + rt.ids::<crate::ui::PathProvider>()? + .ok_or_else(|| err_msg("No ids supplied"))? .into_iter() - .map(|url| { - let removed_links = BookmarkCollection::remove_link(collection.deref_mut(), rt.store(), BookmarkLink::from(url))?; - rt.report_all_touched(removed_links.into_iter()).map_err(Error::from) + .map(Ok) + .into_get_iter(rt.store()) + .map_inner_ok_or_else(|| err_msg("Did not find one entry")) + .and_then_ok(|fle| { + rt.report_touched(fle.get_location()) + .map_err(Error::from) + .and_then(|_| rt.store().remove_bookmark(fle)) }) .collect() } +fn find(rt: &Runtime) -> Result<()> { + let substr = rt.cli().subcommand_matches("find").unwrap().value_of("substr").unwrap(); -fn get_collection_name(rt: &Runtime, - subcommand_name: &str, - collection_argument_name: &str) - -> Result<String> -{ - if let Some(cn) = rt.cli() - .subcommand_matches(subcommand_name) - .and_then(|scmd| scmd.value_of(collection_argument_name).map(String::from)) - { - return Ok(cn) + if let Some(ids) = rt.ids::<crate::ui::PathProvider>()? { + ids.into_iter() + .map(Ok) + .into_get_iter(rt.store()) } else { - rt.config().ok_or_else(|| err_msg("No configuration availablew")) - .and_then(|cfg| { - cfg.read_string("bookmark.default_collection")? - .ok_or_else(|| err_msg("Missing config: 'bookmark.default_collection'.")) - }) + 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() } - |