diff options
author | Matthias Beyer <mail@beyermatthias.de> | 2018-04-20 09:23:49 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-04-20 09:23:49 +0200 |
commit | 705c6a33f198a6d3cecceb902913b77898393745 (patch) | |
tree | 75a1d8318eb6209a138c80a6fba4631bc64f24b0 | |
parent | bed49b6ece5ef67689472e0eb7c4cbfc3167f5e0 (diff) | |
parent | e820f8bfb228c493bc21a67a0ece5e582d7e3e75 (diff) |
Merge pull request #1420 from matthiasbeyer/imag-tag/read-ids-from-stdin
imag-tag: Read ids from stdin
-rw-r--r-- | bin/core/imag-tag/src/main.rs | 35 | ||||
-rw-r--r-- | bin/core/imag-tag/src/ui.rs | 12 |
2 files changed, 41 insertions, 6 deletions
diff --git a/bin/core/imag-tag/src/main.rs b/bin/core/imag-tag/src/main.rs index d291af69..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,19 +83,45 @@ fn main() { "Direct interface to the store. Use with great care!", build_ui); - let id = rt.cli().value_of("id").map(PathBuf::from).unwrap(); // enforced by clap + let ids : Vec<PathBuf> = 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() .map(|name| match name { - "list" => list(id, &rt), - "remove" => { + "list" => for id in ids { + list(id, &rt) + }, + "remove" => for id in ids { let id = PathBuf::from(id); let add = None; let rem = get_remove_tags(rt.cli()); debug!("id = {:?}, add = {:?}, rem = {:?}", id, add, rem); alter(&rt, id, add, rem); }, - "add" => { + "add" => for id in ids { let id = PathBuf::from(id); let add = get_add_tags(rt.cli()); let rem = None; diff --git a/bin/core/imag-tag/src/ui.rs b/bin/core/imag-tag/src/ui.rs index 48c1bd11..ec1a565f 100644 --- a/bin/core/imag-tag/src/ui.rs +++ b/bin/core/imag-tag/src/ui.rs @@ -25,11 +25,19 @@ pub fn build_ui<'a>(app: App<'a, 'a>) -> App<'a, 'a> { app.arg(Arg::with_name("id") .index(1) .takes_value(true) - .required(true) - .multiple(false) + .required(false) + .multiple(true) .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") |