From 0fe90bf0188f94be8334ff9030bcb9ed42e3329b Mon Sep 17 00:00:00 2001 From: Thomas O'Donnell Date: Wed, 15 Jan 2020 19:52:38 +0100 Subject: [PATCH] refactor(dotnet): Use `exec_cmd` util (#826) Have refactored the dotnet module to use the util::exec_cmd rather than the Command module directly. --- src/modules/dotnet.rs | 94 ++++++++++++++++--------------------------- 1 file changed, 34 insertions(+), 60 deletions(-) diff --git a/src/modules/dotnet.rs b/src/modules/dotnet.rs index 2c11a510a..738e38a93 100644 --- a/src/modules/dotnet.rs +++ b/src/modules/dotnet.rs @@ -2,11 +2,11 @@ use std::ffi::OsStr; use std::iter::Iterator; use std::ops::Deref; use std::path::{Path, PathBuf}; -use std::process::{Command, Stdio}; use std::str; use super::{Context, Module, RootModuleConfig}; use crate::configs::dotnet::DotnetConfig; +use crate::utils; type JValue = serde_json::Value; @@ -201,70 +201,44 @@ fn map_str_to_lower(value: Option<&OsStr>) -> Option { } fn get_version_from_cli() -> Option { - let version_output = match Command::new("dotnet").arg("--version").output() { - Ok(output) => output, - Err(e) => { - log::warn!("Failed to execute `dotnet --version`. {}", e); - return None; - } - }; - let version = str::from_utf8(version_output.stdout.as_slice()) - .ok()? - .trim(); - - let mut buffer = String::with_capacity(version.len() + 1); - buffer.push('v'); - buffer.push_str(version); - - Some(Version(buffer)) + let version_output = utils::exec_cmd("dotnet", &["--version"])?; + Some(Version(format!("v{}", version_output.stdout.trim()))) } fn get_latest_sdk_from_cli() -> Option { - let mut cmd = Command::new("dotnet"); - cmd.arg("--list-sdks") - .stdout(Stdio::piped()) - .stderr(Stdio::null()) - .stdin(Stdio::null()); - - let exit_code = match cmd.status() { - Ok(status) => status, - Err(e) => { - log::warn!("Failed to execute `dotnet --list-sdks`. {}", e); - return None; + match utils::exec_cmd("dotnet", &["--list-sdks"]) { + Some(sdks_output) => { + fn parse_failed() -> Option { + log::warn!("Unable to parse the output from `dotnet --list-sdks`."); + None + }; + let latest_sdk = sdks_output + .stdout + .lines() + .map(str::trim) + .filter(|l| !l.is_empty()) + .last() + .or_else(parse_failed)?; + let take_until = latest_sdk.find('[').or_else(parse_failed)? - 1; + if take_until > 1 { + let version = &latest_sdk[..take_until]; + let mut buffer = String::with_capacity(version.len() + 1); + buffer.push('v'); + buffer.push_str(version); + Some(Version(buffer)) + } else { + parse_failed() + } } - }; - - if exit_code.success() { - let sdks_output = cmd.output().ok()?; - fn parse_failed() -> Option { - log::warn!("Unable to parse the output from `dotnet --list-sdks`."); - None - }; - let latest_sdk = str::from_utf8(sdks_output.stdout.as_slice()) - .ok()? - .lines() - .map(str::trim) - .filter(|l| !l.is_empty()) - .last() - .or_else(parse_failed)?; - let take_until = latest_sdk.find('[').or_else(parse_failed)? - 1; - if take_until > 1 { - let version = &latest_sdk[..take_until]; - let mut buffer = String::with_capacity(version.len() + 1); - buffer.push('v'); - buffer.push_str(version); - Some(Version(buffer)) - } else { - parse_failed() + None => { + // Older versions of the dotnet cli do not support the --list-sdks command + // So, if the status code indicates failure, fall back to `dotnet --version` + log::warn!( + "Received a non-success exit code from `dotnet --list-sdks`. \ + Falling back to `dotnet --version`.", + ); + get_version_from_cli() } - } else { - // Older versions of the dotnet cli do not support the --list-sdks command - // So, if the status code indicates failure, fall back to `dotnet --version` - log::warn!( - "Received a non-success exit code from `dotnet --list-sdks`. \ - Falling back to `dotnet --version`.", - ); - get_version_from_cli() } }