imag-create: implement ImagApplication & add CLI completion

Signed-off-by: Leon Schuermann <leon@is.currently.online>
This commit is contained in:
Leon Schuermann 2019-10-25 18:00:18 +02:00 committed by Matthias Beyer
parent 82e209bea8
commit e66f29187f
6 changed files with 158 additions and 84 deletions

View file

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

View file

@ -0,0 +1,105 @@
//
// 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,
)]
extern crate clap;
extern crate failure;
#[macro_use] extern crate log;
extern crate libimagerror;
extern crate libimagrt;
extern crate libimagstore;
use failure::Fallible as Result;
use clap::App;
use libimagrt::runtime::Runtime;
use libimagrt::application::ImagApplication;
use libimagerror::trace::MapErrTrace;
use libimagstore::iter::create::StoreIdCreateIteratorExtension;
use libimagstore::iter::retrieve::StoreIdRetrieveIteratorExtension;
use libimagerror::exit::ExitUnwrap;
mod ui;
pub enum ImagCreate {}
impl ImagApplication for ImagCreate {
fn run(rt: Runtime) -> Result<()> {
let force = rt.cli().is_present("force");
debug!("Detected force = {}", force);
let ids = rt.ids::<crate::ui::PathProvider>()
.map_err_trace_exit_unwrap()
.unwrap_or_else(|| {
error!("No ids supplied");
::std::process::exit(1);
})
.into_iter()
.map(|id| { debug!("id = {}", id); id })
.map(Ok);
if force {
ids.into_retrieve_iter(rt.store()).collect::<Result<Vec<_>>>()
} else {
ids.into_create_iter(rt.store()).collect::<Result<Vec<_>>>()
}.map_err_trace_exit_unwrap()
.into_iter()
.for_each(|el| {
rt.report_touched(el.get_location()).unwrap_or_exit();
trace!("Entry = {}", el.get_location());
});
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 {
"Plumbing tool to create entries"
}
fn version() -> &'static str {
env!("CARGO_PKG_VERSION")
}
}

View file

@ -1,84 +0,0 @@
//
// 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
//
//#![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,
//)]
extern crate clap;
extern crate failure;
#[macro_use] extern crate log;
extern crate libimagerror;
#[macro_use] extern crate libimagrt;
extern crate libimagstore;
use failure::Fallible as Result;
use libimagerror::trace::MapErrTrace;
use libimagrt::setup::generate_runtime_setup;
use libimagstore::iter::create::StoreIdCreateIteratorExtension;
use libimagstore::iter::retrieve::StoreIdRetrieveIteratorExtension;
use libimagerror::exit::ExitUnwrap;
mod ui;
fn main() {
let version = make_imag_version!();
let rt = generate_runtime_setup("imag-create",
&version,
"Plumbing tool creating entries",
ui::build_ui);
let force = rt.cli().is_present("force");
debug!("Detected force = {}", force);
let ids = rt.ids::<crate::ui::PathProvider>()
.map_err_trace_exit_unwrap()
.unwrap_or_else(|| {
error!("No ids supplied");
::std::process::exit(1);
})
.into_iter()
.map(|id| { debug!("id = {}", id); id })
.map(Ok);
if force {
ids.into_retrieve_iter(rt.store()).collect::<Result<Vec<_>>>()
} else {
ids.into_create_iter(rt.store()).collect::<Result<Vec<_>>>()
}.map_err_trace_exit_unwrap()
.into_iter()
.for_each(|el| {
rt.report_touched(el.get_location()).unwrap_or_exit();
trace!("Entry = {}", el.get_location());
});
}

View file

@ -25,6 +25,7 @@ log = "0.4.6"
# Build time dependencies for cli completion
imag-annotate = { optional = true, path = "../imag-annotate" }
imag-create = { optional = true, path = "../imag-create" }
imag-diagnostics = { optional = true, path = "../imag-diagnostics" }
imag-edit = { optional = true, path = "../imag-edit" }
imag-gps = { optional = true, path = "../imag-gps" }
@ -79,6 +80,7 @@ default = [ "cc-all" ]
# Features for enabling cli completion files for individual subcommands
cc-all = [
"cc-imag-annotate",
"cc-imag-create",
"cc-imag-diagnostics",
"cc-imag-edit",
"cc-imag-gps",
@ -103,6 +105,7 @@ cc-all = [
"cc-imag-wiki",
]
cc-imag-annotate = [ "imag-annotate" ]
cc-imag-create = [ "imag-create" ]
cc-imag-diagnostics = [ "imag-diagnostics" ]
cc-imag-edit = [ "imag-edit" ]
cc-imag-gps = [ "imag-gps" ]

View file

@ -30,6 +30,8 @@ use libimagrt::application::ImagApplication;
#[cfg(feature = "cc-imag-annotate")]
extern crate libimagannotatecmd;
#[cfg(feature = "cc-imag-create")]
extern crate libimagcreatecmd;
#[cfg(feature = "cc-imag-diagnostics")]
extern crate libimagdiagnosticscmd;
#[cfg(feature = "cc-imag-edit")]
@ -105,6 +107,8 @@ fn main() {
// TODO: This feels tedious, can we automate this?
#[cfg(feature = "cc-imag-annotate")]
let app = app.subcommand(build_subcommand!("annotate", libimagannotatecmd, ImagAnnotate));
#[cfg(feature = "cc-imag-create")]
let app = app.subcommand(build_subcommand!("create", libimagcreatecmd, ImagCreate));
#[cfg(feature = "cc-imag-diagnostics")]
let app = app.subcommand(build_subcommand!("diagnostics", libimagdiagnosticscmd, ImagDiagnostics));
#[cfg(feature = "cc-imag-edit")]