Skip to content

Commit

Permalink
change stdin to piped (#9)
Browse files Browse the repository at this point in the history
* change stdin to piped

* more io options
  • Loading branch information
dakom authored and sagiegurari committed Jan 6, 2020
1 parent 09d9a0e commit abab157
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 13 deletions.
4 changes: 2 additions & 2 deletions examples/example.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
extern crate run_script;

use run_script::ScriptOptions;
use run_script::{ScriptOptions, IoOptions};

fn main() {
let mut options = ScriptOptions::new();
options.runner = None; // The script runner, for example bash. By default for windows it's cmd.exe and for other systems it is sh.
options.capture_output = true; // True to capture and return the output. False will print it to the parent process output.
options.capture_output = IoOptions::Inherit; // Inherit to capture and return the output. Default is Pipe and will print it to the parent process output.
options.exit_on_error = false; // Adds set -e option (not available for windows)
options.print_commands = false; // Adds set -x option (not available for windows)

Expand Down
3 changes: 3 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,9 @@ pub type ScriptError = types::ScriptError;
/// Options available for invoking the script
pub type ScriptOptions = types::ScriptOptions;

/// Io Options available for invoking the script
pub type IoOptions = types::IoOptions;

/// Invokes the provided script content and returns the invocation output.
///
/// # Arguments
Expand Down
19 changes: 12 additions & 7 deletions src/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#[path = "./runner_test.rs"]
mod runner_test;

use crate::types::{ErrorInfo, ScriptError, ScriptOptions};
use crate::types::{ErrorInfo, ScriptError, ScriptOptions, IoOptions};
use rand::distributions::Alphanumeric;
use rand::{thread_rng, Rng};
use std::env;
Expand Down Expand Up @@ -45,12 +45,17 @@ fn create_command_builder(
command.arg(arg);
}

command.stdin(Stdio::inherit());
if options.capture_output {
command.stdout(Stdio::piped()).stderr(Stdio::piped());
} else {
command.stdout(Stdio::inherit()).stderr(Stdio::inherit());
}
match options.capture_input {
IoOptions::Null => command.stdin(Stdio::null()),
IoOptions::Inherit => command.stdin(Stdio::inherit()),
IoOptions::Pipe => command.stdin(Stdio::piped()),
};

match options.capture_output {
IoOptions::Null => command.stdout(Stdio::null()).stderr(Stdio::null()),
IoOptions::Inherit => command.stdout(Stdio::inherit()).stderr(Stdio::inherit()),
IoOptions::Pipe => command.stdout(Stdio::piped()).stderr(Stdio::piped()),
};

command
}
Expand Down
20 changes: 17 additions & 3 deletions src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,20 +61,34 @@ impl Display for ScriptError {
pub struct ScriptOptions {
/// Defines the requested runner (defaults to cmd in windows and sh for other platforms)
pub runner: Option<String>,
/// False to print the output to the parent process, or capture and return the output (default)
pub capture_output: bool,
/// Default is IoOptions::Pipe
pub capture_output: IoOptions,
/// Default is IoOptions::Inherit
pub capture_input: IoOptions,
/// Sets -e flag. Will exit on any error while running the script (not available for windows)
pub exit_on_error: bool,
/// Sets -x flag for printing each script command before invocation (not available for windows)
pub print_commands: bool,
}

#[derive(Debug, Copy, Clone, PartialEq)]
/// Options available for IO
pub enum IoOptions {
/// Corresponds to Stdio::null()
Null,
/// Corresponds to Stdio::pipe()
Pipe,
/// Corresponds to Stdio::inherit()
Inherit
}

impl ScriptOptions {
/// Returns new instance
pub fn new() -> ScriptOptions {
ScriptOptions {
runner: None,
capture_output: true,
capture_output: IoOptions::Pipe,
capture_input: IoOptions::Inherit,
exit_on_error: false,
print_commands: false,
}
Expand Down
3 changes: 2 additions & 1 deletion src/types_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ fn script_options_new() {
let options = ScriptOptions::new();

assert!(options.runner.is_none());
assert!(options.capture_output);
assert!(options.capture_output == IoOptions::Pipe);
assert!(options.capture_input == IoOptions::Inherit);
assert!(!options.exit_on_error);
assert!(!options.print_commands);
}

0 comments on commit abab157

Please sign in to comment.