From 5061faaa748836920863e0d59976474985566d0b Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Fri, 6 Apr 2018 15:40:34 +0200 Subject: [PATCH] Add support for piping entry ids into command --- bin/core/imag-view/src/main.rs | 37 +++++++++++++++++++++++++++------- bin/core/imag-view/src/ui.rs | 25 ++++++++++++++++------- 2 files changed, 48 insertions(+), 14 deletions(-) diff --git a/bin/core/imag-view/src/main.rs b/bin/core/imag-view/src/main.rs index 831e4d50..69bbdb00 100644 --- a/bin/core/imag-view/src/main.rs +++ b/bin/core/imag-view/src/main.rs @@ -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 { - 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) diff --git a/bin/core/imag-view/src/ui.rs b/bin/core/imag-view/src/ui.rs index 46596943..ef0be8a1 100644 --- a/bin/core/imag-view/src/ui.rs +++ b/bin/core/imag-view/src/ui.rs @@ -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")