fix: use whoami for user/hostname queries again (#6311)

This commit is contained in:
David Knaack 2024-10-14 20:46:04 +02:00 committed by GitHub
parent 1f512ea7d2
commit 77f08832d5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 32 additions and 24 deletions

28
Cargo.lock generated
View File

@ -970,16 +970,6 @@ dependencies = [
"version_check", "version_check",
] ]
[[package]]
name = "gethostname"
version = "0.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dc3655aa6818d65bc620d6911f05aa7b6aeb596291e1e9f79e52df85583d1e30"
dependencies = [
"rustix",
"windows-targets 0.52.6",
]
[[package]] [[package]]
name = "getrandom" name = "getrandom"
version = "0.2.15" version = "0.2.15"
@ -2762,7 +2752,6 @@ dependencies = [
"deelevate", "deelevate",
"dirs 5.0.1", "dirs 5.0.1",
"dunce", "dunce",
"gethostname",
"gix", "gix",
"gix-features", "gix-features",
"guess_host_triple", "guess_host_triple",
@ -2804,6 +2793,7 @@ dependencies = [
"urlencoding", "urlencoding",
"versions", "versions",
"which", "which",
"whoami",
"windows 0.58.0", "windows 0.58.0",
"winres", "winres",
"yaml-rust2", "yaml-rust2",
@ -3293,6 +3283,12 @@ version = "0.11.0+wasi-snapshot-preview1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423"
[[package]]
name = "wasite"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b8dad83b4f25e74f184f64c43b150b91efe7647395b42289f38e50566d82855b"
[[package]] [[package]]
name = "wasm-bindgen" name = "wasm-bindgen"
version = "0.2.95" version = "0.2.95"
@ -3360,6 +3356,16 @@ dependencies = [
"winsafe", "winsafe",
] ]
[[package]]
name = "whoami"
version = "1.5.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "372d5b87f58ec45c384ba03563b03544dc5fadc3983e434b286913f5b4a9bb6d"
dependencies = [
"redox_syscall",
"wasite",
]
[[package]] [[package]]
name = "winapi" name = "winapi"
version = "0.3.9" version = "0.3.9"

View File

@ -23,7 +23,7 @@ license = "ISC"
readme = "README.md" readme = "README.md"
repository = "https://github.com/starship/starship" repository = "https://github.com/starship/starship"
# Note: MSRV is only intended as a hint, and only the latest version is officially supported in starship. # Note: MSRV is only intended as a hint, and only the latest version is officially supported in starship.
rust-version = "1.74" rust-version = "1.76"
description = """ description = """
The minimal, blazing-fast, and infinitely customizable prompt for any shell! 🌌 The minimal, blazing-fast, and infinitely customizable prompt for any shell! 🌌
""" """
@ -47,7 +47,6 @@ clap = { version = "4.5.20", features = ["derive", "cargo", "unicode"] }
clap_complete = "4.5.33" clap_complete = "4.5.33"
dirs = "5.0.1" dirs = "5.0.1"
dunce = "1.0.5" dunce = "1.0.5"
gethostname = "0.5.0"
# 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.66.0", default-features = false, features = ["max-performance-safe", "revision"] } gix = { version = "0.66.0", default-features = false, features = ["max-performance-safe", "revision"] }
gix-features = { version = "0.38.2", optional = true } gix-features = { version = "0.38.2", optional = true }
@ -88,6 +87,7 @@ unicode-width = "0.2.0"
urlencoding = "2.1.3" urlencoding = "2.1.3"
versions = "6.3.2" versions = "6.3.2"
which = "6.0.3" which = "6.0.3"
whoami = { version = "1.5.2", default-features = false }
yaml-rust2 = "0.9.0" yaml-rust2 = "0.9.0"
guess_host_triple = "0.1.4" guess_host_triple = "0.1.4"

View File

@ -1,10 +1,11 @@
use super::{Context, Module}; use super::{Context, Module};
use std::ffi::OsString;
use crate::config::ModuleConfig; use crate::config::ModuleConfig;
use crate::configs::hostname::HostnameConfig; use crate::configs::hostname::HostnameConfig;
use crate::formatter::StringFormatter; use crate::formatter::StringFormatter;
use whoami::fallible::hostname;
/// Creates a module with the system hostname /// Creates a module with the system hostname
/// ///
/// Will display the hostname if all of the following criteria are met: /// Will display the hostname if all of the following criteria are met:
@ -23,15 +24,9 @@ pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
return None; return None;
} }
let os_hostname: OsString = gethostname::gethostname(); let host = hostname()
.inspect_err(|e| log::warn!("Failed to get hostname: {e}"))
let host = match os_hostname.into_string() { .ok()?;
Ok(host) => host,
Err(bad) => {
log::warn!("hostname is not valid UTF!\n{:?}", bad);
return None;
}
};
//rustc doesn't let you do an "if" and an "if let" in the same if statement //rustc doesn't let you do an "if" and an "if let" in the same if statement
// if this changes in the future this can become a lot cleaner // if this changes in the future this can become a lot cleaner
@ -91,7 +86,7 @@ mod tests {
macro_rules! get_hostname { macro_rules! get_hostname {
() => { () => {
if let Ok(hostname) = gethostname::gethostname().into_string() { if let Ok(hostname) = whoami::fallible::hostname() {
hostname hostname
} else { } else {
println!( println!(

View File

@ -19,6 +19,13 @@ const USERNAME_ENV_VAR: &str = "USERNAME";
/// Does not display the username: /// Does not display the username:
/// - If the option `username.detect_env_vars` is set with a negated environment variable [A] /// - If the option `username.detect_env_vars` is set with a negated environment variable [A]
pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> { pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
#[cfg(not(test))]
let mut username = whoami::fallible::username()
.inspect_err(|e| log::debug!("Failed to get username {e:?}"))
.ok()
.or_else(|| context.get_env(USERNAME_ENV_VAR))?;
#[cfg(test)]
let mut username = context.get_env(USERNAME_ENV_VAR)?; let mut username = context.get_env(USERNAME_ENV_VAR)?;
let mut module = context.new_module("username"); let mut module = context.new_module("username");