summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMatthias Beyer <mail@beyermatthias.de>2018-04-07 10:51:10 +0200
committerGitHub <noreply@github.com>2018-04-07 10:51:10 +0200
commit2f47becef4aec3cf4d06d2531b94536f8f1e81b0 (patch)
tree02aca992ac559b3445bf566d4af76d7dc5e4e1c9
parenta14ab7b6354e3916ac15f1e3b63d2b540db5d7d8 (diff)
parent5061faaa748836920863e0d59976474985566d0b (diff)
downloadimag-2f47becef4aec3cf4d06d2531b94536f8f1e81b0.zip
imag-2f47becef4aec3cf4d06d2531b94536f8f1e81b0.tar.gz
Merge pull request #1378 from matthiasbeyer/imag-view/ids-from-stdin
imag-view: Add support for piping entry ids into command
-rw-r--r--bin/core/imag-view/src/main.rs37
-rw-r--r--bin/core/imag-view/src/ui.rs25
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 831e4d5..69bbdb0 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<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)
diff --git a/bin/core/imag-view/src/ui.rs b/bin/core/imag-view/src/ui.rs
index 4659694..ef0be8a 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")