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,17 +77,17 @@ 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
///
fn main() { /// This is used by binaries crates to execute business logic
let version = make_imag_version!(); /// or to build a CLI completion.
let rt = generate_runtime_setup("imag-link", pub enum ImagLink {}
&version, impl ImagApplication for ImagLink {
"Link entries", fn run(rt: Runtime) -> Result<()> {
build_ui);
if rt.cli().is_present("check-consistency") { if rt.cli().is_present("check-consistency") {
let exit_code = match rt.store().check_link_consistency() { let exit_code = match rt.store().check_link_consistency() {
Ok(_) => { Ok(_) => {
@ -102,7 +102,7 @@ fn main() {
::std::process::exit(exit_code); ::std::process::exit(exit_code);
} }
rt.cli() let _ = rt.cli()
.subcommand_name() .subcommand_name()
.map(|name| { .map(|name| {
match name { match name {
@ -120,14 +120,32 @@ fn main() {
}) })
.or_else(|| { .or_else(|| {
if let (Some(from), Some(to)) = (rt.cli().value_of("from"), rt.cli().values_of("to")) { if let (Some(from), Some(to)) = (rt.cli().value_of("from"), rt.cli().values_of("to")) {
link_from_to(&rt, from, to); Some(link_from_to(&rt, from, to))
Some(())
} else { } else {
warn_exit("No commandline call", 1) warn_exit("No commandline call", 1)
} }
}) })
.ok_or_else(|| err_msg("No commandline call".to_owned())) .ok_or_else(|| err_msg("No commandline call".to_owned()))
.map_err_trace_exit_unwrap(); .map_err_trace_exit_unwrap();
Ok(())
}
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>>> { fn get_entry_by_name<'a>(rt: &'a Runtime, name: &str) -> Result<Option<FileLockEntry<'a>>> {