Rewrite imag-contact to handle reference storing

This patch rewrites the imag-contact binary to be able to work with the
new libimagcontact interface, which now uses libimagentryref for storing
references to the actual vcard files.

Signed-off-by: Matthias Beyer <mail@beyermatthias.de>
This commit is contained in:
Matthias Beyer 2019-04-16 18:33:02 +02:00
parent 1c851bbb79
commit 9b7edfc6c1
4 changed files with 49 additions and 7 deletions

View file

@ -24,7 +24,6 @@ maintenance = { status = "actively-developed" }
[dependencies]
log = "0.4.0"
toml = "0.4"
toml-query = "0.8"
vobject = "0.7"
handlebars = "1.0"
walkdir = "2"
@ -46,3 +45,9 @@ version = "^2.29"
default-features = false
features = ["color", "suggestions", "wrap_help"]
[dependencies.toml-query]
#version = "0.8"
default-features = false
features = ["typed"]
git = "https://github.com/matthiasbeyer/toml-query"
branch = "master"

View file

@ -43,9 +43,11 @@ use vobject::vcard::Vcard;
use vobject::vcard::VcardBuilder;
use vobject::write_component;
use toml_query::read::TomlValueReadExt;
use toml_query::read::Partial;
use toml::Value;
use uuid::Uuid;
use failure::Error;
use failure::err_msg;
use libimagcontact::store::ContactStore;
use libimagrt::runtime::Runtime;
@ -80,8 +82,20 @@ fn ask_continue(inputstream: &mut Read, outputstream: &mut Write) -> bool {
}
pub fn create(rt: &Runtime) {
let scmd = rt.cli().subcommand_matches("create").unwrap();
let mut template = String::from(TEMPLATE);
let scmd = rt.cli().subcommand_matches("create").unwrap();
let mut template = String::from(TEMPLATE);
let collection_name = rt.cli().value_of("ref-collection-name").unwrap_or("contacts");
let collection_name = String::from(collection_name);
let ref_config = rt // TODO: Re-Deserialize to libimagentryref::reference::Config
.config()
.ok_or_else(|| err_msg("Configuration missing, cannot continue!"))
.map_err_trace_exit_unwrap()
.read_partial::<libimagentryref::reference::Config>()
.map_err(Error::from)
.map_err_trace_exit_unwrap()
.ok_or_else(|| format_err!("Configuration missing: {}", libimagentryref::reference::Config::LOCATION))
.map_err_trace_exit_unwrap();
// TODO: Refactor the above to libimagutil or libimagrt?
let (mut dest, location, uuid) : (Box<Write>, Option<PathBuf>, String) = {
if let Some(mut fl) = scmd.value_of("file-location").map(PathBuf::from) {
@ -202,7 +216,7 @@ pub fn create(rt: &Runtime) {
if let Some(location) = location {
if !scmd.is_present("dont-track") {
let entry = rt.store()
.create_from_path(&location)
.create_from_path(&location, &ref_config, &collection_name)
.map_err_trace_exit_unwrap();
let _ = rt.report_touched(entry.get_location()).unwrap_or_exit();

View file

@ -43,7 +43,7 @@ extern crate handlebars;
extern crate walkdir;
extern crate uuid;
extern crate serde_json;
extern crate failure;
#[macro_use] extern crate failure;
extern crate libimagcontact;
extern crate libimagstore;
@ -59,7 +59,9 @@ use std::io::Write;
use handlebars::Handlebars;
use clap::ArgMatches;
use toml_query::read::TomlValueReadExt;
use toml_query::read::TomlValueReadTypeExt;
use toml_query::read::Partial;
use walkdir::WalkDir;
use failure::Error;
use failure::err_msg;
@ -158,6 +160,18 @@ fn import(rt: &Runtime) {
let scmd = rt.cli().subcommand_matches("import").unwrap(); // secured by main
let path = scmd.value_of("path").map(PathBuf::from).unwrap(); // secured by clap
let collection_name = rt.cli().value_of("contact-ref-collection-name").unwrap(); // default by clap
let ref_config = rt.config()
.ok_or_else(|| format_err!("No configuration, cannot continue!"))
.map_err_trace_exit_unwrap()
.read_partial::<libimagentryref::reference::Config>()
.map_err(Error::from)
.map_err_trace_exit_unwrap()
.ok_or_else(|| format_err!("Configuration missing: {}", libimagentryref::reference::Config::LOCATION))
.map_err_trace_exit_unwrap();
// TODO: Refactor the above to libimagutil or libimagrt?
if !path.exists() {
error!("Path does not exist");
exit(1)
@ -166,7 +180,7 @@ fn import(rt: &Runtime) {
if path.is_file() {
let entry = rt
.store()
.retrieve_from_path(&path)
.retrieve_from_path(&path, &ref_config, &collection_name)
.map_err_trace_exit_unwrap();
let _ = rt.report_touched(entry.get_location()).unwrap_or_exit();
@ -180,7 +194,7 @@ fn import(rt: &Runtime) {
let pb = PathBuf::from(entry.path());
let fle = rt
.store()
.retrieve_from_path(&pb)
.retrieve_from_path(&pb, &ref_config, &collection_name)
.map_err_trace_exit_unwrap();
let _ = rt.report_touched(fle.get_location()).unwrap_or_exit();

View file

@ -21,6 +21,14 @@ use clap::{Arg, App, SubCommand};
pub fn build_ui<'a>(app: App<'a, 'a>) -> App<'a, 'a> {
app
.arg(Arg::with_name("contact-ref-collection-name")
.long("ref-collection")
.takes_value(true)
.required(false)
.multiple(false)
.default_value("contacts")
.help("Name (Key) of the basepath setting in the configuration file to use"))
.subcommand(SubCommand::with_name("list")
.about("List contacts")
.version("0.1")
@ -56,6 +64,7 @@ pub fn build_ui<'a>(app: App<'a, 'a>) -> App<'a, 'a> {
.multiple(false)
.value_name("PATH")
.help("Import from this file/directory"))
)
.subcommand(SubCommand::with_name("show")