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,70 +201,44 @@ 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()) fn parse_failed<T>() -> Option<T> {
.stderr(Stdio::null()) log::warn!("Unable to parse the output from `dotnet --list-sdks`.");
.stdin(Stdio::null()); None
};
let exit_code = match cmd.status() { let latest_sdk = sdks_output
Ok(status) => status, .stdout
Err(e) => { .lines()
log::warn!("Failed to execute `dotnet --list-sdks`. {}", e); .map(str::trim)
return None; .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
if exit_code.success() { // So, if the status code indicates failure, fall back to `dotnet --version`
let sdks_output = cmd.output().ok()?; log::warn!(
fn parse_failed<T>() -> Option<T> { "Received a non-success exit code from `dotnet --list-sdks`. \
log::warn!("Unable to parse the output from `dotnet --list-sdks`."); Falling back to `dotnet --version`.",
None );
}; get_version_from_cli()
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()
} }
} 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()
} }
} }