Skip to content

Commit

Permalink
revert: refactor(modules): use whoami crate to get username (#5669)
Browse files Browse the repository at this point in the history
Revert "refactor(modules): use whoami crate to get username"
  • Loading branch information
davidkna committed Jan 2, 2024
1 parent 551b82b commit a83e107
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 15 deletions.
18 changes: 11 additions & 7 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Expand Up @@ -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 }
Expand Down Expand Up @@ -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"
Expand Down
4 changes: 2 additions & 2 deletions src/modules/hostname.rs
Expand Up @@ -23,7 +23,7 @@ pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
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,
Expand Down Expand Up @@ -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!(
Expand Down
16 changes: 11 additions & 5 deletions src/modules/username.rs
Expand Up @@ -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<Module<'a>> {
#[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);
Expand Down Expand Up @@ -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);
}

Expand Down

0 comments on commit a83e107

Please sign in to comment.