imag-link: implement ImagApplication

Signed-off-by: Leon Schuermann <leon@is.currently.online>
This commit is contained in:
Leon Schuermann 2019-09-14 18:44:20 +02:00 committed by Matthias Beyer
parent 208d6e62e6
commit aa851a87f5
3 changed files with 110 additions and 47 deletions

View file

@ -54,4 +54,10 @@ path = "../../../lib/core/libimagrt"
default-features = false default-features = false
features = ["testing"] features = ["testing"]
[lib]
name = "libimaglinkcmd"
path = "src/lib.rs"
[[bin]]
name = "imag-link"
path = "src/bin.rs"

View file

@ -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);

View file

@ -45,7 +45,7 @@ extern crate failure;
extern crate libimagentrylink; extern crate libimagentrylink;
extern crate libimagentryurl; extern crate libimagentryurl;
#[macro_use] extern crate libimagrt; extern crate libimagrt;
extern crate libimagstore; extern crate libimagstore;
extern crate libimagerror; extern crate libimagerror;
@ -69,7 +69,7 @@ use libimagerror::trace::{MapErrTrace, trace_error};
use libimagerror::exit::ExitUnwrap; use libimagerror::exit::ExitUnwrap;
use libimagerror::io::ToExitCode; use libimagerror::io::ToExitCode;
use libimagrt::runtime::Runtime; use libimagrt::runtime::Runtime;
use libimagrt::setup::generate_runtime_setup; use libimagrt::application::ImagApplication;
use libimagstore::store::FileLockEntry; use libimagstore::store::FileLockEntry;
use libimagstore::storeid::StoreId; use libimagstore::storeid::StoreId;
use libimagutil::warn_exit::warn_exit; use libimagutil::warn_exit::warn_exit;
@ -77,57 +77,75 @@ use libimagutil::warn_result::*;
use url::Url; use url::Url;
use failure::Fallible as Result; use failure::Fallible as Result;
use clap::App;
mod ui; 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 _ = rt.cli()
let version = make_imag_version!(); .subcommand_name()
let rt = generate_runtime_setup("imag-link", .map(|name| {
&version, match name {
"Link entries", "remove" => remove_linking(&rt),
build_ui); "unlink" => unlink(&rt),
if rt.cli().is_present("check-consistency") { "list" => list_linkings(&rt),
let exit_code = match rt.store().check_link_consistency() { other => {
Ok(_) => { debug!("Unknown command");
info!("Store is consistent"); let _ = rt.handle_unknown_subcommand("imag-link", other, rt.cli())
0 .map_err_trace_exit_unwrap()
} .code()
Err(e) => { .map(::std::process::exit);
trace_error(&e); },
1 }
} })
}; .or_else(|| {
::std::process::exit(exit_code); 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() fn build_cli<'a>(app: App<'a, 'a>) -> App<'a, 'a> {
.subcommand_name() ui::build_ui(app)
.map(|name| { }
match name {
"remove" => remove_linking(&rt), fn name() -> &'static str {
"unlink" => unlink(&rt), env!("CARGO_PKG_NAME")
"list" => list_linkings(&rt), }
other => {
debug!("Unknown command"); fn description() -> &'static str {
let _ = rt.handle_unknown_subcommand("imag-link", other, rt.cli()) "Link entries"
.map_err_trace_exit_unwrap() }
.code()
.map(::std::process::exit); fn version() -> &'static str {
}, env!("CARGO_PKG_VERSION")
} }
})
.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 get_entry_by_name<'a>(rt: &'a Runtime, name: &str) -> Result<Option<FileLockEntry<'a>>> { fn get_entry_by_name<'a>(rt: &'a Runtime, name: &str) -> Result<Option<FileLockEntry<'a>>> {