diff options
Diffstat (limited to 'bin/domain/imag-bookmark/src/lib.rs')
-rw-r--r-- | bin/domain/imag-bookmark/src/lib.rs | 90 |
1 files changed, 90 insertions, 0 deletions
diff --git a/bin/domain/imag-bookmark/src/lib.rs b/bin/domain/imag-bookmark/src/lib.rs index 6782ed59..21d4bd4a 100644 --- a/bin/domain/imag-bookmark/src/lib.rs +++ b/bin/domain/imag-bookmark/src/lib.rs @@ -42,6 +42,8 @@ extern crate uuid; extern crate toml_query; #[macro_use] extern crate failure; extern crate resiter; +extern crate handlebars; +extern crate rayon; extern crate libimagbookmark; extern crate libimagrt; @@ -51,6 +53,8 @@ extern crate libimagutil; extern crate libimagentryurl; use std::io::Write; +use std::collections::BTreeMap; +use std::process::Command; use failure::Error; use failure::err_msg; @@ -59,10 +63,15 @@ use resiter::AndThen; use resiter::IterInnerOkOrElse; use clap::App; use url::Url; +use handlebars::Handlebars; +use rayon::iter::ParallelIterator; +use rayon::iter::IntoParallelIterator; +use toml_query::read::TomlValueReadExt; use libimagrt::runtime::Runtime; use libimagrt::application::ImagApplication; use libimagstore::iter::get::StoreIdGetIteratorExtension; +use libimagstore::store::FileLockEntry; use libimagbookmark::store::BookmarkStore; use libimagbookmark::bookmark::Bookmark; use libimagentryurl::link::Link; @@ -79,6 +88,7 @@ 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), + "open" => open(&rt), "list" => list(&rt), "remove" => remove(&rt), "find" => find(&rt), @@ -125,6 +135,63 @@ fn add(rt: &Runtime) -> Result<()> { .collect() } +fn open(rt: &Runtime) -> Result<()> { + let scmd = rt.cli().subcommand_matches("open").unwrap(); + let open_command = rt.config() + .map(|value| { + value.read("bookmark.open")? + .ok_or_else(|| err_msg("Configuration missing: 'bookmark.open'"))? + .as_str() + .ok_or_else(|| err_msg("Open command should be a string")) + }) + .or_else(|| Ok(scmd.value_of("opencmd")).transpose()) + .unwrap_or_else(|| Err(err_msg("No open command available in config or on commandline")))?; + + let hb = { + let mut hb = Handlebars::new(); + hb.register_template_string("format", open_command)?; + hb + }; + + let iter = rt.ids::<crate::ui::PathProvider>()? + .ok_or_else(|| err_msg("No ids supplied"))? + .into_iter() + .map(Ok) + .into_get_iter(rt.store()) + .map_inner_ok_or_else(|| err_msg("Did not find one entry")); + + if scmd.is_present("openparallel") { + let links = iter + .and_then_ok(|link| rt.report_touched(link.get_location()).map_err(Error::from).map(|_| link)) + .and_then_ok(|link| calculate_command_data(&hb, &link, open_command)) + .collect::<Result<Vec<_>>>()?; + + links + .into_par_iter() + .map(|command_rendered| { + Command::new(&command_rendered[0]) // indexing save with check above + .args(&command_rendered[1..]) + .output() + .map_err(Error::from) + }) + .collect::<Result<Vec<_>>>() + .map(|_| ()) + } else { + iter.and_then_ok(|link| { + rt.report_touched(link.get_location()).map_err(Error::from).map(|_| link) + }) + .and_then_ok(|link| { + let command_rendered = calculate_command_data(&hb, &link, open_command)?; + Command::new(&command_rendered[0]) // indexing save with check above + .args(&command_rendered[1..]) + .output() + .map_err(Error::from) + }) + .collect::<Result<Vec<_>>>() + .map(|_| ()) + } +} + fn list(rt: &Runtime) -> Result<()> { rt.store() .all_bookmarks()? @@ -193,3 +260,26 @@ fn find(rt: &Runtime) -> Result<()> { }) .collect() } + +fn calculate_command_data<'a>(hb: &Handlebars, entry: &FileLockEntry<'a>, open_command: &str) -> Result<Vec<String>> { + let url = entry.get_url()? + .ok_or_else(|| format_err!("Failed to retrieve URL for {}", entry.get_location()))? + .into_string(); + + let data = { + let mut data = BTreeMap::new(); + data.insert("url", url); + data + }; + + let command_rendered = hb.render("format", &data)? + .split_whitespace() + .map(String::from) + .collect::<Vec<String>>(); + + if command_rendered.len() > 2 { + return Err(format_err!("Command seems not to include URL: '{}'", open_command)); + } + + Ok(command_rendered.into_iter().map(String::from).collect()) +} |