New --get-exit-code flag for exec command #127

This commit is contained in:
sagie gur ari 2020-08-26 18:20:14 +00:00
parent 3389ced74f
commit 595d1d3ee6
5 changed files with 60 additions and 16 deletions

View file

@ -1,5 +1,9 @@
## CHANGELOG
### v0.6.7
* New --get-exit-code flag for exec command #127
### v0.6.6 (2020-08-14)
* Bug fix in exec command with fail on error flag.

View file

@ -4628,11 +4628,13 @@ output = exec command [args]*
stdout = set ${output.stdout}
stderr = set ${output.stderr}
exit_code = set ${output.code}
exit_code = exec --get-exit-code command [args]*
```
Executes the provided native command and arguments.<br>
If no output variable is set, the command output will be flushed to the main out/err stream of the parent process.<br>
In addition, in order to fail the command in case of the child process failed, add the --fail_on_error flag.<br>
In addition, in order to fail the command in case of the child process failed, add the --fail-on-error flag.<br>
If an output variable is set, it will be used as a base variable name from which the command stout, stderr and exit code information can be pulled from.<br>
The actual output variable name will not be modified, instead new variables will be created using that variable name as a baseline:
@ -4640,9 +4642,12 @@ The actual output variable name will not be modified, instead new variables will
* *output*.stderr - Will hold the stderr text content.
* *output*.code - Will hold the process exit code.
If an output variable is set and the --get-exit-code flag is provided, the output will only contain the exit code.
#### Parameters
* --fail-on-error - If no output variable is provided, it will cause an error in case the executed processed exists with an error exist code.
* --get-exit-code - If an output variable is provided, it will contain the exit code.
* The command to execute and its arguments.
#### Return Value

View file

@ -5,11 +5,13 @@ output = exec command [args]*
stdout = set ${output.stdout}
stderr = set ${output.stderr}
exit_code = set ${output.code}
exit_code = exec --get-exit-code command [args]*
```
Executes the provided native command and arguments.<br>
If no output variable is set, the command output will be flushed to the main out/err stream of the parent process.<br>
In addition, in order to fail the command in case of the child process failed, add the --fail_on_error flag.<br>
In addition, in order to fail the command in case of the child process failed, add the --fail-on-error flag.<br>
If an output variable is set, it will be used as a base variable name from which the command stout, stderr and exit code information can be pulled from.<br>
The actual output variable name will not be modified, instead new variables will be created using that variable name as a baseline:
@ -17,9 +19,12 @@ The actual output variable name will not be modified, instead new variables will
* *output*.stderr - Will hold the stderr text content.
* *output*.code - Will hold the process exit code.
If an output variable is set and the --get-exit-code flag is provided, the output will only contain the exit code.
#### Parameters
* --fail-on-error - If no output variable is provided, it will cause an error in case the executed processed exists with an error exist code.
* --get-exit-code - If an output variable is provided, it will contain the exit code.
* The command to execute and its arguments.
#### Return Value

View file

@ -45,30 +45,40 @@ impl Command for CommandImpl {
_line: usize,
) -> CommandResult {
let allow_input = output_variable.is_some();
let print_output = !allow_input;
let (start_index, fail_on_error) =
let (print_output, start_index, fail_on_error, exit_code_output) =
if !arguments.is_empty() && arguments[0] == "--fail-on-error" {
(1, output_variable.is_none())
(
output_variable.is_none(),
1,
output_variable.is_none(),
false,
)
} else if !arguments.is_empty() && arguments[0] == "--get-exit-code" {
(true, 1, false, true)
} else {
(0, false)
(output_variable.is_none(), 0, false, false)
};
match exec::exec(&arguments, print_output, allow_input, start_index) {
Ok((stdout, stderr, exit_code)) => match output_variable {
Some(name) => {
let mut key = String::from(&name);
key.push_str(".stdout");
variables.insert(key.clone(), stdout);
if exit_code_output {
CommandResult::Continue(Some(exit_code.to_string()))
} else {
let mut key = String::from(&name);
key.push_str(".stdout");
variables.insert(key.clone(), stdout);
key = String::from(&name);
key.push_str(".stderr");
variables.insert(key.clone(), stderr);
key = String::from(&name);
key.push_str(".stderr");
variables.insert(key.clone(), stderr);
key = String::from(&name);
key.push_str(".code");
variables.insert(key.clone(), exit_code.to_string());
key = String::from(&name);
key.push_str(".code");
variables.insert(key.clone(), exit_code.to_string());
CommandResult::Continue(None)
CommandResult::Continue(None)
}
}
None => {
if fail_on_error && exit_code != 0 {

View file

@ -52,3 +52,23 @@ fn run_with_output() {
fn run_error_code_with_output() {
test::run_script_and_error(vec![create("")], "out = exec badcommand", "out");
}
#[test]
#[cfg(target_os = "linux")]
fn run_get_exit_code_valid() {
test::run_script_and_validate(
vec![create("")],
"out = exec --get-exit-code true",
CommandValidation::Match("out".to_string(), "0".to_string()),
);
}
#[test]
#[cfg(target_os = "linux")]
fn run_get_exit_code_error() {
test::run_script_and_validate(
vec![create("")],
"out = exec --get-exit-code false",
CommandValidation::Match("out".to_string(), "1".to_string()),
);
}