Reimplement imag-bookmark commandline frontend
Signed-off-by: Matthias Beyer <mail@beyermatthias.de>
This commit is contained in:
parent
37b7b2e67e
commit
4f2ea236af
3 changed files with 92 additions and 136 deletions
|
@ -25,11 +25,14 @@ toml = "0.5.1"
|
||||||
toml-query = "0.9.2"
|
toml-query = "0.9.2"
|
||||||
failure = "0.1.5"
|
failure = "0.1.5"
|
||||||
resiter = "0.4.0"
|
resiter = "0.4.0"
|
||||||
|
url = "2"
|
||||||
|
|
||||||
libimagrt = { version = "0.10.0", path = "../../../lib/core/libimagrt" }
|
libimagrt = { version = "0.10.0", path = "../../../lib/core/libimagrt" }
|
||||||
libimagerror = { version = "0.10.0", path = "../../../lib/core/libimagerror" }
|
libimagerror = { version = "0.10.0", path = "../../../lib/core/libimagerror" }
|
||||||
|
libimagstore = { version = "0.10.0", path = "../../../lib/core/libimagstore" }
|
||||||
libimagbookmark = { version = "0.10.0", path = "../../../lib/domain/libimagbookmark" }
|
libimagbookmark = { version = "0.10.0", path = "../../../lib/domain/libimagbookmark" }
|
||||||
libimagentrylink = { version = "0.10.0", path = "../../../lib/entry/libimagentrylink" }
|
libimagentrylink = { version = "0.10.0", path = "../../../lib/entry/libimagentrylink" }
|
||||||
|
libimagentryurl = { version = "0.10.0", path = "../../../lib/entry/libimagentryurl" }
|
||||||
libimagutil = { version = "0.10.0", path = "../../../lib/etc/libimagutil" }
|
libimagutil = { version = "0.10.0", path = "../../../lib/etc/libimagutil" }
|
||||||
|
|
||||||
[dependencies.clap]
|
[dependencies.clap]
|
||||||
|
@ -37,6 +40,11 @@ version = "2.33.0"
|
||||||
default-features = false
|
default-features = false
|
||||||
features = ["color", "suggestions", "wrap_help"]
|
features = ["color", "suggestions", "wrap_help"]
|
||||||
|
|
||||||
|
[dependencies.uuid]
|
||||||
|
version = "0.7"
|
||||||
|
default-features = false
|
||||||
|
features = ["serde", "v4"]
|
||||||
|
|
||||||
[lib]
|
[lib]
|
||||||
name = "libimagbookmarkfrontend"
|
name = "libimagbookmarkfrontend"
|
||||||
path = "src/lib.rs"
|
path = "src/lib.rs"
|
||||||
|
|
|
@ -37,6 +37,8 @@
|
||||||
extern crate clap;
|
extern crate clap;
|
||||||
#[macro_use] extern crate log;
|
#[macro_use] extern crate log;
|
||||||
extern crate toml;
|
extern crate toml;
|
||||||
|
extern crate url;
|
||||||
|
extern crate uuid;
|
||||||
extern crate toml_query;
|
extern crate toml_query;
|
||||||
#[macro_use] extern crate failure;
|
#[macro_use] extern crate failure;
|
||||||
extern crate resiter;
|
extern crate resiter;
|
||||||
|
@ -44,25 +46,27 @@ extern crate resiter;
|
||||||
extern crate libimagbookmark;
|
extern crate libimagbookmark;
|
||||||
extern crate libimagrt;
|
extern crate libimagrt;
|
||||||
extern crate libimagerror;
|
extern crate libimagerror;
|
||||||
|
extern crate libimagstore;
|
||||||
extern crate libimagutil;
|
extern crate libimagutil;
|
||||||
extern crate libimagentrylink;
|
extern crate libimagentryurl;
|
||||||
|
|
||||||
use std::io::Write;
|
use std::io::Write;
|
||||||
use std::ops::DerefMut;
|
|
||||||
|
|
||||||
use toml_query::read::TomlValueReadTypeExt;
|
|
||||||
use failure::Error;
|
use failure::Error;
|
||||||
use failure::err_msg;
|
use failure::err_msg;
|
||||||
use failure::Fallible as Result;
|
use failure::Fallible as Result;
|
||||||
use resiter::AndThen;
|
use resiter::AndThen;
|
||||||
|
use resiter::IterInnerOkOrElse;
|
||||||
use clap::App;
|
use clap::App;
|
||||||
|
use url::Url;
|
||||||
|
|
||||||
use libimagrt::runtime::Runtime;
|
use libimagrt::runtime::Runtime;
|
||||||
use libimagrt::application::ImagApplication;
|
use libimagrt::application::ImagApplication;
|
||||||
use libimagbookmark::collection::BookmarkCollection;
|
use libimagstore::iter::get::StoreIdGetIteratorExtension;
|
||||||
use libimagbookmark::collection::BookmarkCollectionStore;
|
use libimagbookmark::store::BookmarkStore;
|
||||||
use libimagbookmark::link::Link as BookmarkLink;
|
use libimagbookmark::bookmark::Bookmark;
|
||||||
use libimagentrylink::linkable::Linkable;
|
use libimagentryurl::link::Link;
|
||||||
|
|
||||||
|
|
||||||
mod ui;
|
mod ui;
|
||||||
|
|
||||||
|
@ -75,7 +79,6 @@ impl ImagApplication for ImagBookmark {
|
||||||
fn run(rt: Runtime) -> Result<()> {
|
fn run(rt: Runtime) -> Result<()> {
|
||||||
match rt.cli().subcommand_name().ok_or_else(|| err_msg("No subcommand called"))? {
|
match rt.cli().subcommand_name().ok_or_else(|| err_msg("No subcommand called"))? {
|
||||||
"add" => add(&rt),
|
"add" => add(&rt),
|
||||||
"collection" => collection(&rt),
|
|
||||||
"list" => list(&rt),
|
"list" => list(&rt),
|
||||||
"remove" => remove(&rt),
|
"remove" => remove(&rt),
|
||||||
other => {
|
other => {
|
||||||
|
@ -108,105 +111,52 @@ impl ImagApplication for ImagBookmark {
|
||||||
|
|
||||||
fn add(rt: &Runtime) -> Result<()> {
|
fn add(rt: &Runtime) -> Result<()> {
|
||||||
let scmd = rt.cli().subcommand_matches("add").unwrap();
|
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")
|
scmd.values_of("urls")
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.map(|url| {
|
.map(|s| Url::parse(s).map_err(Error::from))
|
||||||
let new_ids = BookmarkCollection::add_link(collection.deref_mut(), rt.store(), BookmarkLink::from(url))?;
|
.and_then_ok(|url| {
|
||||||
rt.report_all_touched(new_ids.into_iter()).map_err(Error::from)
|
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()
|
.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<()> {
|
fn list(rt: &Runtime) -> Result<()> {
|
||||||
let coll = get_collection_name(rt, "list", "collection")?;
|
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)?;
|
||||||
|
}
|
||||||
|
|
||||||
let collection = BookmarkCollectionStore::get(rt.store(), &coll)?
|
rt.report_touched(entry.get_location()).map_err(Error::from)
|
||||||
.ok_or_else(|| format_err!("No bookmark collection '{}' found", coll))?;
|
} else {
|
||||||
|
Ok(())
|
||||||
rt.report_touched(collection.get_location())?;
|
}
|
||||||
|
|
||||||
let mut i = 0; // poor mans enumerate()
|
|
||||||
|
|
||||||
collection
|
|
||||||
.get_links(rt.store())?
|
|
||||||
.and_then_ok(|link| {
|
|
||||||
let r = writeln!(rt.stdout(), "{: >3}: {}", i, link).map_err(Error::from);
|
|
||||||
i += 1;
|
|
||||||
r
|
|
||||||
})
|
})
|
||||||
.collect()
|
.collect()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn remove(rt: &Runtime) -> Result<()> {
|
fn remove(rt: &Runtime) -> Result<()> {
|
||||||
let scmd = rt.cli().subcommand_matches("remove").unwrap();
|
rt.ids::<crate::ui::PathProvider>()?
|
||||||
let coll = get_collection_name(rt, "list", "collection")?;
|
.ok_or_else(|| err_msg("No ids supplied"))?
|
||||||
|
|
||||||
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()
|
.into_iter()
|
||||||
.map(|url| {
|
.map(Ok)
|
||||||
let removed_links = BookmarkCollection::remove_link(collection.deref_mut(), rt.store(), BookmarkLink::from(url))?;
|
.into_get_iter(rt.store())
|
||||||
rt.report_all_touched(removed_links.into_iter()).map_err(Error::from)
|
.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()
|
.collect()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
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)
|
|
||||||
} 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'."))
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
|
@ -17,23 +17,21 @@
|
||||||
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||||
//
|
//
|
||||||
|
|
||||||
use clap::{Arg, App, SubCommand};
|
use std::path::PathBuf;
|
||||||
|
|
||||||
|
use failure::Fallible as Result;
|
||||||
|
use clap::{Arg, ArgMatches, App, SubCommand};
|
||||||
|
|
||||||
use libimagutil::cli_validators::*;
|
use libimagutil::cli_validators::*;
|
||||||
|
use libimagstore::storeid::StoreId;
|
||||||
|
use libimagstore::storeid::IntoStoreId;
|
||||||
|
use libimagrt::runtime::IdPathProvider;
|
||||||
|
|
||||||
pub fn build_ui<'a>(app: App<'a, 'a>) -> App<'a, 'a> {
|
pub fn build_ui<'a>(app: App<'a, 'a>) -> App<'a, 'a> {
|
||||||
app
|
app
|
||||||
.subcommand(SubCommand::with_name("add")
|
.subcommand(SubCommand::with_name("add")
|
||||||
.about("Add bookmarks")
|
.about("Add bookmarks")
|
||||||
.version("0.1")
|
.version("0.1")
|
||||||
.arg(Arg::with_name("collection")
|
|
||||||
.long("collection")
|
|
||||||
.short("c")
|
|
||||||
.takes_value(true)
|
|
||||||
.required(false)
|
|
||||||
.multiple(false)
|
|
||||||
.value_name("COLLECTION")
|
|
||||||
.help("Add to this collection, if not specified default from config will be used"))
|
|
||||||
.arg(Arg::with_name("urls")
|
.arg(Arg::with_name("urls")
|
||||||
.index(1)
|
.index(1)
|
||||||
.takes_value(true)
|
.takes_value(true)
|
||||||
|
@ -47,22 +45,13 @@ pub fn build_ui<'a>(app: App<'a, 'a>) -> App<'a, 'a> {
|
||||||
.subcommand(SubCommand::with_name("remove")
|
.subcommand(SubCommand::with_name("remove")
|
||||||
.about("Remove bookmarks")
|
.about("Remove bookmarks")
|
||||||
.version("0.1")
|
.version("0.1")
|
||||||
.arg(Arg::with_name("collection")
|
.arg(Arg::with_name("ids")
|
||||||
.long("collection")
|
|
||||||
.short("c")
|
|
||||||
.takes_value(true)
|
|
||||||
.required(false)
|
|
||||||
.multiple(false)
|
|
||||||
.value_name("COLLECTION")
|
|
||||||
.help("Remove from this collection, if not specified default from config will be used"))
|
|
||||||
.arg(Arg::with_name("urls")
|
|
||||||
.index(1)
|
.index(1)
|
||||||
.takes_value(true)
|
.takes_value(true)
|
||||||
.required(true)
|
.required(true)
|
||||||
.multiple(true)
|
.multiple(true)
|
||||||
.value_name("URL")
|
.value_name("ID")
|
||||||
.validator(is_url)
|
.help("Remove these urls, specified as ID"))
|
||||||
.help("Remove this url(s)"))
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// .subcommand(SubCommand::with_name("open")
|
// .subcommand(SubCommand::with_name("open")
|
||||||
|
@ -79,32 +68,41 @@ pub fn build_ui<'a>(app: App<'a, 'a>) -> App<'a, 'a> {
|
||||||
// )
|
// )
|
||||||
|
|
||||||
.subcommand(SubCommand::with_name("list")
|
.subcommand(SubCommand::with_name("list")
|
||||||
.about("List bookmarks")
|
.about("List bookmarks, if used in pipe, ignores everything that is not a bookmark")
|
||||||
.version("0.1")
|
.version("0.1")
|
||||||
.arg(Arg::with_name("collection")
|
|
||||||
.long("collection")
|
.arg(Arg::with_name("ids")
|
||||||
.short("c")
|
.index(1)
|
||||||
.takes_value(true)
|
.takes_value(true)
|
||||||
.required(false)
|
.required(false)
|
||||||
.multiple(false)
|
.multiple(true)
|
||||||
.value_name("COLLECTION")
|
.value_name("ID")
|
||||||
.help("Select from this collection, if not specified default from config will be used"))
|
.help("IDs of bookmarks to list"))
|
||||||
)
|
|
||||||
|
|
||||||
.subcommand(SubCommand::with_name("collection")
|
|
||||||
.about("Collection commands")
|
|
||||||
.version("0.1")
|
|
||||||
.arg(Arg::with_name("add")
|
|
||||||
.long("add")
|
|
||||||
.short("a")
|
|
||||||
.takes_value(true)
|
|
||||||
.value_name("NAME")
|
|
||||||
.help("Add a collection with this name"))
|
|
||||||
.arg(Arg::with_name("remove")
|
|
||||||
.long("remove")
|
|
||||||
.short("r")
|
|
||||||
.takes_value(true)
|
|
||||||
.value_name("NAME")
|
|
||||||
.help("Remove a collection with this name (and all links)"))
|
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub struct PathProvider;
|
||||||
|
impl IdPathProvider for PathProvider {
|
||||||
|
fn get_ids(matches: &ArgMatches) -> Result<Option<Vec<StoreId>>> {
|
||||||
|
fn no_ids_error() -> Result<Option<Vec<StoreId>>> {
|
||||||
|
Err(format_err!("Command does not get IDs as input"))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_id_paths(field: &str, subm: &ArgMatches) -> Result<Option<Vec<StoreId>>> {
|
||||||
|
subm.values_of(field)
|
||||||
|
.map(|v| v
|
||||||
|
.map(PathBuf::from)
|
||||||
|
.map(|pb| pb.into_storeid())
|
||||||
|
.collect::<Result<Vec<_>>>()
|
||||||
|
)
|
||||||
|
.transpose()
|
||||||
|
}
|
||||||
|
|
||||||
|
match matches.subcommand() {
|
||||||
|
("add", _) => no_ids_error(),
|
||||||
|
("remove", Some(subm)) => get_id_paths("ids", subm),
|
||||||
|
("list", Some(subm)) => get_id_paths("ids", subm),
|
||||||
|
(other, _) => Err(format_err!("Not a known command: {}", other)),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue