src/tools.rs - command_output: add parameter to check exit code
This commit is contained in:
		| @ -149,7 +149,7 @@ pub fn compute_file_diff(filename: &str, shadow: &str) -> Result<String, Error> | ||||
|         .output() | ||||
|         .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))?; | ||||
|  | ||||
|     Ok(diff) | ||||
| @ -165,17 +165,14 @@ pub fn assert_ifupdown2_installed() -> Result<(), Error> { | ||||
|  | ||||
| pub fn network_reload() -> Result<(), Error> { | ||||
|  | ||||
|     let status = Command::new("/sbin/ifreload") | ||||
|     let output = Command::new("/sbin/ifreload") | ||||
|         .arg("-a") | ||||
|         .status() | ||||
|         .map_err(|err| format_err!("failed to execute ifreload: - {}", err))?; | ||||
|         .output() | ||||
|         .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(()) | ||||
| } | ||||
|  | ||||
							
								
								
									
										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 | ||||
| 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() { | ||||
|         match output.status.code() { | ||||
|             Some(code) => { | ||||
|                 if code == 0 { return Ok(String::new()); } | ||||
|                 if code != 1 { | ||||
|                 let is_ok = match exit_code_check { | ||||
|                     Some(check_fn) => check_fn(code), | ||||
|                     None => code == 0, | ||||
|                 }; | ||||
|                 if !is_ok { | ||||
|                     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)")); | ||||
|  | ||||
		Reference in New Issue
	
	Block a user