Add support for piping entry ids into command

This commit is contained in:
Matthias Beyer 2018-04-06 15:40:34 +02:00
parent 1d5171d770
commit 5061faaa74
2 changed files with 48 additions and 14 deletions

View File

@ -46,6 +46,7 @@ extern crate libimagstore;
use std::collections::BTreeMap;
use std::io::Write;
use std::io::Read;
use std::path::PathBuf;
use std::process::Command;
use std::process::exit;
@ -187,13 +188,35 @@ fn main() {
}
fn entry_ids(rt: &Runtime) -> Vec<StoreId> {
rt.cli()
.values_of("id")
.unwrap() // enforced by clap
.map(PathBuf::from)
.map(PathBuf::into_storeid)
.trace_unwrap_exit(1)
.collect()
match rt.cli().values_of("id") {
Some(pathes) => pathes
.map(PathBuf::from)
.map(PathBuf::into_storeid)
.trace_unwrap_exit(1)
.collect(),
None => if rt.cli().is_present("entries-from-stdin") {
let stdin = rt.stdin().unwrap_or_else(|| {
error!("Cannot get handle to stdin");
::std::process::exit(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)
});
buf.lines()
.map(PathBuf::from)
.map(PathBuf::into_storeid)
.trace_unwrap_exit(1)
.collect()
} else {
error!("Something weird happened. I was not able to find the path of the entries to edit");
::std::process::exit(1)
}
}
}
fn create_tempfile_for<'a>(entry: &FileLockEntry<'a>, view_header: bool, hide_content: bool)

View File

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