summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLeon Schuermann <leon@is.currently.online>2019-09-14 18:44:20 +0200
committerMatthias Beyer <mail@beyermatthias.de>2019-10-26 14:41:31 +0200
commitaa851a87f5fb8ddf98edd98561980a562319b05e (patch)
treeec9184baddf4a31b4c7e1ca72f7a13b0bc13f18d
parent208d6e62e60f4d80658629c2c13280c4bdab448c (diff)
imag-link: implement ImagApplication
Signed-off-by: Leon Schuermann <leon@is.currently.online>
-rw-r--r--bin/core/imag-link/Cargo.toml6
-rw-r--r--bin/core/imag-link/src/bin.rs39
-rw-r--r--bin/core/imag-link/src/lib.rs (renamed from bin/core/imag-link/src/main.rs)114
3 files changed, 111 insertions, 48 deletions
diff --git a/bin/core/imag-link/Cargo.toml b/bin/core/imag-link/Cargo.toml
index 5b98906e..50ef9d95 100644
--- a/bin/core/imag-link/Cargo.toml
+++ b/bin/core/imag-link/Cargo.toml
@@ -54,4 +54,10 @@ path = "../../../lib/core/libimagrt"
default-features = false
features = ["testing"]
+[lib]
+name = "libimaglinkcmd"
+path = "src/lib.rs"
+[[bin]]
+name = "imag-link"
+path = "src/bin.rs"
diff --git a/bin/core/imag-link/src/bin.rs b/bin/core/imag-link/src/bin.rs
new file mode 100644
index 00000000..1bcebf1d
--- /dev/null
+++ b/bin/core/imag-link/src/bin.rs
@@ -0,0 +1,39 @@
+//
+// imag - the personal information management suite for the commandline
+// Copyright (C) 2015-2019 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
+//
+
+#![forbid(unsafe_code)]
+
+#![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,
+)]
+
+#[macro_use] extern crate libimagrt;
+
+simple_imag_application_binary!(libimaglinkcmd, ImagLink);
diff --git a/bin/core/imag-link/src/main.rs b/bin/core/imag-link/src/lib.rs
index 7e451a83..bfa927dd 100644
--- a/bin/core/imag-link/src/main.rs
+++ b/bin/core/imag-link/src/lib.rs
@@ -45,7 +45,7 @@ extern crate failure;
extern crate libimagentrylink;
extern crate libimagentryurl;
-#[macro_use] extern crate libimagrt;
+extern crate libimagrt;
extern crate libimagstore;
extern crate libimagerror;
@@ -69,7 +69,7 @@ use libimagerror::trace::{MapErrTrace, trace_error};
use libimagerror::exit::ExitUnwrap;
use libimagerror::io::ToExitCode;
use libimagrt::runtime::Runtime;
-use libimagrt::setup::generate_runtime_setup;
+use libimagrt::application::ImagApplication;
use libimagstore::store::FileLockEntry;
use libimagstore::storeid::StoreId;
use libimagutil::warn_exit::warn_exit;
@@ -77,57 +77,75 @@ use libimagutil::warn_result::*;
use url::Url;
use failure::Fallible as Result;
+use clap::App;
mod ui;
-use crate::ui::build_ui;
-
-fn main() {
- let version = make_imag_version!();
- let rt = generate_runtime_setup("imag-link",
- &version,
- "Link entries",
- build_ui);
- if rt.cli().is_present("check-consistency") {
- let exit_code = match rt.store().check_link_consistency() {
- Ok(_) => {
- info!("Store is consistent");
- 0
- }
- Err(e) => {
- trace_error(&e);
- 1
- }
- };
- ::std::process::exit(exit_code);
+/// Marker enum for implementing ImagApplication on
+///
+/// This is used by binaries crates to execute business logic
+/// or to build a CLI completion.
+pub enum ImagLink {}
+impl ImagApplication for ImagLink {
+ fn run(rt: Runtime) -> Result<()> {
+ if rt.cli().is_present("check-consistency") {
+ let exit_code = match rt.store().check_link_consistency() {
+ Ok(_) => {
+ info!("Store is consistent");
+ 0
+ }
+ Err(e) => {
+ trace_error(&e);
+ 1
+ }
+ };
+ ::std::process::exit(exit_code);
+ }
+
+ let _ = rt.cli()
+ .subcommand_name()
+ .map(|name| {
+ match name {
+ "remove" => remove_linking(&rt),
+ "unlink" => unlink(&rt),
+ "list" => list_linkings(&rt),
+ other => {
+ debug!("Unknown command");
+ let _ = rt.handle_unknown_subcommand("imag-link", other, rt.cli())
+ .map_err_trace_exit_unwrap()
+ .code()
+ .map(::std::process::exit);
+ },
+ }
+ })
+ .or_else(|| {
+ if let (Some(from), Some(to)) = (rt.cli().value_of("from"), rt.cli().values_of("to")) {
+ Some(link_from_to(&rt, from, to))
+ } else {
+ warn_exit("No commandline call", 1)
+ }
+ })
+ .ok_or_else(|| err_msg("No commandline call".to_owned()))
+ .map_err_trace_exit_unwrap();
+
+ Ok(())
}
- rt.cli()
- .subcommand_name()
- .map(|name| {
- match name {
- "remove" => remove_linking(&rt),
- "unlink" => unlink(&rt),
- "list" => list_linkings(&rt),
- other => {
- debug!("Unknown command");
- let _ = rt.handle_unknown_subcommand("imag-link", other, rt.cli())
- .map_err_trace_exit_unwrap()
- .code()
- .map(::std::process::exit);
- },
- }
- })
- .or_else(|| {
- if let (Some(from), Some(to)) = (rt.cli().value_of("from"), rt.cli().values_of("to")) {
- link_from_to(&rt, from, to);
- Some(())
- } else {
- warn_exit("No commandline call", 1)
- }
- })
- .ok_or_else(|| err_msg("No commandline call".to_owned()))
- .map_err_trace_exit_unwrap();
+ fn build_cli<'a>(app: App<'a, 'a>) -> App<'a, 'a> {
+ ui::build_ui(app)
+ }
+
+ fn name() -> &'static str {
+ env!("CARGO_PKG_NAME")
+ }
+
+ fn description() -> &'static str {
+ "Link entries"
+ }
+
+ fn version() -> &'static str {
+ env!("CARGO_PKG_VERSION")
+ }
}
fn get_entry_by_name<'a>(rt: &'a Runtime, name: &str) -> Result<Option<FileLockEntry<'a>>> {