imag-contact: implement ImagApplication

Signed-off-by: Leon Schuermann <leon@is.currently.online>
This commit is contained in:
Leon Schuermann 2019-09-14 18:51:22 +02:00 committed by Matthias Beyer
parent cb69214e72
commit 3144080488
3 changed files with 93 additions and 28 deletions

View file

@ -47,3 +47,11 @@ features = ["color", "suggestions", "wrap_help"]
version = "0.9.2" version = "0.9.2"
default-features = false default-features = false
features = ["typed"] features = ["typed"]
[lib]
name = "libimagcontactfrontend"
path = "src/lib.rs"
[[bin]]
name = "imag-contact"
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!(libimagcontactfrontend, ImagContact);

View file

@ -47,7 +47,7 @@ extern crate serde_json;
extern crate libimagcontact; extern crate libimagcontact;
extern crate libimagstore; extern crate libimagstore;
#[macro_use] extern crate libimagrt; extern crate libimagrt;
extern crate libimagerror; extern crate libimagerror;
extern crate libimagutil; extern crate libimagutil;
extern crate libimaginteraction; extern crate libimaginteraction;
@ -59,16 +59,17 @@ use std::path::PathBuf;
use std::io::Write; use std::io::Write;
use handlebars::Handlebars; use handlebars::Handlebars;
use clap::ArgMatches; use clap::{App, ArgMatches};
use toml_query::read::TomlValueReadExt; use toml_query::read::TomlValueReadExt;
use toml_query::read::TomlValueReadTypeExt; use toml_query::read::TomlValueReadTypeExt;
use toml_query::read::Partial; use toml_query::read::Partial;
use walkdir::WalkDir; use walkdir::WalkDir;
use failure::Error; use failure::Error;
use failure::err_msg; use failure::err_msg;
use failure::Fallible as Result;
use libimagrt::runtime::Runtime; use libimagrt::runtime::Runtime;
use libimagrt::setup::generate_runtime_setup; use libimagrt::application::ImagApplication;
use libimagerror::trace::MapErrTrace; use libimagerror::trace::MapErrTrace;
use libimagerror::io::ToExitCode; use libimagerror::io::ToExitCode;
use libimagerror::exit::ExitUnwrap; use libimagerror::exit::ExitUnwrap;
@ -82,36 +83,53 @@ mod util;
mod create; mod create;
mod edit; mod edit;
use crate::ui::build_ui;
use crate::util::build_data_object_for_handlebars; use crate::util::build_data_object_for_handlebars;
use crate::create::create; use crate::create::create;
use crate::edit::edit; use crate::edit::edit;
fn main() { /// Marker enum for implementing ImagApplication on
let version = make_imag_version!(); ///
let rt = generate_runtime_setup("imag-contact", /// This is used by binaries crates to execute business logic
&version, /// or to build a CLI completion.
"Contact management tool", pub enum ImagContact {}
build_ui); impl ImagApplication for ImagContact {
fn run(rt: Runtime) -> Result<()> {
if let Some(name) = rt.cli().subcommand_name() {
if let Some(name) = rt.cli().subcommand_name() { debug!("Call {}", name);
debug!("Call {}", name); match name {
match name { "list" => list(&rt),
"list" => list(&rt), "import" => import(&rt),
"import" => import(&rt), "show" => show(&rt),
"show" => show(&rt), "edit" => edit(&rt),
"edit" => edit(&rt), "find" => find(&rt),
"find" => find(&rt), "create" => create(&rt),
"create" => create(&rt), other => {
other => { debug!("Unknown command");
debug!("Unknown command"); let _ = rt.handle_unknown_subcommand("imag-contact", other, rt.cli())
let _ = rt.handle_unknown_subcommand("imag-contact", other, rt.cli()) .map_err_trace_exit_unwrap()
.map_err_trace_exit_unwrap() .code()
.code() .map(::std::process::exit);
.map(::std::process::exit); },
}, }
} }
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 {
"Contact management tool"
}
fn version() -> &'static str {
env!("CARGO_PKG_VERSION")
} }
} }