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,33 +89,35 @@ 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)
.map(|pb| pb.into_storeid().map_err_trace_exit_unwrap(1))
.unwrap(); // safed by clap
ids.into_iter().for_each(|id| {
let _ = rt.store() let _ = rt.store()
.get(entry_name) .get(id.clone())
.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()))) .ok_or_else(|| EM::EntryNotFound(id.local_display_string()))
.map_err(Error::from)
.map_err_trace_exit_unwrap(1) .map_err_trace_exit_unwrap(1)
.annotate(rt.store(), annotation_name) .annotate(rt.store(), annotation_name)
.map_err_trace_exit_unwrap(1) .map_err_trace_exit_unwrap(1)
.edit_content(&rt) .edit_content(&rt)
.map_err_trace_exit_unwrap(1); .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);
ids.into_iter().for_each(|id| {
let mut entry = rt.store() 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) .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); .map_err_trace_exit_unwrap(1);
let annotation = entry let annotation = entry
@ -140,18 +140,25 @@ fn remove(rt: &Runtime) {
} else { } else {
debug!("Not deleting annotation object"); 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) => {
if ids.len() != 0 {
let _ = ids
.into_iter()
.for_each(|id| {
let _ = rt let _ = rt
.store() .store()
.get(pb.into_storeid().map_err_trace_exit_unwrap(1)) .get(id.clone())
.map_err_trace_exit_unwrap(1) .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) .map_err_trace_exit_unwrap(1)
.annotations(rt.store()) .annotations(rt.store())
.map_err_trace_exit_unwrap(1) .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) list_annotation(&rt, i, a.map_err_trace_exit_unwrap(1), with_text)
}) })
.collect::<Vec<_>>(); .collect::<Vec<_>>();
} });
} else { // ids.len() == 0
None => {
// show them all // show them all
let _ = rt let _ = rt
.store() .store()
@ -174,7 +180,6 @@ fn list(rt: &Runtime) {
}) })
.collect::<Vec<_>>(); .collect::<Vec<_>>();
} }
}
} }
fn list_annotation<'a>(rt: &Runtime, i: usize, a: FileLockEntry<'a>, with_text: bool) { fn list_annotation<'a>(rt: &Runtime, i: usize, a: FileLockEntry<'a>, with_text: bool) {

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