src/tools.rs - command_output: add parameter to check exit code
This commit is contained in:
parent
97fab7aa11
commit
143b654550
|
@ -149,7 +149,7 @@ pub fn compute_file_diff(filename: &str, shadow: &str) -> Result<String, Error>
|
||||||
.output()
|
.output()
|
||||||
.map_err(|err| format_err!("failed to execute diff - {}", err))?;
|
.map_err(|err| format_err!("failed to execute diff - {}", err))?;
|
||||||
|
|
||||||
let diff = crate::tools::command_output(output)
|
let diff = crate::tools::command_output(output, Some(|c| c == 0 || c == 1))
|
||||||
.map_err(|err| format_err!("diff failed: {}", err))?;
|
.map_err(|err| format_err!("diff failed: {}", err))?;
|
||||||
|
|
||||||
Ok(diff)
|
Ok(diff)
|
||||||
|
@ -165,17 +165,14 @@ pub fn assert_ifupdown2_installed() -> Result<(), Error> {
|
||||||
|
|
||||||
pub fn network_reload() -> Result<(), Error> {
|
pub fn network_reload() -> Result<(), Error> {
|
||||||
|
|
||||||
let status = Command::new("/sbin/ifreload")
|
let output = Command::new("/sbin/ifreload")
|
||||||
.arg("-a")
|
.arg("-a")
|
||||||
.status()
|
.output()
|
||||||
.map_err(|err| format_err!("failed to execute ifreload: - {}", err))?;
|
.map_err(|err| format_err!("failed to execute '/sbin/ifreload' - {}", err))?;
|
||||||
|
|
||||||
|
crate::tools::command_output(output, None)
|
||||||
|
.map_err(|err| format_err!("ifreload failed: {}", err))?;
|
||||||
|
|
||||||
if !status.success() {
|
|
||||||
match status.code() {
|
|
||||||
Some(code) => bail!("ifreload failed with status code: {}", code),
|
|
||||||
None => bail!("ifreload terminated by signal")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
15
src/tools.rs
15
src/tools.rs
|
@ -475,13 +475,22 @@ pub fn normalize_uri_path(path: &str) -> Result<(String, Vec<&str>), Error> {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Helper to check result from std::process::Command output
|
/// Helper to check result from std::process::Command output
|
||||||
pub fn command_output(output: std::process::Output) -> Result<String, Error> {
|
///
|
||||||
|
/// The exit_code_check() function should return true if the exit code
|
||||||
|
/// is considered successful.
|
||||||
|
pub fn command_output(
|
||||||
|
output: std::process::Output,
|
||||||
|
exit_code_check: Option<fn(i32) -> bool>
|
||||||
|
) -> Result<String, Error> {
|
||||||
|
|
||||||
if !output.status.success() {
|
if !output.status.success() {
|
||||||
match output.status.code() {
|
match output.status.code() {
|
||||||
Some(code) => {
|
Some(code) => {
|
||||||
if code == 0 { return Ok(String::new()); }
|
let is_ok = match exit_code_check {
|
||||||
if code != 1 {
|
Some(check_fn) => check_fn(code),
|
||||||
|
None => code == 0,
|
||||||
|
};
|
||||||
|
if !is_ok {
|
||||||
let msg = String::from_utf8(output.stderr)
|
let msg = String::from_utf8(output.stderr)
|
||||||
.map(|m| if m.is_empty() { String::from("no error message") } else { m })
|
.map(|m| if m.is_empty() { String::from("no error message") } else { m })
|
||||||
.unwrap_or_else(|_| String::from("non utf8 error message (suppressed)"));
|
.unwrap_or_else(|_| String::from("non utf8 error message (suppressed)"));
|
||||||
|
|
Loading…
Reference in New Issue