imag-link: implement ImagApplication
Signed-off-by: Leon Schuermann <leon@is.currently.online>
This commit is contained in:
parent
208d6e62e6
commit
aa851a87f5
3 changed files with 110 additions and 47 deletions
|
@ -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"
|
||||
|
|
39
bin/core/imag-link/src/bin.rs
Normal file
39
bin/core/imag-link/src/bin.rs
Normal 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);
|
|
@ -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<Option<FileLockEntry<'a>>> {
|
Loading…
Reference in a new issue