Skip to content

Commit

Permalink
Merge pull request #95 from autarch/autarch/more-execute-details
Browse files Browse the repository at this point in the history
Include stdout & stderr from executed command when it fails
  • Loading branch information
haimgel authored May 29, 2022
2 parents bc8de45 + 6e64efc commit f8773c0
Showing 1 changed file with 26 additions and 9 deletions.
35 changes: 26 additions & 9 deletions src/display_control.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,18 +130,35 @@ fn run_command(execute_command: &str) {
}

let executable = arguments.remove(0);
let result = Command::new(executable)
.args(arguments)
.stdin(Stdio::null())
.stdout(Stdio::null())
.stderr(Stdio::null())
.spawn()?
.wait()?;
return if result.success() {
let output = Command::new(executable).args(arguments).stdin(Stdio::null()).output()?;
return if output.status.success() {
info!("External command '{}' executed successfully", execute_command);
Ok(())
} else {
Err(Error::msg(format!("Exited with status {}", result)))
let msg = if let Some(code) = output.status.code() {
format!("Exited with status {}\n", code)
} else {
"Exited because of a signal\n".to_string()
};
let stdout = if !output.stdout.is_empty() {
if let Ok(s) = String::from_utf8(output.stdout) {
format!("Stdout = [{}]\n", s)
} else {
format!("Stdout was not UTF-8")
}
} else {
"No stdout\n".to_string()
};
let stderr = if !output.stderr.is_empty() {
if let Ok(s) = String::from_utf8(output.stderr) {
format!("Stderr = [{}]\n", s)
} else {
format!("Stderr was not UTF-8")
}
} else {
"No stderr\n".to_string()
};
Err(Error::msg(format!("{} {} {}", msg, stdout, stderr)))
};
}

Expand Down

0 comments on commit f8773c0

Please sign in to comment.