From 97fab7aa112c4d7b7422f349cf840f3c301f0044 Mon Sep 17 00:00:00 2001 From: Dietmar Maurer Date: Wed, 27 May 2020 06:52:21 +0200 Subject: [PATCH] src/tools.rs: new helper to handle command_output (std::process::Output) --- src/config/network/helper.rs | 19 ++----------------- src/tools.rs | 25 +++++++++++++++++++++++++ 2 files changed, 27 insertions(+), 17 deletions(-) diff --git a/src/config/network/helper.rs b/src/config/network/helper.rs index 569594d8..e32d3e20 100644 --- a/src/config/network/helper.rs +++ b/src/config/network/helper.rs @@ -149,23 +149,8 @@ pub fn compute_file_diff(filename: &str, shadow: &str) -> Result .output() .map_err(|err| format_err!("failed to execute diff - {}", err))?; - if !output.status.success() { - match output.status.code() { - Some(code) => { - if code == 0 { return Ok(String::new()); } - if code != 1 { - let msg = String::from_utf8(output.stderr) - .map(|m| if m.is_empty() { String::from("no error message") } else { m }) - .unwrap_or_else(|_| String::from("non utf8 error message (suppressed)")); - - bail!("diff failed with status code: {} - {}", code, msg); - } - } - None => bail!("diff terminated by signal"), - } - } - - let diff = String::from_utf8(output.stdout)?; + let diff = crate::tools::command_output(output) + .map_err(|err| format_err!("diff failed: {}", err))?; Ok(diff) } diff --git a/src/tools.rs b/src/tools.rs index bbee59bd..06682480 100644 --- a/src/tools.rs +++ b/src/tools.rs @@ -474,6 +474,31 @@ pub fn normalize_uri_path(path: &str) -> Result<(String, Vec<&str>), Error> { Ok((path, components)) } +/// Helper to check result from std::process::Command output +pub fn command_output(output: std::process::Output) -> Result { + + if !output.status.success() { + match output.status.code() { + Some(code) => { + if code == 0 { return Ok(String::new()); } + if code != 1 { + let msg = String::from_utf8(output.stderr) + .map(|m| if m.is_empty() { String::from("no error message") } else { m }) + .unwrap_or_else(|_| String::from("non utf8 error message (suppressed)")); + + bail!("status code: {} - {}", code, msg); + } + } + None => bail!("terminated by signal"), + } + } + + let output = String::from_utf8(output.stdout)?; + + Ok(output) +} + + pub fn fd_change_cloexec(fd: RawFd, on: bool) -> Result<(), Error> { use nix::fcntl::{fcntl, FdFlag, F_GETFD, F_SETFD}; let mut flags = FdFlag::from_bits(fcntl(fd, F_GETFD)?)