refactor(dotnet): Use exec_cmd util (#826)

Have refactored the dotnet module to use the util::exec_cmd rather than
the Command module directly.
This commit is contained in:
Thomas O'Donnell 2020-01-15 19:52:38 +01:00 committed by Matan Kushner
parent ddfcee3809
commit 0fe90bf018

View File

@ -2,11 +2,11 @@ use std::ffi::OsStr;
use std::iter::Iterator; use std::iter::Iterator;
use std::ops::Deref; use std::ops::Deref;
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
use std::process::{Command, Stdio};
use std::str; use std::str;
use super::{Context, Module, RootModuleConfig}; use super::{Context, Module, RootModuleConfig};
use crate::configs::dotnet::DotnetConfig; use crate::configs::dotnet::DotnetConfig;
use crate::utils;
type JValue = serde_json::Value; type JValue = serde_json::Value;
@ -201,47 +201,19 @@ fn map_str_to_lower(value: Option<&OsStr>) -> Option<String> {
} }
fn get_version_from_cli() -> Option<Version> { fn get_version_from_cli() -> Option<Version> {
let version_output = match Command::new("dotnet").arg("--version").output() { let version_output = utils::exec_cmd("dotnet", &["--version"])?;
Ok(output) => output, Some(Version(format!("v{}", version_output.stdout.trim())))
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))
} }
fn get_latest_sdk_from_cli() -> Option<Version> { fn get_latest_sdk_from_cli() -> Option<Version> {
let mut cmd = Command::new("dotnet"); match utils::exec_cmd("dotnet", &["--list-sdks"]) {
cmd.arg("--list-sdks") Some(sdks_output) => {
.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;
}
};
if exit_code.success() {
let sdks_output = cmd.output().ok()?;
fn parse_failed<T>() -> Option<T> { fn parse_failed<T>() -> Option<T> {
log::warn!("Unable to parse the output from `dotnet --list-sdks`."); log::warn!("Unable to parse the output from `dotnet --list-sdks`.");
None None
}; };
let latest_sdk = str::from_utf8(sdks_output.stdout.as_slice()) let latest_sdk = sdks_output
.ok()? .stdout
.lines() .lines()
.map(str::trim) .map(str::trim)
.filter(|l| !l.is_empty()) .filter(|l| !l.is_empty())
@ -257,7 +229,8 @@ fn get_latest_sdk_from_cli() -> Option<Version> {
} else { } else {
parse_failed() parse_failed()
} }
} else { }
None => {
// Older versions of the dotnet cli do not support the --list-sdks command // 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` // So, if the status code indicates failure, fall back to `dotnet --version`
log::warn!( log::warn!(
@ -266,6 +239,7 @@ fn get_latest_sdk_from_cli() -> Option<Version> {
); );
get_version_from_cli() get_version_from_cli()
} }
}
} }
struct DotNetFile<'a> { struct DotNetFile<'a> {