From e820f8bfb228c493bc21a67a0ece5e582d7e3e75 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Thu, 19 Apr 2018 22:06:53 +0200 Subject: [PATCH] Add functionality to read storeids from stdin --- bin/core/imag-tag/src/main.rs | 26 +++++++++++++++++++++++++- bin/core/imag-tag/src/ui.rs | 8 ++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/bin/core/imag-tag/src/main.rs b/bin/core/imag-tag/src/main.rs index 8a131f8d..f07b8b25 100644 --- a/bin/core/imag-tag/src/main.rs +++ b/bin/core/imag-tag/src/main.rs @@ -57,6 +57,7 @@ extern crate env_logger; use std::path::PathBuf; use std::io::Write; +use std::io::Read; use libimagrt::runtime::Runtime; use libimagrt::setup::generate_runtime_setup; @@ -82,7 +83,30 @@ fn main() { "Direct interface to the store. Use with great care!", build_ui); - let ids = rt.cli().values_of("id").unwrap().map(PathBuf::from); // enforced by clap + let ids : Vec = rt + .cli() + .values_of("id") + .map(|vals| { + vals.map(PathBuf::from).collect() + }).unwrap_or_else(|| { + if !rt.cli().is_present("ids-from-stdin") { + error!("No ids"); + ::std::process::exit(1) + } + + 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).collect() + }); rt.cli() .subcommand_name() diff --git a/bin/core/imag-tag/src/ui.rs b/bin/core/imag-tag/src/ui.rs index 40643d40..ec1a565f 100644 --- a/bin/core/imag-tag/src/ui.rs +++ b/bin/core/imag-tag/src/ui.rs @@ -30,6 +30,14 @@ pub fn build_ui<'a>(app: App<'a, 'a>) -> App<'a, 'a> { .value_name("ID") .help("Entry to use")) + .arg(Arg::with_name("ids-from-stdin") + .long("ids-from-stdin") + .short("I") + .takes_value(false) + .required(false) + .multiple(false) + .help("Read store ids to tag from stdin")) + .subcommand(SubCommand::with_name("add") .about("Add tags") .version("0.1")