From aa851a87f5fb8ddf98edd98561980a562319b05e Mon Sep 17 00:00:00 2001 From: Leon Schuermann Date: Sat, 14 Sep 2019 18:44:20 +0200 Subject: [PATCH] imag-link: implement ImagApplication Signed-off-by: Leon Schuermann --- bin/core/imag-link/Cargo.toml | 6 ++ bin/core/imag-link/src/bin.rs | 39 +++++++ bin/core/imag-link/src/{main.rs => lib.rs} | 112 ++++++++++++--------- 3 files changed, 110 insertions(+), 47 deletions(-) create mode 100644 bin/core/imag-link/src/bin.rs rename bin/core/imag-link/src/{main.rs => lib.rs} (89%) 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 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 similarity index 89% rename from bin/core/imag-link/src/main.rs rename to 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; +/// 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); + } -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); + 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>> {