From d8bb1f5bf0266a5826fecd55eb746d1619c50f95 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Sun, 1 Mar 2020 14:13:12 +0100 Subject: Allow more fine-granular control over IO behaviour in runtime This patch removes the --ignore-ids option and adds --pipe-input / -I and --pipe-output / -O instead. The behaviour of -IO is the same as --ignore-ids was before, but this change gives the user the option to enable only one of both (input or output, or both). Signed-off-by: Matthias Beyer --- lib/core/libimagrt/src/runtime.rs | 69 ++++++++++++++++++++++++--------------- 1 file changed, 43 insertions(+), 26 deletions(-) diff --git a/lib/core/libimagrt/src/runtime.rs b/lib/core/libimagrt/src/runtime.rs index eff1fcbd..4522a1f0 100644 --- a/lib/core/libimagrt/src/runtime.rs +++ b/lib/core/libimagrt/src/runtime.rs @@ -60,7 +60,8 @@ pub struct Runtime<'a> { has_output_pipe: bool, has_input_pipe: bool, - ignore_ids: bool + input_data: bool, + output_data: bool, } impl<'a> Runtime<'a> { @@ -140,11 +141,13 @@ impl<'a> Runtime<'a> { let has_output_pipe = !atty::is(atty::Stream::Stdout); let has_input_pipe = !atty::is(atty::Stream::Stdin); - let ignore_ids = matches.is_present("ignore-ids"); + let input_data = matches.is_present("input-pipe-data"); + let output_data = matches.is_present("output-pipe-data"); - debug!("has output pipe = {}", has_output_pipe); - debug!("has input pipe = {}", has_input_pipe); - debug!("ignore ids = {}", ignore_ids); + 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); store_result.map(|store| Runtime { cli_matches: matches, @@ -154,7 +157,8 @@ impl<'a> Runtime<'a> { has_output_pipe, has_input_pipe, - ignore_ids, + input_data, + output_data, }) .context(err_msg("Cannot instantiate runtime")) .map_err(Error::from) @@ -238,12 +242,23 @@ impl<'a> Runtime<'a> { .required(false) .takes_value(true)) - .arg(Arg::with_name("ignore-ids") - .long("ignore-ids") - .help("Do not assume that the output is a pipe to another imag command. This overrides the default behaviour where imag only prints the IDs of the touched entries to stdout if stdout is a pipe.") - .long_help("Without this flag, imag assumes that if stdout is a pipe, the command imag pipes to is also an imag command. Thus, it prints the IDs of the processed entries to stdout and automatically redirects the command output to stderr. By providing this flag, this behaviour gets overridden: The IDs are not printed at all and the normal output is printed to stdout.") - .required(false) - .takes_value(false)) + .arg(Arg::with_name("input-pipe-data") + .long("pipe-input") + .short("I") + .required(false) + .takes_value(false) + .multiple(false) + .help("Do not expect imag ids on stdin if stdin is a pipe.") + ) + + .arg(Arg::with_name("output-pipe-data") + .long("pipe-output") + .short("O") + .required(false) + .takes_value(false) + .multiple(false) + .help("Do not print imag ids to stdout if stdout is a pipe.") + ) } @@ -406,24 +421,26 @@ impl<'a> Runtime<'a> { self.has_input_pipe } - /// Alias for Runtime::input_is_pipe() + /// Check whether the runtime expects imag ids from stdin pub fn ids_from_stdin(&self) -> bool { - self.input_is_pipe() + self.input_is_pipe() && !self.input_data } + /// Check whether the runtime allows data to be piped into the program + pub fn input_data_pipe(&self) -> bool { + self.input_is_pipe() && self.input_data + } - /// Check whether the runtime ignores touched ids - /// - /// "Ignoring" in this context means whether the runtime prints them or not. - pub fn ignore_ids(&self) -> bool { - self.ignore_ids + /// Check whether the runtime allows data to be piped into the program + pub fn output_data_pipe(&self) -> bool { + self.output_is_pipe() && self.output_data } pub fn stdout(&self) -> OutputProxy { - if self.output_is_pipe() && !self.ignore_ids { - OutputProxy::Err(::std::io::stderr()) - } else { + if self.output_is_pipe() && self.output_data { OutputProxy::Out(::std::io::stdout()) + } else { + OutputProxy::Err(::std::io::stderr()) } } @@ -432,10 +449,10 @@ impl<'a> Runtime<'a> { } pub fn stdin(&self) -> Option { - if self.has_input_pipe { - None - } else { + if self.input_is_pipe() && self.input_data { Some(::std::io::stdin()) + } else { + None } } @@ -545,7 +562,7 @@ impl<'a> Runtime<'a> { fn report_touched_id(&self, id: &StoreId, output: &mut StdoutLock) -> Result { use std::io::Write; - if self.output_is_pipe() && !self.ignore_ids { + if self.output_is_pipe() && !self.output_data { trace!("Reporting: {} to {:?}", id, output); if let Err(e) = writeln!(output, "{}", id) { return if e.kind() == std::io::ErrorKind::BrokenPipe { -- cgit v1.2.3 From a69965c359e4401cce2c480002ecd5ce79284ab8 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Sun, 1 Mar 2020 14:22:39 +0100 Subject: Use more fine-granular function as Runtime::ignore_ids() was removed Signed-off-by: Matthias Beyer --- bin/domain/imag-contact/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bin/domain/imag-contact/src/lib.rs b/bin/domain/imag-contact/src/lib.rs index a95006cb..dcfaeb26 100644 --- a/bin/domain/imag-contact/src/lib.rs +++ b/bin/domain/imag-contact/src/lib.rs @@ -318,7 +318,7 @@ fn find(rt: &Runtime) -> Result<()> { let mut i = 0; - if !rt.output_is_pipe() || rt.ignore_ids() { + if !rt.output_is_pipe() || rt.output_data_pipe() { if scmd.is_present("json") { iterator .filter_ok(|tpl| tpl.0) -- cgit v1.2.3 From 4834bc88e45beddcee6903691a483e59b10e19f7 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Sun, 1 Mar 2020 15:01:29 +0100 Subject: Adapt flag forwarding to new runtime flags Signed-off-by: Matthias Beyer --- bin/core/imag/src/main.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/bin/core/imag/src/main.rs b/bin/core/imag/src/main.rs index d08f2191..ae61e985 100644 --- a/bin/core/imag/src/main.rs +++ b/bin/core/imag/src/main.rs @@ -360,6 +360,7 @@ fn forward_commandline_arguments(m: &ArgMatches, scmd: &mut Vec) { push(Some("rtp"), "runtimepath", m , scmd); push(Some("store"), "storepath", m , scmd); push(Some("editor"), "editor", m , scmd); - push(Some("ignore-ids"), "ignore-ids", m , scmd); + push(Some("pipe-input"), "pipe-input", m , scmd); + push(Some("pipe-output"), "pipe-output", m , scmd); } -- cgit v1.2.3 From 8e04277f6a558766ffe3698c10816c825a5bfd78 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Sun, 1 Mar 2020 15:01:38 +0100 Subject: Update docs for new runtime flags Signed-off-by: Matthias Beyer --- doc/user/src/conventions.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/user/src/conventions.md b/doc/user/src/conventions.md index 2bf61acf..4e6de9ac 100644 --- a/doc/user/src/conventions.md +++ b/doc/user/src/conventions.md @@ -22,7 +22,7 @@ the following ways: * If the standard output is not a pipe, the imag module does not print the StoreIDs it touched. -This behaviour can be overridden with the `--ignore-ids` flag. +This behaviour can be overridden with the `--pipe-input` and `--pipe-output` flag. ## Versioning -- cgit v1.2.3 From b23b81658c411508b2d5acaf3db097654f370292 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Sun, 1 Mar 2020 15:01:50 +0100 Subject: Update tests for new runtime flags Signed-off-by: Matthias Beyer --- tests/ui/src/imag_category.rs | 8 ++++++-- tests/ui/src/imag_grep.rs | 3 ++- tests/ui/src/imag_header.rs | 2 +- tests/ui/src/imag_mv.rs | 3 ++- tests/ui/src/imag_tag.rs | 3 ++- tests/ui/src/imag_view.rs | 3 ++- 6 files changed, 15 insertions(+), 7 deletions(-) diff --git a/tests/ui/src/imag_category.rs b/tests/ui/src/imag_category.rs index 0f865c5d..268bc8a2 100644 --- a/tests/ui/src/imag_category.rs +++ b/tests/ui/src/imag_category.rs @@ -28,7 +28,8 @@ pub fn binary(tempdir: &TempDir) -> Command { pub fn call(tmpdir: &TempDir, args: &[&str]) -> Vec { let mut binary = binary(tmpdir); binary.stdin(std::process::Stdio::inherit()); - binary.arg("--ignore-ids"); + binary.arg("--pipe-input"); + binary.arg("--pipe-output"); binary.args(args); debug!("Command = {:?}", binary); crate::imag::stdout_of_command(binary) @@ -44,7 +45,10 @@ fn test_new_entry_has_no_category() { let (assert, stderr_output) = { let mut binary = binary(&imag_home); binary.stdin(std::process::Stdio::inherit()); - binary.args(&["--ignore-ids", "get", "test"]); + binary.arg("--pipe-input"); + binary.arg("--pipe-output"); + binary.arg("get"); + binary.arg("test"); crate::imag::stderr_of_command(&mut binary) }; diff --git a/tests/ui/src/imag_grep.rs b/tests/ui/src/imag_grep.rs index 498bf72f..00b0a602 100644 --- a/tests/ui/src/imag_grep.rs +++ b/tests/ui/src/imag_grep.rs @@ -24,7 +24,8 @@ use assert_fs::fixture::TempDir; pub fn call(tempdir: &TempDir, pattern: &str) -> Vec { let mut binary = binary(tempdir); - binary.arg("--ignore-ids"); + binary.arg("--pipe-input"); + binary.arg("--pipe-output"); binary.arg(pattern); // ensure that stdin is not used by the child process diff --git a/tests/ui/src/imag_header.rs b/tests/ui/src/imag_header.rs index 1ae43bb4..2789c366 100644 --- a/tests/ui/src/imag_header.rs +++ b/tests/ui/src/imag_header.rs @@ -56,7 +56,7 @@ fn test_imag_version_as_semver_string() { crate::imag_init::call(&imag_home); crate::imag_create::call(&imag_home, &["test"]); - let output = call(&imag_home, &["--ignore-ids", "test", "read", "imag.version"]); + let output = call(&imag_home, &["--pipe-input", "--pipe-output", "test", "read", "imag.version"]); let version = version::version!(); debug!("output = {:?}", output); assert_eq!(output.len(), 1); diff --git a/tests/ui/src/imag_mv.rs b/tests/ui/src/imag_mv.rs index 3183097b..4155bf71 100644 --- a/tests/ui/src/imag_mv.rs +++ b/tests/ui/src/imag_mv.rs @@ -30,7 +30,8 @@ pub fn binary(tempdir: &TempDir) -> Command { pub fn call(tmpdir: &TempDir, src: &str, dst: &str) { let mut binary = binary(tmpdir); binary.stdin(std::process::Stdio::inherit()); - binary.arg("--ignore-ids"); + binary.arg("--pipe-input"); + binary.arg("--pipe-output"); binary.arg(src); binary.arg(dst); diff --git a/tests/ui/src/imag_tag.rs b/tests/ui/src/imag_tag.rs index f254db15..2f86496a 100644 --- a/tests/ui/src/imag_tag.rs +++ b/tests/ui/src/imag_tag.rs @@ -28,7 +28,8 @@ pub fn binary(tempdir: &TempDir) -> Command { pub fn call(tmpdir: &TempDir, args: &[&str]) -> Vec { let mut binary = binary(tmpdir); binary.stdin(std::process::Stdio::inherit()); - binary.arg("--ignore-ids"); + binary.arg("--pipe-input"); + binary.arg("--pipe-output"); binary.args(args); debug!("Command = {:?}", binary); crate::imag::stdout_of_command(binary) diff --git a/tests/ui/src/imag_view.rs b/tests/ui/src/imag_view.rs index 498a53c3..b749cdc2 100644 --- a/tests/ui/src/imag_view.rs +++ b/tests/ui/src/imag_view.rs @@ -28,7 +28,8 @@ pub fn call(tempdir: &TempDir, targets: &[&str]) -> Vec { // ensure that stdin is not used by the child process binary.stdin(std::process::Stdio::inherit()); - binary.arg("--ignore-ids"); + binary.arg("--pipe-input"); + binary.arg("--pipe-output"); for target in targets.iter() { binary.arg(target); -- cgit v1.2.3