2017-10-06 09:23:07 +00:00
|
|
|
//
|
|
|
|
// imag - the personal information management suite for the commandline
|
2018-02-07 01:48:53 +00:00
|
|
|
// Copyright (C) 2015-2018 Matthias Beyer <mail@beyermatthias.de> and contributors
|
2017-10-06 09:23:07 +00:00
|
|
|
//
|
|
|
|
// 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;
|
|
|
|
#[macro_use] extern crate log;
|
2017-11-03 19:18:26 +00:00
|
|
|
#[macro_use] extern crate vobject;
|
2017-10-06 09:23:07 +00:00
|
|
|
extern crate toml;
|
|
|
|
extern crate toml_query;
|
2017-10-10 16:48:21 +00:00
|
|
|
extern crate handlebars;
|
|
|
|
extern crate walkdir;
|
2017-11-03 19:18:26 +00:00
|
|
|
extern crate uuid;
|
2017-10-06 09:23:07 +00:00
|
|
|
|
|
|
|
extern crate libimagcontact;
|
2017-10-10 16:48:21 +00:00
|
|
|
extern crate libimagstore;
|
2018-01-31 10:09:00 +00:00
|
|
|
#[macro_use] extern crate libimagrt;
|
2017-10-06 09:23:07 +00:00
|
|
|
extern crate libimagerror;
|
|
|
|
extern crate libimagutil;
|
2017-10-10 16:48:21 +00:00
|
|
|
extern crate libimaginteraction;
|
|
|
|
extern crate libimagentryref;
|
2017-11-03 19:18:26 +00:00
|
|
|
extern crate libimagentryedit;
|
2017-10-06 09:23:07 +00:00
|
|
|
|
|
|
|
use std::process::exit;
|
2017-10-10 16:48:21 +00:00
|
|
|
use std::path::PathBuf;
|
2017-10-06 09:23:07 +00:00
|
|
|
|
2017-10-10 16:48:21 +00:00
|
|
|
use handlebars::Handlebars;
|
|
|
|
use clap::ArgMatches;
|
|
|
|
use vobject::vcard::Vcard;
|
2018-01-12 15:30:09 +00:00
|
|
|
use toml_query::read::TomlValueReadTypeExt;
|
2017-10-10 16:48:21 +00:00
|
|
|
use walkdir::WalkDir;
|
2017-10-06 09:23:07 +00:00
|
|
|
|
|
|
|
use libimagrt::runtime::Runtime;
|
|
|
|
use libimagrt::setup::generate_runtime_setup;
|
2018-02-12 20:25:51 +00:00
|
|
|
use libimagerror::str::ErrFromStr;
|
2017-10-10 16:48:21 +00:00
|
|
|
use libimagerror::trace::MapErrTrace;
|
|
|
|
use libimagcontact::store::ContactStore;
|
|
|
|
use libimagcontact::error::ContactError as CE;
|
|
|
|
use libimagcontact::contact::Contact;
|
|
|
|
use libimagstore::iter::get::StoreIdGetIteratorExtension;
|
|
|
|
use libimagentryref::reference::Ref;
|
|
|
|
use libimagentryref::refstore::RefStore;
|
2017-10-06 09:23:07 +00:00
|
|
|
|
|
|
|
mod ui;
|
2017-10-18 09:20:27 +00:00
|
|
|
mod util;
|
|
|
|
mod create;
|
2017-10-06 09:23:07 +00:00
|
|
|
|
|
|
|
use ui::build_ui;
|
2017-10-18 09:20:27 +00:00
|
|
|
use util::build_data_object_for_handlebars;
|
|
|
|
use create::create;
|
2017-10-06 09:23:07 +00:00
|
|
|
|
|
|
|
fn main() {
|
2018-01-31 10:09:00 +00:00
|
|
|
let version = make_imag_version!();
|
2017-10-06 09:23:07 +00:00
|
|
|
let rt = generate_runtime_setup("imag-contact",
|
2018-01-31 10:09:00 +00:00
|
|
|
&version,
|
2017-10-06 09:23:07 +00:00
|
|
|
"Contact management tool",
|
|
|
|
build_ui);
|
|
|
|
|
2017-10-10 16:48:21 +00:00
|
|
|
|
|
|
|
rt.cli()
|
|
|
|
.subcommand_name()
|
|
|
|
.map(|name| {
|
|
|
|
debug!("Call {}", name);
|
|
|
|
match name {
|
|
|
|
"list" => list(&rt),
|
|
|
|
"import" => import(&rt),
|
|
|
|
"show" => show(&rt),
|
2017-10-18 09:20:27 +00:00
|
|
|
"create" => create(&rt),
|
2017-10-10 16:48:21 +00:00
|
|
|
_ => {
|
|
|
|
error!("Unknown command"); // More error handling
|
|
|
|
},
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
fn list(rt: &Runtime) {
|
|
|
|
let scmd = rt.cli().subcommand_matches("list").unwrap();
|
|
|
|
let list_format = get_contact_print_format("contact.list_format", rt, &scmd);
|
|
|
|
|
|
|
|
let _ = rt
|
|
|
|
.store()
|
|
|
|
.all_contacts()
|
2018-01-22 11:48:28 +00:00
|
|
|
.map_err_trace_exit_unwrap(1)
|
2017-10-10 16:48:21 +00:00
|
|
|
.into_get_iter(rt.store())
|
|
|
|
.map(|fle| {
|
|
|
|
let fle = fle
|
2018-01-22 11:48:28 +00:00
|
|
|
.map_err_trace_exit_unwrap(1)
|
2017-10-10 16:48:21 +00:00
|
|
|
.ok_or_else(|| CE::from("StoreId not found".to_owned()))
|
2018-01-22 11:48:28 +00:00
|
|
|
.map_err_trace_exit_unwrap(1);
|
2017-10-10 16:48:21 +00:00
|
|
|
|
|
|
|
fle
|
|
|
|
.get_contact_data()
|
|
|
|
.map(|cd| (fle, cd))
|
|
|
|
.map(|(fle, cd)| (fle, cd.into_inner()))
|
|
|
|
.map(|(fle, cd)| (fle, Vcard::from_component(cd)))
|
2018-01-22 11:48:28 +00:00
|
|
|
.map_err_trace_exit_unwrap(1)
|
2017-10-10 16:48:21 +00:00
|
|
|
})
|
|
|
|
.enumerate()
|
|
|
|
.map(|(i, (fle, vcard))| {
|
2018-01-22 11:48:28 +00:00
|
|
|
let hash = fle.get_path_hash().map_err_trace_exit_unwrap(1);
|
2017-10-10 16:48:21 +00:00
|
|
|
let vcard = vcard.unwrap_or_else(|e| {
|
|
|
|
error!("Element is not a VCARD object: {:?}", e);
|
|
|
|
exit(1)
|
|
|
|
});
|
|
|
|
|
|
|
|
let data = build_data_object_for_handlebars(i, hash, &vcard);
|
|
|
|
|
|
|
|
let s = list_format.render("format", &data)
|
2018-02-12 20:25:51 +00:00
|
|
|
.err_from_str()
|
2018-02-12 20:05:06 +00:00
|
|
|
.map_err(CE::from)
|
2018-01-22 11:48:28 +00:00
|
|
|
.map_err_trace_exit_unwrap(1);
|
2017-10-10 16:48:21 +00:00
|
|
|
println!("{}", s);
|
|
|
|
})
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
if !path.exists() {
|
|
|
|
error!("Path does not exist");
|
|
|
|
exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
if path.is_file() {
|
|
|
|
let _ = rt
|
|
|
|
.store()
|
|
|
|
.create_from_path(&path)
|
2018-01-22 11:48:28 +00:00
|
|
|
.map_err_trace_exit_unwrap(1);
|
2017-10-10 16:48:21 +00:00
|
|
|
} else if path.is_dir() {
|
|
|
|
for entry in WalkDir::new(path).min_depth(1).into_iter() {
|
2018-02-12 20:05:06 +00:00
|
|
|
let entry = entry
|
2018-02-12 20:25:51 +00:00
|
|
|
.err_from_str()
|
2018-02-12 20:05:06 +00:00
|
|
|
.map_err(CE::from)
|
|
|
|
.map_err_trace_exit_unwrap(1);
|
2017-10-10 16:48:21 +00:00
|
|
|
if entry.file_type().is_file() {
|
|
|
|
let pb = PathBuf::from(entry.path());
|
|
|
|
let _ = rt
|
|
|
|
.store()
|
|
|
|
.create_from_path(&pb)
|
2018-01-22 11:48:28 +00:00
|
|
|
.map_err_trace_exit_unwrap(1);
|
2017-10-10 16:48:21 +00:00
|
|
|
info!("Imported: {}", entry.path().to_str().unwrap_or("<non UTF-8 path>"));
|
|
|
|
} else {
|
|
|
|
warn!("Ignoring non-file: {}", entry.path().to_str().unwrap_or("<non UTF-8 path>"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
error!("Path is neither directory nor file");
|
|
|
|
exit(1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn show(rt: &Runtime) {
|
|
|
|
let scmd = rt.cli().subcommand_matches("show").unwrap();
|
|
|
|
let hash = scmd.value_of("hash").map(String::from).unwrap(); // safed by clap
|
|
|
|
|
|
|
|
let contact_data = rt.store()
|
|
|
|
.get_by_hash(hash.clone())
|
2018-01-22 11:48:28 +00:00
|
|
|
.map_err_trace_exit_unwrap(1)
|
2017-10-10 16:48:21 +00:00
|
|
|
.ok_or(CE::from(format!("No entry for hash {}", hash)))
|
2018-01-22 11:48:28 +00:00
|
|
|
.map_err_trace_exit_unwrap(1)
|
2017-10-10 16:48:21 +00:00
|
|
|
.get_contact_data()
|
2018-01-22 11:48:28 +00:00
|
|
|
.map_err_trace_exit_unwrap(1)
|
2017-10-10 16:48:21 +00:00
|
|
|
.into_inner();
|
|
|
|
let vcard = Vcard::from_component(contact_data)
|
|
|
|
.unwrap_or_else(|e| {
|
|
|
|
error!("Element is not a VCARD object: {:?}", e);
|
|
|
|
exit(1)
|
|
|
|
});
|
|
|
|
|
|
|
|
let show_format = get_contact_print_format("contact.show_format", rt, &scmd);
|
|
|
|
let data = build_data_object_for_handlebars(0, hash, &vcard);
|
|
|
|
|
2018-02-12 20:05:06 +00:00
|
|
|
let s = show_format
|
|
|
|
.render("format", &data)
|
2018-02-12 20:25:51 +00:00
|
|
|
.err_from_str()
|
2018-02-12 20:05:06 +00:00
|
|
|
.map_err(CE::from)
|
|
|
|
.map_err_trace_exit_unwrap(1);
|
2017-10-10 16:48:21 +00:00
|
|
|
println!("{}", s);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_contact_print_format(config_value_path: &'static str, rt: &Runtime, scmd: &ArgMatches) -> Handlebars {
|
|
|
|
let fmt = scmd
|
|
|
|
.value_of("format")
|
|
|
|
.map(String::from)
|
|
|
|
.unwrap_or_else(|| {
|
|
|
|
rt.config()
|
|
|
|
.ok_or_else(|| CE::from("No configuration file".to_owned()))
|
2018-01-12 15:30:09 +00:00
|
|
|
.map_err_trace_exit_unwrap(1)
|
|
|
|
.read_string(config_value_path)
|
|
|
|
.map_err_trace_exit_unwrap(1)
|
2017-10-10 16:48:21 +00:00
|
|
|
.ok_or_else(|| CE::from("Configuration 'contact.list_format' does not exist".to_owned()))
|
2018-01-12 15:30:09 +00:00
|
|
|
.map_err_trace_exit_unwrap(1)
|
2017-10-10 16:48:21 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
let mut hb = Handlebars::new();
|
2018-02-12 20:05:06 +00:00
|
|
|
let _ = hb
|
|
|
|
.register_template_string("format", fmt)
|
2018-02-12 20:25:51 +00:00
|
|
|
.err_from_str()
|
2018-02-12 20:05:06 +00:00
|
|
|
.map_err(CE::from)
|
|
|
|
.map_err_trace_exit_unwrap(1);
|
2017-10-10 16:48:21 +00:00
|
|
|
|
|
|
|
hb.register_escape_fn(::handlebars::no_escape);
|
|
|
|
::libimaginteraction::format::register_all_color_helpers(&mut hb);
|
|
|
|
::libimaginteraction::format::register_all_format_helpers(&mut hb);
|
|
|
|
hb
|
2017-10-06 09:23:07 +00:00
|
|
|
}
|
|
|
|
|