summaryrefslogtreecommitdiffstats
path: root/bin/core/imag-annotate
diff options
context:
space:
mode:
authorMatthias Beyer <mail@beyermatthias.de>2018-09-29 17:44:36 +0200
committerMatthias Beyer <mail@beyermatthias.de>2018-11-06 20:33:30 +0100
commit01de94a387f97adffb7a12745fa667f4e7966d55 (patch)
treedf3ae28f3db76b1f5e5420424134e3fff7993d78 /bin/core/imag-annotate
parentc1c74973e39814cda66f09f56d4132aed3bebc35 (diff)
Move imag-annotate to ID provider infrastructure
Diffstat (limited to 'bin/core/imag-annotate')
-rw-r--r--bin/core/imag-annotate/src/main.rs155
-rw-r--r--bin/core/imag-annotate/src/ui.rs62
2 files changed, 141 insertions, 76 deletions
diff --git a/bin/core/imag-annotate/src/main.rs b/bin/core/imag-annotate/src/main.rs
index 639488ea..5beb0c1b 100644
--- a/bin/core/imag-annotate/src/main.rs
+++ b/bin/core/imag-annotate/src/main.rs
@@ -45,10 +45,8 @@ extern crate libimagstore;
extern crate libimagutil;
use std::io::Write;
-use std::path::PathBuf;
use failure::Error;
-use failure::err_msg;
use libimagentryannotation::annotateable::*;
use libimagentryannotation::annotation_fetcher::*;
@@ -56,10 +54,10 @@ use libimagentryedit::edit::*;
use libimagerror::trace::MapErrTrace;
use libimagerror::exit::ExitUnwrap;
use libimagerror::io::ToExitCode;
+use libimagerror::errors::ErrorMsg as EM;
use libimagrt::runtime::Runtime;
use libimagrt::setup::generate_runtime_setup;
use libimagstore::store::FileLockEntry;
-use libimagstore::storeid::IntoStoreId;
mod ui;
@@ -91,89 +89,96 @@ fn main() {
fn add(rt: &Runtime) {
let scmd = rt.cli().subcommand_matches("add").unwrap(); // safed by main()
let annotation_name = scmd.value_of("annotation_name").unwrap(); // safed by clap
- let entry_name = scmd
- .value_of("entry")
- .map(PathBuf::from)
- .map(|pb| pb.into_storeid().map_err_trace_exit_unwrap(1))
- .unwrap(); // safed by clap
-
- let _ = rt.store()
- .get(entry_name)
- .map_err_trace_exit_unwrap(1)
- .ok_or_else(|| Error::from(err_msg("Entry does not exist".to_owned())))
- .map_err_trace_exit_unwrap(1)
- .annotate(rt.store(), annotation_name)
- .map_err_trace_exit_unwrap(1)
- .edit_content(&rt)
- .map_err_trace_exit_unwrap(1);
+ let ids = rt.ids::<::ui::PathProvider>().map_err_trace_exit_unwrap(1);
+
+ ids.into_iter().for_each(|id| {
+ let _ = rt.store()
+ .get(id.clone())
+ .map_err_trace_exit_unwrap(1)
+ .ok_or_else(|| EM::EntryNotFound(id.local_display_string()))
+ .map_err(Error::from)
+ .map_err_trace_exit_unwrap(1)
+ .annotate(rt.store(), annotation_name)
+ .map_err_trace_exit_unwrap(1)
+ .edit_content(&rt)
+ .map_err_trace_exit_unwrap(1);
+ })
+
}
fn remove(rt: &Runtime) {
let scmd = rt.cli().subcommand_matches("remove").unwrap(); // safed by main()
- let entry_name = scmd.value_of("entry").unwrap(); // safed by clap
let annotation_name = scmd.value_of("annotation_name").unwrap(); // safed by clap
let delete = scmd.is_present("delete-annotation");
-
- let mut entry = rt.store()
- .get(PathBuf::from(entry_name).into_storeid().map_err_trace_exit_unwrap(1))
- .map_err_trace_exit_unwrap(1)
- .ok_or_else(|| Error::from(err_msg("Entry does not exist".to_owned())))
- .map_err_trace_exit_unwrap(1);
-
- let annotation = entry
- .denotate(rt.store(), annotation_name)
- .map_err_trace_exit_unwrap(1);
-
- if delete {
- debug!("Deleting annotation object");
- if let Some(an) = annotation {
- let loc = an.get_location().clone();
- drop(an);
-
- let _ = rt
- .store()
- .delete(loc)
- .map_err_trace_exit_unwrap(1);
+ let ids = rt.ids::<::ui::PathProvider>().map_err_trace_exit_unwrap(1);
+
+ ids.into_iter().for_each(|id| {
+ let mut entry = rt.store()
+ .get(id.clone())
+ .map_err_trace_exit_unwrap(1)
+ .ok_or_else(|| EM::EntryNotFound(id.local_display_string()))
+ .map_err(Error::from)
+ .map_err_trace_exit_unwrap(1);
+
+ let annotation = entry
+ .denotate(rt.store(), annotation_name)
+ .map_err_trace_exit_unwrap(1);
+
+ if delete {
+ debug!("Deleting annotation object");
+ if let Some(an) = annotation {
+ let loc = an.get_location().clone();
+ drop(an);
+
+ let _ = rt
+ .store()
+ .delete(loc)
+ .map_err_trace_exit_unwrap(1);
+ } else {
+ warn!("Not having annotation object, cannot delete!");
+ }
} else {
- warn!("Not having annotation object, cannot delete!");
+ debug!("Not deleting annotation object");
}
- } else {
- debug!("Not deleting annotation object");
- }
+ })
+
}
fn list(rt: &Runtime) {
- let scmd = rt.cli().subcommand_matches("list").unwrap(); // safed by clap
- let with_text = scmd.is_present("list-with-text");
- match scmd.value_of("entry").map(PathBuf::from) {
- Some(pb) => {
- let _ = rt
- .store()
- .get(pb.into_storeid().map_err_trace_exit_unwrap(1))
- .map_err_trace_exit_unwrap(1)
- .ok_or_else(|| Error::from(err_msg("Entry does not exist")))
- .map_err_trace_exit_unwrap(1)
- .annotations(rt.store())
- .map_err_trace_exit_unwrap(1)
- .enumerate()
- .map(|(i, a)| {
- list_annotation(&rt, i, a.map_err_trace_exit_unwrap(1), with_text)
- })
- .collect::<Vec<_>>();
- }
-
- None => {
- // show them all
- let _ = rt
- .store()
- .all_annotations()
- .map_err_trace_exit_unwrap(1)
- .enumerate()
- .map(|(i, a)| {
- list_annotation(&rt, i, a.map_err_trace_exit_unwrap(1), with_text)
- })
- .collect::<Vec<_>>();
- }
+ let scmd = rt.cli().subcommand_matches("list").unwrap(); // safed by clap
+ let with_text = scmd.is_present("list-with-text");
+ let ids = rt.ids::<::ui::PathProvider>().map_err_trace_exit_unwrap(1);
+
+ if ids.len() != 0 {
+ let _ = ids
+ .into_iter()
+ .for_each(|id| {
+ let _ = rt
+ .store()
+ .get(id.clone())
+ .map_err_trace_exit_unwrap(1)
+ .ok_or_else(|| EM::EntryNotFound(id.local_display_string()))
+ .map_err(Error::from)
+ .map_err_trace_exit_unwrap(1)
+ .annotations(rt.store())
+ .map_err_trace_exit_unwrap(1)
+ .enumerate()
+ .map(|(i, a)| {
+ list_annotation(&rt, i, a.map_err_trace_exit_unwrap(1), with_text)
+ })
+ .collect::<Vec<_>>();
+ });
+ } else { // ids.len() == 0
+ // show them all
+ let _ = rt
+ .store()
+ .all_annotations()
+ .map_err_trace_exit_unwrap(1)
+ .enumerate()
+ .map(|(i, a)| {
+ list_annotation(&rt, i, a.map_err_trace_exit_unwrap(1), with_text)
+ })
+ .collect::<Vec<_>>();
}
}
diff --git a/bin/core/imag-annotate/src/ui.rs b/bin/core/imag-annotate/src/ui.rs
index 544cd2a4..1cf119e6 100644
--- a/bin/core/imag-annotate/src/ui.rs
+++ b/bin/core/imag-annotate/src/ui.rs
@@ -17,7 +17,14 @@
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
//
-use clap::{Arg, App, SubCommand};
+use std::path::PathBuf;
+
+use clap::{Arg, ArgMatches, App, SubCommand};
+
+use libimagstore::storeid::StoreId;
+use libimagstore::storeid::IntoStoreId;
+use libimagrt::runtime::IdPathProvider;
+use libimagerror::trace::MapErrTrace;
pub fn build_ui<'a>(app: App<'a, 'a>) -> App<'a, 'a> {
app
@@ -86,3 +93,56 @@ pub fn build_ui<'a>(app: App<'a, 'a>) -> App<'a, 'a> {
)
}
+pub struct PathProvider;
+impl IdPathProvider for PathProvider {
+ fn get_ids(matches: &ArgMatches) -> Vec<StoreId> {
+ match matches.subcommand() {
+ ("add", Some(subm)) => {
+ subm.values_of("entry")
+ .ok_or_else(|| {
+ error!("No StoreId found");
+ ::std::process::exit(1)
+ })
+ .unwrap()
+ .into_iter()
+ .map(PathBuf::from)
+ .map(|pb| pb.into_storeid())
+ .collect::<Result<Vec<_>, _>>()
+ .map_err_trace_exit_unwrap(1)
+ },
+
+ ("remove", Some(subm)) => {
+ subm.values_of("entry")
+ .ok_or_else(|| {
+ error!("No StoreId found");
+ ::std::process::exit(1)
+ })
+ .unwrap()
+ .into_iter()
+ .map(PathBuf::from)
+ .map(|pb| pb.into_storeid())
+ .collect::<Result<Vec<_>, _>>()
+ .map_err_trace_exit_unwrap(1)
+ },
+
+ ("list", Some(subm)) => {
+ subm.values_of("entry")
+ .ok_or_else(|| {
+ error!("No StoreId found");
+ ::std::process::exit(1)
+ })
+ .unwrap()
+ .into_iter()
+ .map(PathBuf::from)
+ .map(|pb| pb.into_storeid())
+ .collect::<Result<Vec<_>, _>>()
+ .map_err_trace_exit_unwrap(1)
+ },
+
+ (other, _) => {
+ error!("Not a known command: {}", other);
+ ::std::process::exit(1)
+ }
+ }
+ }
+}