summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMatthias Beyer <mail@beyermatthias.de>2017-10-13 16:20:57 +0200
committerGitHub <noreply@github.com>2017-10-13 16:20:57 +0200
commit704c01b2c9f44ea2cb158e44c610d4af1ee8a9fd (patch)
treed41c463901a2b647e0fca56d26e80389807d1ffb
parent935701955133f7b42f30ded96b04d6bcbb2b4ab7 (diff)
parent60f2a340745a936d1f3229cc3cb1b1bfbeb5f31b (diff)
downloadimag-704c01b2c9f44ea2cb158e44c610d4af1ee8a9fd.zip
imag-704c01b2c9f44ea2cb158e44c610d4af1ee8a9fd.tar.gz
Merge pull request #1130 from matthiasbeyer/imag-annotate/init
Imag annotate/init
-rw-r--r--Cargo.toml1
-rw-r--r--bin/core/imag-annotate/Cargo.toml30
-rw-r--r--bin/core/imag-annotate/src/main.rs194
-rw-r--r--bin/core/imag-annotate/src/ui.rs88
-rw-r--r--doc/src/09020-changelog.md1
-rw-r--r--lib/entry/libimagentryannotation/src/annotateable.rs1
6 files changed, 315 insertions, 0 deletions
diff --git a/Cargo.toml b/Cargo.toml
index 459f9f9..3ec4b5b 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -1,6 +1,7 @@
[workspace]
members = [
"bin/core/imag",
+ "bin/core/imag-annotate",
"bin/core/imag-gps",
"bin/core/imag-grep",
"bin/core/imag-link",
diff --git a/bin/core/imag-annotate/Cargo.toml b/bin/core/imag-annotate/Cargo.toml
new file mode 100644
index 0000000..68dc29c
--- /dev/null
+++ b/bin/core/imag-annotate/Cargo.toml
@@ -0,0 +1,30 @@
+[package]
+name = "imag-annotate"
+version = "0.5.0"
+authors = ["Matthias Beyer <mail@beyermatthias.de>"]
+
+description = "Part of the imag core distribution: imag-annotate command"
+
+keywords = ["imag", "PIM", "personal", "information", "management"]
+readme = "../../../README.md"
+license = "LGPL-2.1"
+
+documentation = "https://matthiasbeyer.github.io/imag/imag_documentation/index.html"
+repository = "https://github.com/matthiasbeyer/imag"
+homepage = "http://imag-pim.org"
+
+[dependencies]
+clap = ">=2.17"
+log = "0.3"
+version = "2.0.1"
+url = "1.2"
+toml = "0.4"
+toml-query = "0.3.0"
+
+libimagstore = { version = "0.5.0", path = "../../../lib/core/libimagstore" }
+libimagrt = { version = "0.5.0", path = "../../../lib/core/libimagrt" }
+libimagerror = { version = "0.5.0", path = "../../../lib/core/libimagerror" }
+libimagentryannotation = { version = "0.5.0", path = "../../../lib/entry/libimagentryannotation" }
+libimagentryedit = { version = "0.5.0", path = "../../../lib/entry/libimagentryedit" }
+libimagutil = { version = "0.5.0", path = "../../../lib/etc/libimagutil" }
+
diff --git a/bin/core/imag-annotate/src/main.rs b/bin/core/imag-annotate/src/main.rs
new file mode 100644
index 0000000..dd2c68e
--- /dev/null
+++ b/bin/core/imag-annotate/src/main.rs
@@ -0,0 +1,194 @@
+//
+// imag - the personal information management suite for the commandline
+// Copyright (C) 2015, 2016 Matthias Beyer <mail@beyermatthias.de> and contributors
+//
+// This library is free software; you can redistribute it and/or
+// modify it under the terms of the GNU Lesser General Public
+// License as published by the Free Software Foundation; version
+// 2.1 of the License.
+//
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+// Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public
+// License along with this library; if not, write to the Free Software
+// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+//
+
+#![deny(
+ non_camel_case_types,
+ non_snake_case,
+ path_statements,
+ trivial_numeric_casts,
+ unstable_features,
+ unused_allocation,
+ unused_import_braces,
+ unused_imports,
+ unused_must_use,
+ unused_mut,
+ unused_qualifications,
+ while_true,
+)]
+
+extern crate clap;
+#[macro_use]
+extern crate log;
+#[macro_use]
+extern crate version;
+
+extern crate libimagentryannotation;
+extern crate libimagentryedit;
+extern crate libimagerror;
+extern crate libimagrt;
+extern crate libimagstore;
+extern crate libimagutil;
+
+use std::path::PathBuf;
+
+use libimagentryannotation::annotateable::*;
+use libimagentryannotation::annotation_fetcher::*;
+use libimagentryannotation::error::AnnotationError as AE;
+use libimagentryedit::edit::*;
+use libimagerror::trace::MapErrTrace;
+use libimagrt::runtime::Runtime;
+use libimagrt::setup::generate_runtime_setup;
+use libimagstore::store::FileLockEntry;
+use libimagstore::storeid::IntoStoreId;
+use libimagutil::warn_exit::warn_exit;
+
+mod ui;
+
+fn main() {
+ let rt = generate_runtime_setup("imag-annotation",
+ &version!()[..],
+ "Add annotations to entries",
+ ui::build_ui);
+
+ rt.cli()
+ .subcommand_name()
+ .map(|name| {
+ match name {
+ "add" => add(&rt),
+ "remove" => remove(&rt),
+ "list" => list(&rt),
+ _ => warn_exit("No commandline call", 1)
+ }
+ });
+}
+
+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(1).unwrap())
+ .unwrap(); // safed by clap
+
+ let _ = rt.store()
+ .get(entry_name)
+ .map_err_trace_exit(1)
+ .unwrap()
+ .ok_or(AE::from("Entry does not exist".to_owned()))
+ .map_err_trace_exit(1)
+ .unwrap()
+ .annotate(rt.store(), annotation_name)
+ .map_err_trace_exit(1)
+ .unwrap()
+ .edit_content(&rt)
+ .map_err_trace_exit(1)
+ .unwrap();
+
+ info!("Ok");
+}
+
+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(1).unwrap())
+ .map_err_trace_exit(1)
+ .unwrap()
+ .ok_or(AE::from("Entry does not exist".to_owned()))
+ .map_err_trace_exit(1)
+ .unwrap();
+
+ let annotation = entry
+ .denotate(rt.store(), annotation_name)
+ .map_err_trace_exit(1)
+ .unwrap();
+
+ 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(1)
+ .unwrap();
+ } else {
+ warn!("Not having annotation object, cannot delete!");
+ }
+ } else {
+ debug!("Not deleting annotation object");
+ }
+
+ info!("Ok");
+}
+
+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(1).unwrap())
+ .map_err_trace_exit(1)
+ .unwrap()
+ .ok_or(AE::from("Entry does not exist".to_owned()))
+ .map_err_trace_exit(1)
+ .unwrap()
+ .annotations(rt.store())
+ .map_err_trace_exit(1)
+ .unwrap()
+ .enumerate()
+ .map(|(i, a)| list_annotation(i, a.map_err_trace_exit(1).unwrap(), with_text))
+ .collect::<Vec<_>>();
+ }
+
+ None => {
+ // show them all
+ let _ = rt
+ .store()
+ .all_annotations()
+ .map_err_trace_exit(1)
+ .unwrap()
+ .enumerate()
+ .map(|(i, a)| list_annotation(i, a.map_err_trace_exit(1).unwrap(), with_text))
+ .collect::<Vec<_>>();
+ }
+ }
+
+ info!("Ok");
+}
+
+fn list_annotation<'a>(i: usize, a: FileLockEntry<'a>, with_text: bool) {
+ if with_text {
+ println!("--- {i: >5} | {id}\n{text}\n\n",
+ i = i,
+ id = a.get_location(),
+ text = a.get_content());
+ } else {
+ println!("{: >5} | {}", i, a.get_location());
+ }
+}
+
diff --git a/bin/core/imag-annotate/src/ui.rs b/bin/core/imag-annotate/src/ui.rs
new file mode 100644
index 0000000..fd7d62f
--- /dev/null
+++ b/bin/core/imag-annotate/src/ui.rs
@@ -0,0 +1,88 @@
+//
+// imag - the personal information management suite for the commandline
+// Copyright (C) 2015, 2016 Matthias Beyer <mail@beyermatthias.de> and contributors
+//
+// This library is free software; you can redistribute it and/or
+// modify it under the terms of the GNU Lesser General Public
+// License as published by the Free Software Foundation; version
+// 2.1 of the License.
+//
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+// Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public
+// License along with this library; if not, write to the Free Software
+// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+//
+
+use clap::{Arg, App, SubCommand};
+
+pub fn build_ui<'a>(app: App<'a, 'a>) -> App<'a, 'a> {
+ app
+ .subcommand(SubCommand::with_name("add")
+ .about("Add annotation to an entry")
+ .version("0.1")
+ .arg(Arg::with_name("entry")
+ .index(1)
+ .takes_value(true)
+ .required(true)
+ .multiple(false)
+ .help("The entry to add the latitude/longitude to")
+ .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")
+ .about("Remove annotation from an entry")
+ .version("0.1")
+ .arg(Arg::with_name("entry")
+ .index(1)
+ .takes_value(true)
+ .required(true)
+ .multiple(false)
+ .help("The entry to remove the latitude/longitude from")
+ .value_name("ENTRY"))
+ .arg(Arg::with_name("annotation_name")
+ .index(2)
+ .takes_value(true)
+ .required(true)
+ .multiple(false)
+ .help("Name of the annotation to remove")
+ .value_name("NAME"))
+ .arg(Arg::with_name("delete-annotation")
+ .short("D")
+ .long("delete")
+ .takes_value(false)
+ .required(false)
+ .multiple(false)
+ .help("Do not only 'unlink' the annotation, but also delete it from the store"))
+ )
+
+ .subcommand(SubCommand::with_name("list")
+ .about("List annotations")
+ .version("0.1")
+ .arg(Arg::with_name("entry")
+ .index(1)
+ .takes_value(true)
+ .required(false)
+ .multiple(false)
+ .help("The entry to list annotations for (all annotations if not passed)")
+ .value_name("ENTRY"))
+ .arg(Arg::with_name("list-with-text")
+ .long("text")
+ .short("t")
+ .takes_value(false)
+ .required(false)
+ .multiple(false)
+ .help("List annotations with text"))
+ )
+}
+
diff --git a/doc/src/09020-changelog.md b/doc/src/09020-changelog.md
index 85cdd77..9129b57 100644
--- a/doc/src/09020-changelog.md
+++ b/doc/src/09020-changelog.md
@@ -28,6 +28,7 @@ This section contains the changelog from the last release to the next release.
* The logger is now able to handle multiple destinations (file and "-" for
stderr)
* `imag-store` can dump all storeids now
+ * `imag-annotate` was introduced
* Minor changes
* `libimagentryannotation` got a rewrite, is not based on `libimagnotes`
diff --git a/lib/entry/libimagentryannotation/src/annotateable.rs b/lib/entry/libimagentryannotation/src/annotateable.rs
index bc2aaa4..356e66c 100644
--- a/lib/entry/libimagentryannotation/src/annotateable.rs
+++ b/lib/entry/libimagentryannotation/src/annotateable.rs
@@ -80,6 +80,7 @@ impl Annotateable for Entry {
if name == ann_name {
let _ = try!(self.remove_internal_link(&mut anno));
+ return Ok(Some(anno));
}
}