Add support for looping over edit

This patch adds a imag-contact feature where we loop as long as we
cannot parse the vcard. If the `--no-retry` flag is passed, we fail immediately.

Signed-off-by: Matthias Beyer <mail@beyermatthias.de>
This commit is contained in:
Matthias Beyer 2019-05-11 12:25:41 +02:00
parent aedea111c5
commit 8b0f187f7d
2 changed files with 62 additions and 25 deletions

View file

@ -33,16 +33,21 @@
)] )]
use std::process::exit; use std::process::exit;
use std::io::Read;
use std::io::Write;
use failure::Error; use failure::Error;
use failure::err_msg; use failure::err_msg;
use failure::Fallible as Result;
use libimagrt::runtime::Runtime; use libimagrt::runtime::Runtime;
use libimagerror::trace::MapErrTrace; use libimagerror::trace::MapErrTrace;
use libimagstore::store::FileLockEntry;
use libimagcontact::store::ContactStore; use libimagcontact::store::ContactStore;
use libimagentryref::reference::fassade::RefFassade; use libimagentryref::reference::fassade::RefFassade;
use libimagentryref::hasher::default::DefaultHasher; use libimagentryref::hasher::default::DefaultHasher;
use libimagentryref::reference::Ref; use libimagentryref::reference::Ref;
use libimagentryref::reference::Config as RefConfig;
const TEMPLATE : &'static str = include_str!("../static/new-contact-template.toml"); const TEMPLATE : &'static str = include_str!("../static/new-contact-template.toml");
@ -52,17 +57,40 @@ pub fn edit(rt: &Runtime) {
let ref_config = libimagentryref::util::get_ref_config(&rt, "imag-contact").map_err_trace_exit_unwrap(); let ref_config = libimagentryref::util::get_ref_config(&rt, "imag-contact").map_err_trace_exit_unwrap();
let hash = scmd.value_of("hash").map(String::from).unwrap(); // safed by clap let hash = scmd.value_of("hash").map(String::from).unwrap(); // safed by clap
let force_override = true; // when editing, we want to override, right? let force_override = true; // when editing, we want to override, right?
let retry = !scmd.is_present("fail-on-parse-error");
if rt.output_is_pipe() { if rt.output_is_pipe() {
error!("Cannot spawn editor if output is a pipe!"); error!("Cannot spawn editor if output is a pipe!");
exit(1); exit(1);
} }
let mut output = rt.stdout();
let mut input = rt.stdin().unwrap_or_else(|| {
error!("No input stream. Cannot ask for permission.");
exit(1)
});
::util::find_contact_by_hash(rt, hash) ::util::find_contact_by_hash(rt, hash)
.for_each(|contact| { .for_each(|contact| {
loop {
let res = edit_contact(&rt, &contact, &ref_config, collection_name, force_override);
if !retry {
let _ = res.map_err_trace_exit_unwrap();
} else {
if ask_continue(&mut input, &mut output) {
continue;
} else {
exit(1)
}
}
}
});
}
fn edit_contact<'a>(rt: &Runtime, contact: &FileLockEntry<'a>, ref_config: &RefConfig, collection_name: &str, force_override: bool) -> Result<()> {
let filepath = contact let filepath = contact
.as_ref_with_hasher::<DefaultHasher>() .as_ref_with_hasher::<DefaultHasher>()
.get_path(&ref_config) .get_path(ref_config)
.map_err_trace_exit_unwrap(); .map_err_trace_exit_unwrap();
let success = rt.editor() let success = rt.editor()
@ -82,10 +110,13 @@ pub fn edit(rt: &Runtime) {
exit(1); exit(1);
} }
let _ = rt rt.store()
.store()
.retrieve_from_path(&filepath, &ref_config, &collection_name, force_override) .retrieve_from_path(&filepath, &ref_config, &collection_name, force_override)
.map_err_trace_exit_unwrap(); .map(|_| ())
}); }
fn ask_continue(inputstream: &mut Read, outputstream: &mut Write) -> bool {
::libimaginteraction::ask::ask_bool("Edit vcard", Some(true), inputstream, outputstream)
.map_err_trace_exit_unwrap()
} }

View file

@ -102,6 +102,12 @@ pub fn build_ui<'a>(app: App<'a, 'a>) -> App<'a, 'a> {
.multiple(true) .multiple(true)
.value_name("HASH") .value_name("HASH")
.help("Edit the contact pointed to by this reference hash(es)")) .help("Edit the contact pointed to by this reference hash(es)"))
.arg(Arg::with_name("fail-on-parse-error")
.long("no-retry")
.takes_value(false)
.required(false)
.multiple(false)
.help("Fail immediately if vcard cannot be parsed after editing."))
) )
.subcommand(SubCommand::with_name("find") .subcommand(SubCommand::with_name("find")