From 25bcb61319ea172f4fb8028586239b2fa2fd221f Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Mon, 23 Mar 2020 15:22:15 +0100 Subject: Add blackhole as default cli option Signed-off-by: Matthias Beyer --- lib/core/libimagrt/Cargo.toml | 1 + lib/core/libimagrt/src/lib.rs | 1 + lib/core/libimagrt/src/runtime.rs | 35 ++++++++++++++++++++++++++++++----- 3 files changed, 32 insertions(+), 5 deletions(-) diff --git a/lib/core/libimagrt/Cargo.toml b/lib/core/libimagrt/Cargo.toml index d6792b51..fc1a2d29 100644 --- a/lib/core/libimagrt/Cargo.toml +++ b/lib/core/libimagrt/Cargo.toml @@ -27,6 +27,7 @@ itertools = "0.8.0" ansi_term = "0.12" atty = "0.2.11" anyhow = "1" +indoc = "0.3" serde_derive = "1.0.94" serde = "1.0.94" diff --git a/lib/core/libimagrt/src/lib.rs b/lib/core/libimagrt/src/lib.rs index c36e2d01..224fa361 100644 --- a/lib/core/libimagrt/src/lib.rs +++ b/lib/core/libimagrt/src/lib.rs @@ -44,6 +44,7 @@ extern crate handlebars; extern crate serde; #[macro_use] extern crate anyhow; #[macro_use] extern crate toml_query; +#[macro_use] extern crate indoc; extern crate clap; extern crate toml; diff --git a/lib/core/libimagrt/src/runtime.rs b/lib/core/libimagrt/src/runtime.rs index 90ad0920..aaa89772 100644 --- a/lib/core/libimagrt/src/runtime.rs +++ b/lib/core/libimagrt/src/runtime.rs @@ -61,6 +61,7 @@ pub struct Runtime<'a> { input_data: bool, output_data: bool, + blackhole_stdout: bool, } impl<'a> Runtime<'a> { @@ -138,15 +139,17 @@ impl<'a> Runtime<'a> { Store::new(storepath, &config) }; - let has_output_pipe = !atty::is(atty::Stream::Stdout); - let has_input_pipe = !atty::is(atty::Stream::Stdin); - let input_data = matches.is_present("input-pipe-data"); - let output_data = matches.is_present("output-pipe-data"); + let has_output_pipe = !atty::is(atty::Stream::Stdout); + let has_input_pipe = !atty::is(atty::Stream::Stdin); + let input_data = matches.is_present("input-pipe-data"); + let output_data = matches.is_present("output-pipe-data"); + let blackhole_stdout = matches.is_present("blackhole-stdout"); debug!("has output pipe = {}", has_output_pipe); debug!("has input pipe = {}", has_input_pipe); debug!("input pipe data = {}", input_data); debug!("output pipe data = {}", output_data); + debug!("blackhole output = {}", blackhole_stdout); store_result.map(|store| Runtime { cli_matches: matches, @@ -158,6 +161,7 @@ impl<'a> Runtime<'a> { has_input_pipe, input_data, output_data, + blackhole_stdout, }) .context(anyhow!("Cannot instantiate runtime")) .map_err(Error::from) @@ -257,7 +261,22 @@ impl<'a> Runtime<'a> { .takes_value(false) .multiple(false) .help("Do not print imag ids to stdout if stdout is a pipe.") - ) + ) + + .arg(Arg::with_name("blackhole-stdout") + .long("blackhole") + .short("B") + .required(false) + .takes_value(false) + .multiple(false) + .help("Do not print normal output") + .long_help(indoc!(r#" + This flag can be used to ignore output. + For example, if imag is used in a piping context, which pipes the Store IDs, the + normal output is automatically redirected to stderr. To ignore the normal output + instead, this flag can be passed. + "#)) + ) } @@ -420,6 +439,10 @@ impl<'a> Runtime<'a> { self.has_input_pipe } + pub fn output_is_blackholed(&self) -> bool { + self.blackhole_stdout + } + /// Check whether the runtime expects imag ids from stdin pub fn ids_from_stdin(&self) -> bool { self.input_is_pipe() && !self.input_data @@ -438,6 +461,8 @@ impl<'a> Runtime<'a> { pub fn stdout(&self) -> OutputProxy { if self.output_is_pipe() && self.output_data { OutputProxy::Out(::std::io::stdout()) + } else if self.output_is_blackholed() { + OutputProxy::Sink } else { OutputProxy::Err(::std::io::stderr()) } -- cgit v1.2.3