mirror of
https://github.com/starship/starship.git
synced 2024-11-27 02:35:25 +01:00
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:
parent
ddfcee3809
commit
0fe90bf018
@ -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<String> {
|
||||
}
|
||||
|
||||
fn get_version_from_cli() -> Option<Version> {
|
||||
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<Version> {
|
||||
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<T>() -> Option<T> {
|
||||
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<T>() -> Option<T> {
|
||||
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()
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user