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;
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,33 +89,35 @@ 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 ids = rt.ids::<::ui::PathProvider>().map_err_trace_exit_unwrap(1);
ids.into_iter().for_each(|id| {
let _ = rt.store()
.get(entry_name)
.get(id.clone())
.map_err_trace_exit_unwrap(1)
.ok_or_else(|| Error::from(err_msg("Entry does not exist".to_owned())))
.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 ids = rt.ids::<::ui::PathProvider>().map_err_trace_exit_unwrap(1);
ids.into_iter().for_each(|id| {
let mut entry = rt.store()
.get(PathBuf::from(entry_name).into_storeid().map_err_trace_exit_unwrap(1))
.get(id.clone())
.map_err_trace_exit_unwrap(1)
.ok_or_else(|| Error::from(err_msg("Entry does not exist".to_owned())))
.ok_or_else(|| EM::EntryNotFound(id.local_display_string()))
.map_err(Error::from)
.map_err_trace_exit_unwrap(1);
let annotation = entry
@ -140,18 +140,25 @@ fn remove(rt: &Runtime) {
} 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 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(pb.into_storeid().map_err_trace_exit_unwrap(1))
.get(id.clone())
.map_err_trace_exit_unwrap(1)
.ok_or_else(|| Error::from(err_msg("Entry does not exist")))
.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)
@ -160,9 +167,8 @@ fn list(rt: &Runtime) {
list_annotation(&rt, i, a.map_err_trace_exit_unwrap(1), with_text)
})
.collect::<Vec<_>>();
}
None => {
});
} else { // ids.len() == 0
// show them all
let _ = rt
.store()
@ -175,7 +181,6 @@ fn list(rt: &Runtime) {
.collect::<Vec<_>>();
}
}
}
fn list_annotation<'a>(rt: &Runtime, i: usize, a: FileLockEntry<'a>, with_text: bool) {
let _ = if with_text {

View File

@ -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)
}
}
}
}