From 8776ddc9c6cc238d6caa4b3a79c288b0ff4870c0 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Sun, 10 Nov 2019 00:58:29 +0100 Subject: Collapse else-if block Signed-off-by: Matthias Beyer --- bin/core/imag/src/main.rs | 152 +++++++++++++++++++++++----------------------- 1 file changed, 75 insertions(+), 77 deletions(-) diff --git a/bin/core/imag/src/main.rs b/bin/core/imag/src/main.rs index d879d755..628c1093 100644 --- a/bin/core/imag/src/main.rs +++ b/bin/core/imag/src/main.rs @@ -196,89 +196,87 @@ fn main() -> Result<()> { if matches.is_present("version") { debug!("Showing version"); writeln!(out, "imag {}", env!("CARGO_PKG_VERSION")).map_err(Error::from) - } else { - if matches.is_present("versions") { - debug!("Showing versions"); - commands - .iter() - .map(|command| { - match Command::new(format!("imag-{}", command)) - .stdin(::std::process::Stdio::inherit()) - .stdout(::std::process::Stdio::piped()) - .stderr(::std::process::Stdio::inherit()) - .arg("--version") - .output() - .map(|v| v.stdout) - { - Ok(s) => match String::from_utf8(s) { - Ok(s) => format!("{:15} -> {}", command, s), - Err(e) => format!("UTF8 Error while working with output of imag{}: {:?}", command, e), - }, - Err(e) => format!("Failed calling imag-{} -> {:?}", command, e), - } - }) - .fold(Ok(()), |_, line| { - // The amount of newlines may differ depending on the subprocess - writeln!(out, "{}", line.trim()).map_err(Error::from) - }) - } else { - let aliases = fetch_aliases(config.as_ref()) - .map_err(Error::from) - .context("Error while fetching aliases from configuration file")?; - - // Matches any subcommand given, except calling for example 'imag --versions', as this option - // does not exit. There's nothing to do in such a case - if let (subcommand, Some(scmd)) = matches.subcommand() { - // Get all given arguments and further subcommands to pass to - // the imag-<> binary - // Providing no arguments is OK, and is therefore ignored here - let mut subcommand_args : Vec = match scmd.values_of("") { - Some(values) => values.map(String::from).collect(), - None => Vec::new() - }; - - debug!("Processing forwarding of commandline arguments"); - forward_commandline_arguments(&matches, &mut subcommand_args); - - let subcommand = String::from(subcommand); - let subcommand = aliases.get(&subcommand).cloned().unwrap_or(subcommand); - - debug!("Calling 'imag-{}' with args: {:?}", subcommand, subcommand_args); - - // Create a Command, and pass it the gathered arguments - match Command::new(format!("imag-{}", subcommand)) - .stdin(Stdio::inherit()) - .stdout(Stdio::inherit()) - .stderr(Stdio::inherit()) - .args(&subcommand_args[..]) - .spawn() - .and_then(|mut c| c.wait()) + } else if matches.is_present("versions") { + debug!("Showing versions"); + commands + .iter() + .map(|command| { + match Command::new(format!("imag-{}", command)) + .stdin(::std::process::Stdio::inherit()) + .stdout(::std::process::Stdio::piped()) + .stderr(::std::process::Stdio::inherit()) + .arg("--version") + .output() + .map(|v| v.stdout) { - Ok(exit_status) => if !exit_status.success() { - debug!("imag-{} exited with non-zero exit code: {:?}", subcommand, exit_status); - Err(format_err!("imag-{} exited with non-zero exit code", subcommand)) - } else { - debug!("Successful exit!"); - Ok(()) + Ok(s) => match String::from_utf8(s) { + Ok(s) => format!("{:15} -> {}", command, s), + Err(e) => format!("UTF8 Error while working with output of imag{}: {:?}", command, e), }, + Err(e) => format!("Failed calling imag-{} -> {:?}", command, e), + } + }) + .fold(Ok(()), |_, line| { + // The amount of newlines may differ depending on the subprocess + writeln!(out, "{}", line.trim()).map_err(Error::from) + }) + } else { + let aliases = fetch_aliases(config.as_ref()) + .map_err(Error::from) + .context("Error while fetching aliases from configuration file")?; + + // Matches any subcommand given, except calling for example 'imag --versions', as this option + // does not exit. There's nothing to do in such a case + if let (subcommand, Some(scmd)) = matches.subcommand() { + // Get all given arguments and further subcommands to pass to + // the imag-<> binary + // Providing no arguments is OK, and is therefore ignored here + let mut subcommand_args : Vec = match scmd.values_of("") { + Some(values) => values.map(String::from).collect(), + None => Vec::new() + }; + + debug!("Processing forwarding of commandline arguments"); + forward_commandline_arguments(&matches, &mut subcommand_args); + + let subcommand = String::from(subcommand); + let subcommand = aliases.get(&subcommand).cloned().unwrap_or(subcommand); + + debug!("Calling 'imag-{}' with args: {:?}", subcommand, subcommand_args); + + // Create a Command, and pass it the gathered arguments + match Command::new(format!("imag-{}", subcommand)) + .stdin(Stdio::inherit()) + .stdout(Stdio::inherit()) + .stderr(Stdio::inherit()) + .args(&subcommand_args[..]) + .spawn() + .and_then(|mut c| c.wait()) + { + Ok(exit_status) => if !exit_status.success() { + debug!("imag-{} exited with non-zero exit code: {:?}", subcommand, exit_status); + Err(format_err!("imag-{} exited with non-zero exit code", subcommand)) + } else { + debug!("Successful exit!"); + Ok(()) + }, - Err(e) => { - debug!("Error calling the subcommand"); - match e.kind() { - ErrorKind::NotFound => { - writeln!(out, "No such command: 'imag-{}'", subcommand)?; - writeln!(out, "See 'imag --help' for available subcommands").map_err(Error::from) - }, - ErrorKind::PermissionDenied => { - writeln!(out, "No permission to execute: 'imag-{}'", subcommand).map_err(Error::from) - }, - _ => writeln!(out, "Error spawning: {:?}", e).map_err(Error::from), - } + Err(e) => { + debug!("Error calling the subcommand"); + match e.kind() { + ErrorKind::NotFound => { + writeln!(out, "No such command: 'imag-{}'", subcommand)?; + writeln!(out, "See 'imag --help' for available subcommands").map_err(Error::from) + }, + ErrorKind::PermissionDenied => { + writeln!(out, "No permission to execute: 'imag-{}'", subcommand).map_err(Error::from) + }, + _ => writeln!(out, "Error spawning: {:?}", e).map_err(Error::from), } } - } else { - Ok(()) } + } else { + Ok(()) } } } -- cgit v1.2.3