~matthiasbeyer/imag

d8bb1f5bf0266a5826fecd55eb746d1619c50f95 — Matthias Beyer 1 year, 1 month ago 4c9befd
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 <mail@beyermatthias.de>
1 files changed, 43 insertions(+), 26 deletions(-)

M lib/core/libimagrt/src/runtime.rs
M lib/core/libimagrt/src/runtime.rs => lib/core/libimagrt/src/runtime.rs +43 -26
@@ 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<Stdin> {
        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<bool> {
        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 {