mirror of
https://github.com/Llewellynvdm/starship.git
synced 2024-12-12 14:17:45 +00:00
refactor(modules): use whoami crate to get username
Signed-off-by: bestgopher <84328409@qq.com>
This commit is contained in:
parent
ba19753289
commit
b5f9457b28
18
Cargo.lock
generated
18
Cargo.lock
generated
@ -994,16 +994,6 @@ dependencies = [
|
|||||||
"version_check",
|
"version_check",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "gethostname"
|
|
||||||
version = "0.4.3"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "0176e0459c2e4a1fe232f984bca6890e681076abb9934f6cea7c326f3fc47818"
|
|
||||||
dependencies = [
|
|
||||||
"libc",
|
|
||||||
"windows-targets",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "getrandom"
|
name = "getrandom"
|
||||||
version = "0.2.10"
|
version = "0.2.10"
|
||||||
@ -2826,7 +2816,6 @@ dependencies = [
|
|||||||
"deelevate",
|
"deelevate",
|
||||||
"dirs-next",
|
"dirs-next",
|
||||||
"dunce",
|
"dunce",
|
||||||
"gethostname",
|
|
||||||
"gix",
|
"gix",
|
||||||
"gix-features",
|
"gix-features",
|
||||||
"guess_host_triple",
|
"guess_host_triple",
|
||||||
@ -2868,6 +2857,7 @@ dependencies = [
|
|||||||
"urlencoding",
|
"urlencoding",
|
||||||
"versions",
|
"versions",
|
||||||
"which",
|
"which",
|
||||||
|
"whoami",
|
||||||
"windows 0.48.0",
|
"windows 0.48.0",
|
||||||
"winres",
|
"winres",
|
||||||
"yaml-rust",
|
"yaml-rust",
|
||||||
@ -3430,6 +3420,12 @@ dependencies = [
|
|||||||
"windows-sys",
|
"windows-sys",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "whoami"
|
||||||
|
version = "1.4.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "22fc3756b8a9133049b26c7f61ab35416c130e8c09b660f5b3958b446f52cc50"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "winapi"
|
name = "winapi"
|
||||||
version = "0.3.9"
|
version = "0.3.9"
|
||||||
|
@ -47,7 +47,6 @@ clap = { version = "4.4.8", features = ["derive", "cargo", "unicode"] }
|
|||||||
clap_complete = "4.4.4"
|
clap_complete = "4.4.4"
|
||||||
dirs-next = "2.0.0"
|
dirs-next = "2.0.0"
|
||||||
dunce = "1.0.4"
|
dunce = "1.0.4"
|
||||||
gethostname = "0.4.3"
|
|
||||||
# default feature restriction addresses https://github.com/starship/starship/issues/4251
|
# default feature restriction addresses https://github.com/starship/starship/issues/4251
|
||||||
gix = { version = "0.55.2", default-features = false, features = ["max-performance-safe", "revision"] }
|
gix = { version = "0.55.2", default-features = false, features = ["max-performance-safe", "revision"] }
|
||||||
gix-features = { version = "0.36.0", optional = true }
|
gix-features = { version = "0.36.0", optional = true }
|
||||||
@ -94,6 +93,7 @@ process_control = { version = "4.0.3", features = ["crossbeam-channel"] }
|
|||||||
guess_host_triple = "0.1.3"
|
guess_host_triple = "0.1.3"
|
||||||
home = "0.5.5"
|
home = "0.5.5"
|
||||||
shell-words = "1.1.0"
|
shell-words = "1.1.0"
|
||||||
|
whoami = { version = "1.4.1", default-features = false }
|
||||||
|
|
||||||
[dependencies.schemars]
|
[dependencies.schemars]
|
||||||
version = "0.8.15"
|
version = "0.8.15"
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
use std::fmt::Write as _;
|
||||||
use std::process;
|
use std::process;
|
||||||
use std::process::Stdio;
|
use std::process::Stdio;
|
||||||
use std::str::FromStr;
|
use std::str::FromStr;
|
||||||
@ -96,8 +97,10 @@ pub fn print_configuration(context: &Context, use_default: bool, paths: &[String
|
|||||||
"# $all is shorthand for {}",
|
"# $all is shorthand for {}",
|
||||||
PROMPT_ORDER
|
PROMPT_ORDER
|
||||||
.iter()
|
.iter()
|
||||||
.map(|module_name| format!("${module_name}"))
|
.fold(String::new(), |mut output, module_name| {
|
||||||
.collect::<String>()
|
let _ = write!(output, "${module_name}");
|
||||||
|
output
|
||||||
|
})
|
||||||
);
|
);
|
||||||
|
|
||||||
// Unwrapping is fine because config is based on FullConfig
|
// Unwrapping is fine because config is based on FullConfig
|
||||||
@ -105,10 +108,10 @@ pub fn print_configuration(context: &Context, use_default: bool, paths: &[String
|
|||||||
if !use_default && !custom_modules.is_empty() {
|
if !use_default && !custom_modules.is_empty() {
|
||||||
println!(
|
println!(
|
||||||
"# $custom (excluding any modules already listed in `format`) is shorthand for {}",
|
"# $custom (excluding any modules already listed in `format`) is shorthand for {}",
|
||||||
custom_modules
|
custom_modules.keys().fold(String::new(), |mut output, b| {
|
||||||
.keys()
|
let _ = write!(output, "${{custom.{b}}}");
|
||||||
.map(|module_name| format!("${{custom.{module_name}}}"))
|
output
|
||||||
.collect::<String>()
|
})
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -668,7 +668,7 @@ credential_process = /opt/bin/awscreds-retriever
|
|||||||
|
|
||||||
let expiration_env_vars = ["AWS_SESSION_EXPIRATION", "AWS_CREDENTIAL_EXPIRATION"];
|
let expiration_env_vars = ["AWS_SESSION_EXPIRATION", "AWS_CREDENTIAL_EXPIRATION"];
|
||||||
expiration_env_vars.iter().for_each(|env_var| {
|
expiration_env_vars.iter().for_each(|env_var| {
|
||||||
let now_plus_half_hour: DateTime<Utc> = chrono::DateTime::from_utc(
|
let now_plus_half_hour: DateTime<Utc> = DateTime::from_naive_utc_and_offset(
|
||||||
NaiveDateTime::from_timestamp_opt(chrono::Local::now().timestamp() + 1800, 0)
|
NaiveDateTime::from_timestamp_opt(chrono::Local::now().timestamp() + 1800, 0)
|
||||||
.unwrap(),
|
.unwrap(),
|
||||||
Utc,
|
Utc,
|
||||||
@ -702,7 +702,7 @@ credential_process = /opt/bin/awscreds-retriever
|
|||||||
|
|
||||||
use chrono::{DateTime, NaiveDateTime, Utc};
|
use chrono::{DateTime, NaiveDateTime, Utc};
|
||||||
|
|
||||||
let now_plus_half_hour: DateTime<Utc> = chrono::DateTime::from_utc(
|
let now_plus_half_hour: DateTime<Utc> = DateTime::from_naive_utc_and_offset(
|
||||||
NaiveDateTime::from_timestamp_opt(chrono::Local::now().timestamp() + 1800, 0).unwrap(),
|
NaiveDateTime::from_timestamp_opt(chrono::Local::now().timestamp() + 1800, 0).unwrap(),
|
||||||
Utc,
|
Utc,
|
||||||
);
|
);
|
||||||
@ -789,7 +789,7 @@ aws_secret_access_key=dummy
|
|||||||
fn expiration_date_set_expired() {
|
fn expiration_date_set_expired() {
|
||||||
use chrono::{DateTime, NaiveDateTime, SecondsFormat, Utc};
|
use chrono::{DateTime, NaiveDateTime, SecondsFormat, Utc};
|
||||||
|
|
||||||
let now: DateTime<Utc> = chrono::DateTime::from_utc(
|
let now: DateTime<Utc> = DateTime::from_naive_utc_and_offset(
|
||||||
NaiveDateTime::from_timestamp_opt(chrono::Local::now().timestamp() - 1800, 0).unwrap(),
|
NaiveDateTime::from_timestamp_opt(chrono::Local::now().timestamp() - 1800, 0).unwrap(),
|
||||||
Utc,
|
Utc,
|
||||||
);
|
);
|
||||||
|
@ -23,7 +23,7 @@ pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
|
|||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
|
|
||||||
let os_hostname: OsString = gethostname::gethostname();
|
let os_hostname: OsString = whoami::hostname_os();
|
||||||
|
|
||||||
let host = match os_hostname.into_string() {
|
let host = match os_hostname.into_string() {
|
||||||
Ok(host) => host,
|
Ok(host) => host,
|
||||||
@ -87,7 +87,7 @@ mod tests {
|
|||||||
|
|
||||||
macro_rules! get_hostname {
|
macro_rules! get_hostname {
|
||||||
() => {
|
() => {
|
||||||
if let Ok(hostname) = gethostname::gethostname().into_string() {
|
if let Ok(hostname) = whoami::hostname_os().into_string() {
|
||||||
hostname
|
hostname
|
||||||
} else {
|
} else {
|
||||||
println!(
|
println!(
|
||||||
|
@ -2,21 +2,15 @@ use super::{Context, Module, ModuleConfig};
|
|||||||
|
|
||||||
use crate::configs::username::UsernameConfig;
|
use crate::configs::username::UsernameConfig;
|
||||||
use crate::formatter::StringFormatter;
|
use crate::formatter::StringFormatter;
|
||||||
|
#[cfg(test)]
|
||||||
#[cfg(not(target_os = "windows"))]
|
|
||||||
const USERNAME_ENV_VAR: &str = "USER";
|
|
||||||
|
|
||||||
#[cfg(target_os = "windows")]
|
|
||||||
const USERNAME_ENV_VAR: &str = "USERNAME";
|
const USERNAME_ENV_VAR: &str = "USERNAME";
|
||||||
|
|
||||||
/// Creates a module with the current user's username
|
/// Creates a module with the current user's username
|
||||||
///
|
|
||||||
/// Will display the username if any of the following criteria are met:
|
|
||||||
/// - The current user is root (UID = 0) [1]
|
|
||||||
/// - The current user isn't the same as the one that is logged in (`$LOGNAME` != `$USER`) [2]
|
|
||||||
/// - The user is currently connected as an SSH session (`$SSH_CONNECTION`) [3]
|
|
||||||
pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
|
pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
|
||||||
|
#[cfg(test)]
|
||||||
let mut username = context.get_env(USERNAME_ENV_VAR)?;
|
let mut username = context.get_env(USERNAME_ENV_VAR)?;
|
||||||
|
#[cfg(not(test))]
|
||||||
|
let mut username = whoami::username();
|
||||||
|
|
||||||
let mut module = context.new_module("username");
|
let mut module = context.new_module("username");
|
||||||
let config: UsernameConfig = UsernameConfig::try_load(module.config);
|
let config: UsernameConfig = UsernameConfig::try_load(module.config);
|
||||||
@ -151,8 +145,8 @@ mod tests {
|
|||||||
let actual = ModuleRenderer::new("username")
|
let actual = ModuleRenderer::new("username")
|
||||||
.env("SSH_CONNECTION", "192.168.223.17 36673 192.168.223.229 22")
|
.env("SSH_CONNECTION", "192.168.223.17 36673 192.168.223.229 22")
|
||||||
.collect();
|
.collect();
|
||||||
let expected = None;
|
|
||||||
|
|
||||||
|
let expected = None;
|
||||||
assert_eq!(expected, actual);
|
assert_eq!(expected, actual);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -507,8 +507,10 @@ pub fn preset_command(name: Option<Preset>, output: Option<PathBuf>, list: bool)
|
|||||||
fn preset_list() -> String {
|
fn preset_list() -> String {
|
||||||
Preset::value_variants()
|
Preset::value_variants()
|
||||||
.iter()
|
.iter()
|
||||||
.map(|v| format!("{}\n", v.0))
|
.fold(String::new(), |mut output, b| {
|
||||||
.collect()
|
let _ = writeln!(output, "{}", b.0);
|
||||||
|
output
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
|
Loading…
Reference in New Issue
Block a user