2017-10-06 09:23:07 +00:00
|
|
|
//
|
|
|
|
// imag - the personal information management suite for the commandline
|
2019-01-03 01:32:07 +00:00
|
|
|
// Copyright (C) 2015-2019 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
|
|
|
|
//
|
|
|
|
|
2018-09-27 06:13:58 +00:00
|
|
|
#![forbid(unsafe_code)]
|
|
|
|
|
2017-10-06 09:23:07 +00:00
|
|
|
#![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;
|
2018-03-12 16:46:06 +00:00
|
|
|
extern crate serde_json;
|
2018-10-30 17:40:53 +00:00
|
|
|
extern crate failure;
|
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;
|
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;
|
2018-02-14 17:03:01 +00:00
|
|
|
use std::io::Write;
|
2017-10-06 09:23:07 +00:00
|
|
|
|
2017-10-10 16:48:21 +00:00
|
|
|
use handlebars::Handlebars;
|
|
|
|
use clap::ArgMatches;
|
2018-01-12 15:30:09 +00:00
|
|
|
use toml_query::read::TomlValueReadTypeExt;
|
2017-10-10 16:48:21 +00:00
|
|
|
use walkdir::WalkDir;
|
2018-10-30 17:40:53 +00:00
|
|
|
use failure::Error;
|
|
|
|
use failure::err_msg;
|
2017-10-06 09:23:07 +00:00
|
|
|
|
|
|
|
use libimagrt::runtime::Runtime;
|
|
|
|
use libimagrt::setup::generate_runtime_setup;
|
2017-10-10 16:48:21 +00:00
|
|
|
use libimagerror::trace::MapErrTrace;
|
2018-02-14 17:03:01 +00:00
|
|
|
use libimagerror::io::ToExitCode;
|
|
|
|
use libimagerror::exit::ExitUnwrap;
|
2018-04-25 11:34:46 +00:00
|
|
|
use libimagerror::iter::TraceIterator;
|
2017-10-10 16:48:21 +00:00
|
|
|
use libimagcontact::store::ContactStore;
|
|
|
|
use libimagcontact::contact::Contact;
|
2018-03-12 16:46:06 +00:00
|
|
|
use libimagcontact::deser::DeserVcard;
|
2017-10-10 16:48:21 +00:00
|
|
|
use libimagstore::iter::get::StoreIdGetIteratorExtension;
|
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),
|
2018-03-12 14:23:37 +00:00
|
|
|
"find" => find(&rt),
|
2017-10-18 09:20:27 +00:00
|
|
|
"create" => create(&rt),
|
2018-04-04 15:01:02 +00:00
|
|
|
other => {
|
|
|
|
debug!("Unknown command");
|
|
|
|
let _ = rt.handle_unknown_subcommand("imag-contact", other, rt.cli())
|
2019-02-05 00:37:32 +00:00
|
|
|
.map_err_trace_exit_unwrap()
|
2018-04-04 15:01:02 +00:00
|
|
|
.code()
|
|
|
|
.map(::std::process::exit);
|
2017-10-10 16:48:21 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
fn list(rt: &Runtime) {
|
|
|
|
let scmd = rt.cli().subcommand_matches("list").unwrap();
|
|
|
|
let list_format = get_contact_print_format("contact.list_format", rt, &scmd);
|
|
|
|
|
2018-03-12 16:46:06 +00:00
|
|
|
let iterator = rt
|
2017-10-10 16:48:21 +00:00
|
|
|
.store()
|
|
|
|
.all_contacts()
|
2019-02-05 00:37:32 +00:00
|
|
|
.map_err_trace_exit_unwrap()
|
2017-10-10 16:48:21 +00:00
|
|
|
.into_get_iter(rt.store())
|
2019-02-05 00:37:32 +00:00
|
|
|
.trace_unwrap_exit()
|
2018-11-04 21:08:05 +00:00
|
|
|
.map(|fle| fle.ok_or_else(|| Error::from(err_msg("StoreId not found".to_owned()))))
|
2019-02-05 00:37:32 +00:00
|
|
|
.trace_unwrap_exit()
|
2018-10-06 10:50:12 +00:00
|
|
|
.map(|fle| {
|
2019-02-03 18:53:50 +00:00
|
|
|
let _ = rt.report_touched(fle.get_location()).unwrap_or_exit();
|
2018-10-06 10:50:12 +00:00
|
|
|
fle
|
|
|
|
})
|
2018-11-04 21:08:05 +00:00
|
|
|
.map(|e| e.deser())
|
2019-02-05 00:37:32 +00:00
|
|
|
.trace_unwrap_exit()
|
2018-03-12 16:46:06 +00:00
|
|
|
.enumerate();
|
|
|
|
|
|
|
|
if scmd.is_present("json") {
|
2018-04-25 11:34:46 +00:00
|
|
|
let v : Vec<DeserVcard> = iterator.map(|tpl| tpl.1).collect();
|
|
|
|
|
2018-03-12 16:46:06 +00:00
|
|
|
match ::serde_json::to_string(&v) {
|
|
|
|
Ok(s) => writeln!(rt.stdout(), "{}", s).to_exit_code().unwrap_or_exit(),
|
|
|
|
Err(e) => {
|
|
|
|
error!("Error generating JSON: {:?}", e);
|
|
|
|
::std::process::exit(1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2018-11-07 11:48:34 +00:00
|
|
|
let output = rt.stdout();
|
|
|
|
let mut output = output.lock();
|
|
|
|
iterator
|
2018-11-04 21:08:05 +00:00
|
|
|
.map(|(i, dvcard)| build_data_object_for_handlebars(i, &dvcard))
|
|
|
|
.map(|data| list_format.render("format", &data).map_err(Error::from))
|
2019-02-05 00:37:32 +00:00
|
|
|
.trace_unwrap_exit()
|
2018-11-07 11:48:34 +00:00
|
|
|
.for_each(|s| {
|
|
|
|
writeln!(output, "{}", s).to_exit_code().unwrap_or_exit()
|
|
|
|
});
|
2018-03-12 16:46:06 +00:00
|
|
|
}
|
2017-10-10 16:48:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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() {
|
2018-10-06 10:50:12 +00:00
|
|
|
let entry = rt
|
2017-10-10 16:48:21 +00:00
|
|
|
.store()
|
2018-10-07 08:56:56 +00:00
|
|
|
.retrieve_from_path(&path)
|
2019-02-05 00:37:32 +00:00
|
|
|
.map_err_trace_exit_unwrap();
|
2018-10-06 10:50:12 +00:00
|
|
|
|
2019-02-03 18:53:50 +00:00
|
|
|
let _ = rt.report_touched(entry.get_location()).unwrap_or_exit();
|
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-10-30 17:40:53 +00:00
|
|
|
.map_err(Error::from)
|
2019-02-05 00:37:32 +00:00
|
|
|
.map_err_trace_exit_unwrap();
|
2018-10-06 10:50:12 +00:00
|
|
|
|
2017-10-10 16:48:21 +00:00
|
|
|
if entry.file_type().is_file() {
|
|
|
|
let pb = PathBuf::from(entry.path());
|
2018-10-06 10:50:12 +00:00
|
|
|
let fle = rt
|
2017-10-10 16:48:21 +00:00
|
|
|
.store()
|
2018-10-07 08:56:56 +00:00
|
|
|
.retrieve_from_path(&pb)
|
2019-02-05 00:37:32 +00:00
|
|
|
.map_err_trace_exit_unwrap();
|
2018-10-06 10:50:12 +00:00
|
|
|
|
2019-02-03 18:53:50 +00:00
|
|
|
let _ = rt.report_touched(fle.get_location()).unwrap_or_exit();
|
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) {
|
2018-04-25 11:34:46 +00:00
|
|
|
let scmd = rt.cli().subcommand_matches("show").unwrap();
|
|
|
|
let hash = scmd.value_of("hash").map(String::from).unwrap(); // safed by clap
|
|
|
|
let show_format = get_contact_print_format("contact.show_format", rt, &scmd);
|
|
|
|
let out = rt.stdout();
|
|
|
|
let mut outlock = out.lock();
|
2017-10-10 16:48:21 +00:00
|
|
|
|
2018-04-25 11:34:46 +00:00
|
|
|
rt.store()
|
|
|
|
.all_contacts()
|
2019-02-05 00:37:32 +00:00
|
|
|
.map_err_trace_exit_unwrap()
|
2018-04-25 11:34:46 +00:00
|
|
|
.into_get_iter(rt.store())
|
2019-02-05 00:37:32 +00:00
|
|
|
.trace_unwrap_exit()
|
2018-04-25 11:34:46 +00:00
|
|
|
.map(|o| o.unwrap_or_else(|| {
|
|
|
|
error!("Failed to get entry");
|
2017-10-10 16:48:21 +00:00
|
|
|
exit(1)
|
2018-04-25 11:34:46 +00:00
|
|
|
}))
|
|
|
|
.filter_map(|entry| {
|
2019-02-05 00:37:32 +00:00
|
|
|
let deser = entry.deser().map_err_trace_exit_unwrap();
|
2017-10-10 16:48:21 +00:00
|
|
|
|
2018-04-25 11:34:46 +00:00
|
|
|
if deser.uid()
|
|
|
|
.ok_or_else(|| {
|
|
|
|
error!("Could not get StoreId from Store::all_contacts(). This is a BUG!");
|
|
|
|
::std::process::exit(1)
|
|
|
|
})
|
|
|
|
.unwrap() // exited above
|
|
|
|
.starts_with(&hash)
|
|
|
|
{
|
2019-02-03 18:53:50 +00:00
|
|
|
let _ = rt.report_touched(entry.get_location()).unwrap_or_exit();
|
2018-04-25 11:34:46 +00:00
|
|
|
Some(deser)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
})
|
2018-09-27 06:09:39 +00:00
|
|
|
.enumerate()
|
|
|
|
.for_each(|(i, elem)| {
|
|
|
|
let data = build_data_object_for_handlebars(i, &elem);
|
2018-04-25 11:34:46 +00:00
|
|
|
|
|
|
|
let s = show_format
|
|
|
|
.render("format", &data)
|
2018-10-30 17:40:53 +00:00
|
|
|
.map_err(Error::from)
|
2019-02-05 00:37:32 +00:00
|
|
|
.map_err_trace_exit_unwrap();
|
2018-04-25 11:34:46 +00:00
|
|
|
let _ = writeln!(outlock, "{}", s).to_exit_code().unwrap_or_exit();
|
|
|
|
});
|
2017-10-10 16:48:21 +00:00
|
|
|
}
|
|
|
|
|
2018-03-12 14:23:37 +00:00
|
|
|
fn find(rt: &Runtime) {
|
|
|
|
let scmd = rt.cli().subcommand_matches("find").unwrap();
|
|
|
|
let grepstring = scmd
|
|
|
|
.values_of("string")
|
|
|
|
.unwrap() // safed by clap
|
|
|
|
.map(String::from)
|
|
|
|
.collect::<Vec<String>>();
|
|
|
|
|
|
|
|
// We don't know yet which we need, but we pay that price for simplicity of the codebase
|
|
|
|
let show_format = get_contact_print_format("contact.show_format", rt, &scmd);
|
|
|
|
let list_format = get_contact_print_format("contact.list_format", rt, &scmd);
|
|
|
|
|
2018-03-12 16:46:06 +00:00
|
|
|
let iterator = rt
|
|
|
|
.store()
|
2018-03-12 14:23:37 +00:00
|
|
|
.all_contacts()
|
2019-02-05 00:37:32 +00:00
|
|
|
.map_err_trace_exit_unwrap()
|
2018-03-12 14:23:37 +00:00
|
|
|
.into_get_iter(rt.store())
|
|
|
|
.map(|el| {
|
2019-02-05 00:37:32 +00:00
|
|
|
el.map_err_trace_exit_unwrap()
|
2018-03-12 14:23:37 +00:00
|
|
|
.ok_or_else(|| {
|
|
|
|
error!("Could not get StoreId from Store::all_contacts(). This is a BUG!");
|
|
|
|
::std::process::exit(1)
|
|
|
|
})
|
|
|
|
.unwrap() // safed above
|
|
|
|
})
|
2018-04-25 11:34:46 +00:00
|
|
|
.filter_map(|entry| {
|
2019-02-05 00:37:32 +00:00
|
|
|
let card = entry.deser().map_err_trace_exit_unwrap();
|
2018-03-12 14:23:37 +00:00
|
|
|
|
|
|
|
let str_contains_any = |s: &String, v: &Vec<String>| {
|
|
|
|
v.iter().any(|i| s.contains(i))
|
|
|
|
};
|
|
|
|
|
2018-04-25 11:34:46 +00:00
|
|
|
let take = card.adr().iter().any(|a| str_contains_any(a, &grepstring))
|
2018-04-27 11:48:18 +00:00
|
|
|
|| card.email().iter().any(|a| str_contains_any(&a.address, &grepstring))
|
2018-04-25 11:34:46 +00:00
|
|
|
|| card.fullname().iter().any(|a| str_contains_any(a, &grepstring));
|
2018-03-12 14:23:37 +00:00
|
|
|
|
|
|
|
if take {
|
2019-02-03 18:53:50 +00:00
|
|
|
let _ = rt.report_touched(entry.get_location()).unwrap_or_exit();
|
2018-10-06 10:50:12 +00:00
|
|
|
|
2018-03-12 14:23:37 +00:00
|
|
|
// optimization so we don't have to parse again in the next step
|
2018-04-25 11:34:46 +00:00
|
|
|
Some((entry, card))
|
2018-03-12 14:23:37 +00:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
})
|
2018-03-12 16:46:06 +00:00
|
|
|
.enumerate();
|
|
|
|
|
2019-02-15 18:13:29 +00:00
|
|
|
if !rt.output_is_pipe() || rt.ignore_ids() {
|
2019-02-03 23:01:57 +00:00
|
|
|
if scmd.is_present("json") {
|
|
|
|
let v : Vec<DeserVcard> = iterator.map(|(_, tlp)| tlp.1).collect();
|
|
|
|
|
|
|
|
match ::serde_json::to_string(&v) {
|
|
|
|
Ok(s) => writeln!(rt.stdout(), "{}", s).to_exit_code().unwrap_or_exit(),
|
|
|
|
Err(e) => {
|
|
|
|
error!("Error generating JSON: {:?}", e);
|
|
|
|
::std::process::exit(1)
|
|
|
|
}
|
2018-03-12 16:46:06 +00:00
|
|
|
}
|
2019-02-03 23:01:57 +00:00
|
|
|
} else if scmd.is_present("find-id") {
|
|
|
|
iterator
|
|
|
|
.for_each(|(_i, (entry, _))| {
|
|
|
|
writeln!(rt.stdout(), "{}", entry.get_location())
|
|
|
|
.to_exit_code()
|
|
|
|
.unwrap_or_exit();
|
|
|
|
})
|
|
|
|
} else if scmd.is_present("find-full-id") {
|
|
|
|
let storepath = rt.store().path().display();
|
|
|
|
iterator
|
|
|
|
.for_each(|(_i, (entry, _))| {
|
|
|
|
writeln!(rt.stdout(), "{}/{}", storepath, entry.get_location())
|
|
|
|
.to_exit_code()
|
|
|
|
.unwrap_or_exit();
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
iterator
|
|
|
|
.for_each(|(i, (_, card))| {
|
|
|
|
let fmt = if scmd.is_present("find-show") {
|
|
|
|
&show_format
|
|
|
|
} else if scmd.is_present("find-list") {
|
|
|
|
&list_format
|
|
|
|
} else { // default: find-list
|
|
|
|
&list_format
|
|
|
|
};
|
|
|
|
|
|
|
|
let data = build_data_object_for_handlebars(i, &card);
|
|
|
|
let s = fmt
|
|
|
|
.render("format", &data)
|
|
|
|
.map_err(Error::from)
|
2019-02-05 00:37:32 +00:00
|
|
|
.map_err_trace_exit_unwrap();
|
2018-03-12 14:23:37 +00:00
|
|
|
|
2019-02-03 23:01:57 +00:00
|
|
|
let _ = writeln!(rt.stdout(), "{}", s)
|
|
|
|
.to_exit_code()
|
|
|
|
.unwrap_or_exit();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
} else { // if not printing, we still have to consume the iterator to report the touched IDs
|
|
|
|
let _ = iterator.collect::<Vec<_>>();
|
2018-03-12 16:46:06 +00:00
|
|
|
}
|
2018-03-12 14:23:37 +00:00
|
|
|
}
|
|
|
|
|
2017-10-10 16:48:21 +00:00
|
|
|
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()
|
2018-10-30 17:40:53 +00:00
|
|
|
.ok_or_else(|| Error::from(err_msg("No configuration file")))
|
2019-02-05 00:37:32 +00:00
|
|
|
.map_err_trace_exit_unwrap()
|
2018-01-12 15:30:09 +00:00
|
|
|
.read_string(config_value_path)
|
2018-10-30 17:40:53 +00:00
|
|
|
.map_err(Error::from)
|
2019-02-05 00:37:32 +00:00
|
|
|
.map_err_trace_exit_unwrap()
|
2018-10-30 17:40:53 +00:00
|
|
|
.ok_or_else(|| Error::from(err_msg("Configuration 'contact.list_format' does not exist")))
|
2019-02-05 00:37:32 +00:00
|
|
|
.map_err_trace_exit_unwrap()
|
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-10-30 17:40:53 +00:00
|
|
|
.map_err(Error::from)
|
2019-02-05 00:37:32 +00:00
|
|
|
.map_err_trace_exit_unwrap();
|
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
|
|
|
}
|
|
|
|
|