Add imag-create plumbing command for creating entries

Signed-off-by: Matthias Beyer <mail@beyermatthias.de>
This commit is contained in:
Matthias Beyer 2019-09-28 19:55:35 +02:00
parent 2caeb0a510
commit fd3b46fa46
5 changed files with 165 additions and 0 deletions

View File

@ -3,6 +3,7 @@ members = [
"bin/core/imag", "bin/core/imag",
"bin/core/imag-annotate", "bin/core/imag-annotate",
"bin/core/imag-category", "bin/core/imag-category",
"bin/core/imag-create",
"bin/core/imag-diagnostics", "bin/core/imag-diagnostics",
"bin/core/imag-edit", "bin/core/imag-edit",
"bin/core/imag-git", "bin/core/imag-git",

View File

@ -0,0 +1,29 @@
[package]
name = "imag-create"
version = "0.10.0"
authors = ["Matthias Beyer <mail@beyermatthias.de>"]
edition = "2018"
description = "Part of the imag core distribution: imag-diagnostics command"
keywords = ["imag", "PIM", "personal", "information", "management"]
readme = "../../../README.md"
license = "LGPL-2.1"
documentation = "https://imag-pim.org/doc/"
repository = "https://github.com/matthiasbeyer/imag"
homepage = "http://imag-pim.org"
[dependencies]
log = "0.4"
failure = "0.1"
libimagstore = { version = "0.10.0", path = "../../../lib/core/libimagstore" }
libimagrt = { version = "0.10.0", path = "../../../lib/core/libimagrt" }
libimagerror = { version = "0.10.0", path = "../../../lib/core/libimagerror" }
[dependencies.clap]
version = "2.33.0"
default-features = false
features = ["suggestions", "color", "wrap_help"]

View File

@ -0,0 +1,76 @@
//
// 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;
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");
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(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();
}

View File

@ -0,0 +1,58 @@
//
// 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
//
use std::path::PathBuf;
use clap::{Arg, ArgMatches, App};
use failure::Fallible as Result;
use libimagstore::storeid::StoreId;
use libimagstore::storeid::IntoStoreId;
use libimagrt::runtime::IdPathProvider;
pub fn build_ui<'a>(app: App<'a, 'a>) -> App<'a, 'a> {
app.arg(Arg::with_name("id")
.index(1)
.takes_value(true)
.required(false)
.multiple(true)
.help("Entries to create"))
.arg(Arg::with_name("force")
.long("force")
.short("F")
.takes_value(false)
.required(false)
.multiple(false)
.help("Do not fail if entry already exists"))
}
pub struct PathProvider;
impl IdPathProvider for PathProvider {
fn get_ids(matches: &ArgMatches) -> Result<Option<Vec<StoreId>>> {
matches.values_of("id")
.map(|v| v
.map(PathBuf::from)
.map(|pb| pb.into_storeid())
.collect::<Result<Vec<_>>>()
)
.transpose()
}
}

View File

@ -66,6 +66,7 @@ CRATES=(
./bin/core/imag-git ./bin/core/imag-git
./bin/core/imag-category ./bin/core/imag-category
./bin/core/imag-header ./bin/core/imag-header
./bin/core/imag-create
./bin/core/imag ./bin/core/imag
) )