From a83e10776ba37bd1ab439e5e4d0125a06e947728 Mon Sep 17 00:00:00 2001 From: David Knaack Date: Tue, 2 Jan 2024 15:45:06 +0100 Subject: [PATCH] revert: refactor(modules): use whoami crate to get username (#5669) Revert "refactor(modules): use whoami crate to get username" --- Cargo.lock | 18 +++++++++++------- Cargo.toml | 2 +- src/modules/hostname.rs | 4 ++-- src/modules/username.rs | 16 +++++++++++----- 4 files changed, 25 insertions(+), 15 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 33ada3c1..803bcfa7 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1023,6 +1023,16 @@ dependencies = [ "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 0.48.5", +] + [[package]] name = "getrandom" version = "0.2.11" @@ -2830,6 +2840,7 @@ dependencies = [ "deelevate", "dirs-next", "dunce", + "gethostname", "gix", "gix-features", "guess_host_triple", @@ -2871,7 +2882,6 @@ dependencies = [ "urlencoding", "versions", "which", - "whoami", "windows 0.48.0", "winres", "yaml-rust", @@ -3435,12 +3445,6 @@ dependencies = [ "windows-sys 0.48.0", ] -[[package]] -name = "whoami" -version = "1.4.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "22fc3756b8a9133049b26c7f61ab35416c130e8c09b660f5b3958b446f52cc50" - [[package]] name = "winapi" version = "0.3.9" diff --git a/Cargo.toml b/Cargo.toml index 8762c11d..077659e4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -47,6 +47,7 @@ clap = { version = "4.4.12", features = ["derive", "cargo", "unicode"] } clap_complete = "4.4.5" dirs-next = "2.0.0" dunce = "1.0.4" +gethostname = "0.4.3" # default feature restriction addresses https://github.com/starship/starship/issues/4251 gix = { version = "0.57.1", default-features = false, features = ["max-performance-safe", "revision"] } gix-features = { version = "0.37.1", optional = true } @@ -93,7 +94,6 @@ process_control = { version = "4.0.3", features = ["crossbeam-channel"] } guess_host_triple = "0.1.3" home = "0.5.9" shell-words = "1.1.0" -whoami = { version = "1.4.1", default-features = false } [dependencies.schemars] version = "0.8.16" diff --git a/src/modules/hostname.rs b/src/modules/hostname.rs index d5e967b6..8eec9b5f 100644 --- a/src/modules/hostname.rs +++ b/src/modules/hostname.rs @@ -23,7 +23,7 @@ pub fn module<'a>(context: &'a Context) -> Option> { return None; } - let os_hostname: OsString = whoami::hostname_os(); + let os_hostname: OsString = gethostname::gethostname(); let host = match os_hostname.into_string() { Ok(host) => host, @@ -87,7 +87,7 @@ mod tests { macro_rules! get_hostname { () => { - if let Ok(hostname) = whoami::hostname_os().into_string() { + if let Ok(hostname) = gethostname::gethostname().into_string() { hostname } else { println!( diff --git a/src/modules/username.rs b/src/modules/username.rs index 537e552f..af3fa6e5 100644 --- a/src/modules/username.rs +++ b/src/modules/username.rs @@ -2,15 +2,21 @@ use super::{Context, Module, ModuleConfig}; use crate::configs::username::UsernameConfig; 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"; /// 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> { - #[cfg(test)] 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 config: UsernameConfig = UsernameConfig::try_load(module.config); @@ -145,8 +151,8 @@ mod tests { let actual = ModuleRenderer::new("username") .env("SSH_CONNECTION", "192.168.223.17 36673 192.168.223.229 22") .collect(); - let expected = None; + assert_eq!(expected, actual); }