Move imag-annotate to ID provider infrastructure

This commit is contained in:
Matthias Beyer 2018-09-29 17:44:36 +02:00
parent c1c74973e3
commit 01de94a387
2 changed files with 135 additions and 70 deletions

View file

@ -45,10 +45,8 @@ extern crate libimagstore;
extern crate libimagutil; extern crate libimagutil;
use std::io::Write; use std::io::Write;
use std::path::PathBuf;
use failure::Error; use failure::Error;
use failure::err_msg;
use libimagentryannotation::annotateable::*; use libimagentryannotation::annotateable::*;
use libimagentryannotation::annotation_fetcher::*; use libimagentryannotation::annotation_fetcher::*;
@ -56,10 +54,10 @@ use libimagentryedit::edit::*;
use libimagerror::trace::MapErrTrace; use libimagerror::trace::MapErrTrace;
use libimagerror::exit::ExitUnwrap; use libimagerror::exit::ExitUnwrap;
use libimagerror::io::ToExitCode; use libimagerror::io::ToExitCode;
use libimagerror::errors::ErrorMsg as EM;
use libimagrt::runtime::Runtime; use libimagrt::runtime::Runtime;
use libimagrt::setup::generate_runtime_setup; use libimagrt::setup::generate_runtime_setup;
use libimagstore::store::FileLockEntry; use libimagstore::store::FileLockEntry;
use libimagstore::storeid::IntoStoreId;
mod ui; mod ui;
@ -91,89 +89,96 @@ fn main() {
fn add(rt: &Runtime) { fn add(rt: &Runtime) {
let scmd = rt.cli().subcommand_matches("add").unwrap(); // safed by main() let scmd = rt.cli().subcommand_matches("add").unwrap(); // safed by main()
let annotation_name = scmd.value_of("annotation_name").unwrap(); // safed by clap let annotation_name = scmd.value_of("annotation_name").unwrap(); // safed by clap
let entry_name = scmd let ids = rt.ids::<::ui::PathProvider>().map_err_trace_exit_unwrap(1);
.value_of("entry")
.map(PathBuf::from) ids.into_iter().for_each(|id| {
.map(|pb| pb.into_storeid().map_err_trace_exit_unwrap(1)) let _ = rt.store()
.unwrap(); // safed by clap .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);
})
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);
} }
fn remove(rt: &Runtime) { fn remove(rt: &Runtime) {
let scmd = rt.cli().subcommand_matches("remove").unwrap(); // safed by main() 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 annotation_name = scmd.value_of("annotation_name").unwrap(); // safed by clap
let delete = scmd.is_present("delete-annotation"); let delete = scmd.is_present("delete-annotation");
let ids = rt.ids::<::ui::PathProvider>().map_err_trace_exit_unwrap(1);
let mut entry = rt.store() ids.into_iter().for_each(|id| {
.get(PathBuf::from(entry_name).into_storeid().map_err_trace_exit_unwrap(1)) let mut entry = rt.store()
.map_err_trace_exit_unwrap(1) .get(id.clone())
.ok_or_else(|| Error::from(err_msg("Entry does not exist".to_owned()))) .map_err_trace_exit_unwrap(1)
.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 let annotation = entry
.denotate(rt.store(), annotation_name) .denotate(rt.store(), annotation_name)
.map_err_trace_exit_unwrap(1); .map_err_trace_exit_unwrap(1);
if delete { if delete {
debug!("Deleting annotation object"); debug!("Deleting annotation object");
if let Some(an) = annotation { if let Some(an) = annotation {
let loc = an.get_location().clone(); let loc = an.get_location().clone();
drop(an); drop(an);
let _ = rt let _ = rt
.store() .store()
.delete(loc) .delete(loc)
.map_err_trace_exit_unwrap(1); .map_err_trace_exit_unwrap(1);
} else {
warn!("Not having annotation object, cannot delete!");
}
} else { } else {
warn!("Not having annotation object, cannot delete!"); debug!("Not deleting annotation object");
} }
} else { })
debug!("Not deleting annotation object");
}
} }
fn list(rt: &Runtime) { fn list(rt: &Runtime) {
let scmd = rt.cli().subcommand_matches("list").unwrap(); // safed by clap let scmd = rt.cli().subcommand_matches("list").unwrap(); // safed by clap
let with_text = scmd.is_present("list-with-text"); let with_text = scmd.is_present("list-with-text");
match scmd.value_of("entry").map(PathBuf::from) { let ids = rt.ids::<::ui::PathProvider>().map_err_trace_exit_unwrap(1);
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 => { if ids.len() != 0 {
// show them all let _ = ids
let _ = rt .into_iter()
.store() .for_each(|id| {
.all_annotations() let _ = rt
.map_err_trace_exit_unwrap(1) .store()
.enumerate() .get(id.clone())
.map(|(i, a)| { .map_err_trace_exit_unwrap(1)
list_annotation(&rt, i, a.map_err_trace_exit_unwrap(1), with_text) .ok_or_else(|| EM::EntryNotFound(id.local_display_string()))
}) .map_err(Error::from)
.collect::<Vec<_>>(); .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<_>>();
} }
} }

View file

@ -17,7 +17,14 @@
// 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 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> { pub fn build_ui<'a>(app: App<'a, 'a>) -> App<'a, 'a> {
app 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)
}
}
}
}