imag-category: implement ImagApplication

Signed-off-by: Leon Schuermann <leon@is.currently.online>
This commit is contained in:
Leon Schuermann 2019-09-14 18:26:50 +02:00 committed by Matthias Beyer
parent ec75892516
commit 79cfa8d9d8
3 changed files with 93 additions and 24 deletions

View File

@ -36,3 +36,10 @@ version = "2.33.0"
default-features = false
features = ["color", "suggestions", "wrap_help"]
[lib]
name = "libimagcategorycmd"
path = "src/lib.rs"
[[bin]]
name = "imag-category"
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!(libimagcategorycmd, ImagCategory);

View File

@ -42,15 +42,18 @@ extern crate failure;
extern crate libimagentrycategory;
extern crate libimagerror;
#[macro_use] extern crate libimagrt;
extern crate libimagrt;
extern crate libimagstore;
extern crate libimaginteraction;
use failure::Fallible as Result;
use clap::App;
use libimagerror::trace::MapErrTrace;
use libimagerror::exit::ExitUnwrap;
use libimagerror::io::ToExitCode;
use libimagrt::runtime::Runtime;
use libimagrt::setup::generate_runtime_setup;
use libimagrt::application::ImagApplication;
mod ui;
@ -63,32 +66,52 @@ use libimagerror::iter::TraceIterator;
use libimagentrycategory::entry::EntryCategory;
use libimagentrycategory::category::Category;
fn main() {
let version = make_imag_version!();
let rt = generate_runtime_setup("imag-category",
&version,
"Add a category to entries and manage categories",
ui::build_ui);
if let Some(name) = rt.cli().subcommand_name() {
match name {
"set" => set(&rt),
"get" => get(&rt),
"list-category" => list_category(&rt),
"create-category" => create_category(&rt),
"delete-category" => delete_category(&rt),
"list-categories" => list_categories(&rt),
other => {
debug!("Unknown command");
let _ = rt.handle_unknown_subcommand("imag-category", other, rt.cli())
.map_err_trace_exit_unwrap()
.code()
.map(::std::process::exit);
},
/// Marker enum for implementing ImagApplication on
///
/// This is used by binaries crates to execute business logic
/// or to build a CLI completion.
pub enum ImagCategory {}
impl ImagApplication for ImagCategory {
fn run(rt: Runtime) -> Result<()> {
if let Some(name) = rt.cli().subcommand_name() {
match name {
"set" => set(&rt),
"get" => get(&rt),
"list-category" => list_category(&rt),
"create-category" => create_category(&rt),
"delete-category" => delete_category(&rt),
"list-categories" => list_categories(&rt),
other => {
debug!("Unknown command");
let _ = rt.handle_unknown_subcommand("imag-category", other, rt.cli())
.map_err_trace_exit_unwrap()
.code()
.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 {
"Add a category to entries and manage categories"
}
fn version() -> &'static str {
env!("CARGO_PKG_VERSION")
}
}
fn set(rt: &Runtime) {
let scmd = rt.cli().subcommand_matches("set").unwrap(); // safed by main()
let name = scmd.value_of("set-name").map(String::from).unwrap(); // safed by clap