Allow reading store ids from stdin

This commit is contained in:
Matthias Beyer 2018-03-12 19:47:27 +01:00
parent 1bd91cfd0b
commit 8ae6e735e7
2 changed files with 50 additions and 17 deletions

View file

@ -42,12 +42,15 @@ extern crate libimagstore;
extern crate libimagutil; extern crate libimagutil;
use std::path::PathBuf; use std::path::PathBuf;
use std::io::Read;
use libimagentryedit::edit::*;
use libimagentryedit::error::EditError as EE;
use libimagerror::trace::MapErrTrace; use libimagerror::trace::MapErrTrace;
use libimagerror::iter::TraceIterator;
use libimagentryedit::edit::Edit;
use libimagrt::setup::generate_runtime_setup; use libimagrt::setup::generate_runtime_setup;
use libimagstore::storeid::IntoStoreId; use libimagstore::storeid::IntoStoreId;
use libimagstore::storeid::StoreIdIterator;
use libimagstore::iter::get::StoreIdGetIteratorExtension;
mod ui; mod ui;
@ -58,18 +61,28 @@ fn main() {
"Edit store entries with $EDITOR", "Edit store entries with $EDITOR",
ui::build_ui); ui::build_ui);
let mut entry = { let sids = match rt.cli().value_of("entry") {
let path = rt.cli() Some(path) => vec![PathBuf::from(path).into_storeid().map_err_trace_exit_unwrap(1)],
.value_of("entry") None => if rt.cli().is_present("entries-from-stdin") {
.unwrap(); // safe by clap let stdin = rt.stdin().unwrap_or_else(|| {
error!("Cannot get handle to stdin");
::std::process::exit(1)
});
let sid = PathBuf::from(path).into_storeid().map_err_trace_exit_unwrap(1); let mut buf = String::new();
let _ = stdin.lock().read_to_string(&mut buf).unwrap_or_else(|_| {
error!("Failed to read from stdin");
::std::process::exit(1)
});
rt.store() buf.lines()
.get(sid) .map(PathBuf::from)
.map_err_trace_exit_unwrap(1) .map(|p| p.into_storeid().map_err_trace_exit_unwrap(1))
.ok_or(EE::from(format!("Entry {} does not exist", path))) .collect()
.map_err_trace_exit_unwrap(1) } else {
error!("Something weird happened. I was not able to find the path of the entries to edit");
::std::process::exit(1)
}
}; };
if rt.cli().is_present("edit-header") { if rt.cli().is_present("edit-header") {
@ -78,8 +91,17 @@ fn main() {
::std::process::exit(1); ::std::process::exit(1);
} }
let _ = entry StoreIdIterator::new(Box::new(sids.into_iter()))
.edit_content(&rt) .into_get_iter(rt.store())
.map_err_trace_exit_unwrap(1); .trace_unwrap_exit(1)
.map(|o| o.unwrap_or_else(|| {
error!("Did not find one entry");
::std::process::exit(1)
}))
.for_each(|mut entry| {
let _ = entry
.edit_content(&rt)
.map_err_trace_exit_unwrap(1);
});
} }

View file

@ -17,17 +17,28 @@
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
// //
use clap::{Arg, App}; use clap::{Arg, ArgGroup, App};
pub fn build_ui<'a>(app: App<'a, 'a>) -> App<'a, 'a> { pub fn build_ui<'a>(app: App<'a, 'a>) -> App<'a, 'a> {
app app
.arg(Arg::with_name("entry") .arg(Arg::with_name("entry")
.index(1) .index(1)
.takes_value(true) .takes_value(true)
.required(true) .required(false)
.multiple(true) .multiple(true)
.help("The entry/entries to edit") .help("The entry/entries to edit")
.value_name("ENTRY")) .value_name("ENTRY"))
.arg(Arg::with_name("entries-from-stdin")
.long("ids-from-stdin")
.short("I")
.takes_value(false)
.required(false)
.multiple(false)
.help("The entry/entries are piped in via stdin"))
.group(ArgGroup::with_name("input-method")
.args(&["entry", "entries-from-stdin"])
.required(true))
.arg(Arg::with_name("edit-header") .arg(Arg::with_name("edit-header")
.long("header") .long("header")
.short("H") .short("H")