imag-header: implement ImagApplication

Signed-off-by: Leon Schuermann <leon@is.currently.online>
This commit is contained in:
Leon Schuermann 2019-09-14 18:41:51 +02:00 committed by Matthias Beyer
parent a7d55930d7
commit 97c46da599
3 changed files with 108 additions and 44 deletions

View file

@ -45,3 +45,10 @@ path = "../../../lib/core/libimagrt"
default-features = false default-features = false
features = ["testing"] features = ["testing"]
[lib]
name = "libimagheadercmd"
path = "src/lib.rs"
[[bin]]
name = "imag-header"
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!(libimagheadercmd, ImagHeader);

View file

@ -41,7 +41,7 @@ extern crate failure;
extern crate libimagentryedit; extern crate libimagentryedit;
extern crate libimagerror; extern crate libimagerror;
#[macro_use] extern crate libimagrt; extern crate libimagrt;
extern crate libimagstore; extern crate libimagstore;
extern crate libimagutil; extern crate libimagutil;
@ -49,10 +49,10 @@ use std::io::Write;
use std::str::FromStr; use std::str::FromStr;
use std::string::ToString; use std::string::ToString;
use clap::ArgMatches; use clap::{App, ArgMatches};
use filters::filter::Filter; use filters::filter::Filter;
use failure::Error;
use toml::Value; use toml::Value;
use failure::{Fallible as Result, Error};
use libimagerror::exit::ExitCode; use libimagerror::exit::ExitCode;
use libimagerror::exit::ExitUnwrap; use libimagerror::exit::ExitUnwrap;
@ -60,7 +60,7 @@ use libimagerror::io::ToExitCode;
use libimagerror::iter::TraceIterator; use libimagerror::iter::TraceIterator;
use libimagerror::trace::MapErrTrace; use libimagerror::trace::MapErrTrace;
use libimagrt::runtime::Runtime; use libimagrt::runtime::Runtime;
use libimagrt::setup::generate_runtime_setup; use libimagrt::application::ImagApplication;
use libimagstore::iter::get::StoreIdGetIteratorExtension; use libimagstore::iter::get::StoreIdGetIteratorExtension;
use libimagstore::store::FileLockEntry; use libimagstore::store::FileLockEntry;
use libimagstore::storeid::StoreIdIterator; use libimagstore::storeid::StoreIdIterator;
@ -68,55 +68,73 @@ use libimagstore::storeid::StoreIdIterator;
use toml_query::read::TomlValueReadExt; use toml_query::read::TomlValueReadExt;
use toml_query::read::TomlValueReadTypeExt; use toml_query::read::TomlValueReadTypeExt;
mod ui; mod ui;
const EPS_CMP: f64 = 1e-10; const EPS_CMP: f64 = 1e-10;
fn main() { /// Marker enum for implementing ImagApplication on
let version = make_imag_version!(); ///
let rt = generate_runtime_setup("imag-header", /// This is used by binaries crates to execute business logic
&version, /// or to build a CLI completion.
"Plumbing tool for reading/writing structured data in entries", pub enum ImagHeader {}
ui::build_ui); impl ImagApplication for ImagHeader {
fn run(rt: Runtime) -> Result<()> {
let list_output_with_ids = rt.cli().is_present("list-id");
let list_output_with_ids_fmt = rt.cli().value_of("list-id-format");
let list_output_with_ids = rt.cli().is_present("list-id"); trace!("list_output_with_ids = {:?}", list_output_with_ids );
let list_output_with_ids_fmt = rt.cli().value_of("list-id-format"); trace!("list_output_with_ids_fmt = {:?}", list_output_with_ids_fmt);
trace!("list_output_with_ids = {:?}", list_output_with_ids ); let sids = rt
trace!("list_output_with_ids_fmt = {:?}", list_output_with_ids_fmt); .ids::<crate::ui::PathProvider>()
.map_err_trace_exit_unwrap()
.unwrap_or_else(|| {
error!("No ids supplied");
::std::process::exit(1);
})
.into_iter();
let sids = rt let iter = StoreIdIterator::new(Box::new(sids.map(Ok)))
.ids::<crate::ui::PathProvider>() .into_get_iter(rt.store())
.map_err_trace_exit_unwrap() .trace_unwrap_exit()
.unwrap_or_else(|| { .filter_map(|x| x);
error!("No ids supplied");
::std::process::exit(1);
})
.into_iter();
let iter = StoreIdIterator::new(Box::new(sids.map(Ok))) match rt.cli().subcommand() {
.into_get_iter(rt.store()) ("read", Some(mtch)) => ::std::process::exit(read(&rt, mtch, iter)),
.trace_unwrap_exit() ("has", Some(mtch)) => has(&rt, mtch, iter),
.filter_map(|x| x); ("hasnt", Some(mtch)) => hasnt(&rt, mtch, iter),
("int", Some(mtch)) => int(&rt, mtch, iter),
("float", Some(mtch)) => float(&rt, mtch, iter),
("string", Some(mtch)) => string(&rt, mtch, iter),
("bool", Some(mtch)) => boolean(&rt, mtch, iter),
(other, _mtchs) => {
debug!("Unknown command");
::std::process::exit({
rt.handle_unknown_subcommand("imag-header", other, rt.cli())
.map_err_trace_exit_unwrap()
.code()
.unwrap_or(1)
});
},
};
match rt.cli().subcommand() { Ok(())
("read", Some(mtch)) => ::std::process::exit(read(&rt, mtch, iter)), }
("has", Some(mtch)) => has(&rt, mtch, iter),
("hasnt", Some(mtch)) => hasnt(&rt, mtch, iter), fn build_cli<'a>(app: App<'a, 'a>) -> App<'a, 'a> {
("int", Some(mtch)) => int(&rt, mtch, iter), ui::build_ui(app)
("float", Some(mtch)) => float(&rt, mtch, iter), }
("string", Some(mtch)) => string(&rt, mtch, iter),
("bool", Some(mtch)) => boolean(&rt, mtch, iter), fn name() -> &'static str {
(other, _mtchs) => { env!("CARGO_PKG_NAME")
debug!("Unknown command"); }
::std::process::exit({
rt.handle_unknown_subcommand("imag-header", other, rt.cli()) fn description() -> &'static str {
.map_err_trace_exit_unwrap() "Plumbing tool for reading/writing structured data in entries"
.code() }
.unwrap_or(1)
}); fn version() -> &'static str {
}, env!("CARGO_PKG_VERSION")
} }
} }