Rewrite "add annotation" command

Because before we created a new annotation for each ID to be annotated,
which is not the expected behaviour.

Now we create one annotation object and then link it to all IDs which
are provided on the commandline.

Also, the annotation name is printed.

Signed-off-by: Matthias Beyer <mail@beyermatthias.de>
This commit is contained in:
Matthias Beyer 2019-02-05 20:08:08 +01:00
parent ed01f8b463
commit ab5078f111
3 changed files with 45 additions and 22 deletions

View File

@ -32,6 +32,7 @@ libimagstore = { version = "0.10.0", path = "../../../lib/core/libimag
libimagrt = { version = "0.10.0", path = "../../../lib/core/libimagrt" } libimagrt = { version = "0.10.0", path = "../../../lib/core/libimagrt" }
libimagerror = { version = "0.10.0", path = "../../../lib/core/libimagerror" } libimagerror = { version = "0.10.0", path = "../../../lib/core/libimagerror" }
libimagentryannotation = { version = "0.10.0", path = "../../../lib/entry/libimagentryannotation" } libimagentryannotation = { version = "0.10.0", path = "../../../lib/entry/libimagentryannotation" }
libimagentrylink = { version = "0.10.0", path = "../../../lib/entry/libimagentrylink" }
libimagentryedit = { version = "0.10.0", path = "../../../lib/entry/libimagentryedit" } libimagentryedit = { version = "0.10.0", path = "../../../lib/entry/libimagentryedit" }
libimagutil = { version = "0.10.0", path = "../../../lib/etc/libimagutil" } libimagutil = { version = "0.10.0", path = "../../../lib/etc/libimagutil" }

View File

@ -37,7 +37,9 @@
extern crate clap; extern crate clap;
#[macro_use] #[macro_use]
extern crate log; extern crate log;
#[macro_use]
extern crate failure; extern crate failure;
extern crate toml_query;
extern crate libimagentryannotation; extern crate libimagentryannotation;
extern crate libimagentryedit; extern crate libimagentryedit;
@ -45,6 +47,7 @@ extern crate libimagerror;
#[macro_use] extern crate libimagrt; #[macro_use] extern crate libimagrt;
extern crate libimagstore; extern crate libimagstore;
extern crate libimagutil; extern crate libimagutil;
extern crate libimagentrylink;
use std::io::Write; use std::io::Write;
@ -57,9 +60,12 @@ 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 libimagerror::errors::ErrorMsg as EM;
use libimagerror::iter::TraceIterator;
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::iter::get::StoreIdGetIteratorExtension;
use libimagentrylink::internal::InternalLinker;
mod ui; mod ui;
@ -90,22 +96,44 @@ 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 mut ids = rt.ids::<::ui::PathProvider>().map_err_trace_exit_unwrap(1).into_iter();
let ids = rt.ids::<::ui::PathProvider>().map_err_trace_exit_unwrap(1);
ids.into_iter().for_each(|id| { if let Some(first) = ids.next() {
let _ = rt.store() let mut annotation = rt.store()
.get(id.clone()) .get(first.clone())
.map_err_trace_exit_unwrap(1) .map_err_trace_exit_unwrap(1)
.ok_or_else(|| EM::EntryNotFound(id.local_display_string())) .ok_or_else(|| EM::EntryNotFound(first.local_display_string()))
.map_err(Error::from) .map_err(Error::from)
.map_err_trace_exit_unwrap(1) .map_err_trace_exit_unwrap(1)
.annotate(rt.store(), annotation_name) .annotate(rt.store())
.map_err_trace_exit_unwrap(1)
.edit_content(&rt)
.map_err_trace_exit_unwrap(1); .map_err_trace_exit_unwrap(1);
})
let _ = annotation.edit_content(&rt).map_err_trace_exit_unwrap(1);
for id in ids {
let mut entry = rt.store().get(id.clone())
.map_err_trace_exit_unwrap(1)
.ok_or_else(|| format_err!("Not found: {}", id.local_display_string()))
.map_err_trace_exit_unwrap(1);
let _ = entry.add_internal_link(&mut annotation).map_err_trace_exit_unwrap(1);
}
if let Some(annotation_id) = annotation
.get_header()
.read_string("annotation.name")
.map_err_trace_exit_unwrap(1)
{
let _ = writeln!(rt.stdout(), "Name of the annotation: {}", annotation_id)
.to_exit_code()
.unwrap_or_exit(1);
} else {
error!("Unnamed annotation: {:?}", annotation.get_location());
error!("This is most likely a BUG, please report!");
}
} else {
debug!("No entries to annotate");
}
} }
fn remove(rt: &Runtime) { fn remove(rt: &Runtime) {
@ -164,11 +192,12 @@ fn list(rt: &Runtime) {
.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)
.into_get_iter(rt.store())
.trace_unwrap_exit(1)
.map(|opt| opt.ok_or_else(|| format_err!("Cannot find entry")))
.trace_unwrap_exit(1)
.enumerate() .enumerate()
.map(|(i, a)| { .for_each(|(i, entry)| list_annotation(&rt, i, entry, with_text));
list_annotation(&rt, i, a.map_err_trace_exit_unwrap(1), with_text)
})
.collect::<Vec<_>>();
}); });
} else { // ids.len() == 0 } else { // ids.len() == 0
// show them all // show them all

View File

@ -38,13 +38,6 @@ pub fn build_ui<'a>(app: App<'a, 'a>) -> App<'a, 'a> {
.multiple(false) .multiple(false)
.help("The entry to add the latitude/longitude to") .help("The entry to add the latitude/longitude to")
.value_name("ENTRY")) .value_name("ENTRY"))
.arg(Arg::with_name("annotation_name")
.index(2)
.takes_value(true)
.required(true)
.multiple(false)
.help("Name of the new annotation")
.value_name("NAME"))
) )
.subcommand(SubCommand::with_name("remove") .subcommand(SubCommand::with_name("remove")