diff options
author | bestgopher <84328409@qq.com> | 2023-10-25 22:20:40 +0800 |
---|---|---|
committer | David Knaack <davidkna@users.noreply.github.com> | 2023-11-11 19:16:49 +0100 |
commit | b5f9457b28db92406c03576663a6181a7081fd0f (patch) | |
tree | b6054206d960831f4060e1f6a1c13076661eb687 /src/modules | |
parent | ba19753289768632b63f2028e00796ee231dc0e9 (diff) |
refactor(modules): use whoami crate to get username
Signed-off-by: bestgopher <84328409@qq.com>
Diffstat (limited to 'src/modules')
-rw-r--r-- | src/modules/aws.rs | 6 | ||||
-rw-r--r-- | src/modules/hostname.rs | 4 | ||||
-rw-r--r-- | src/modules/username.rs | 16 |
3 files changed, 10 insertions, 16 deletions
diff --git a/src/modules/aws.rs b/src/modules/aws.rs index 0d837c2ce..115c5bbbb 100644 --- a/src/modules/aws.rs +++ b/src/modules/aws.rs @@ -668,7 +668,7 @@ credential_process = /opt/bin/awscreds-retriever let expiration_env_vars = ["AWS_SESSION_EXPIRATION", "AWS_CREDENTIAL_EXPIRATION"]; 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) .unwrap(), Utc, @@ -702,7 +702,7 @@ credential_process = /opt/bin/awscreds-retriever 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(), Utc, ); @@ -789,7 +789,7 @@ aws_secret_access_key=dummy fn expiration_date_set_expired() { 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(), Utc, ); diff --git a/src/modules/hostname.rs b/src/modules/hostname.rs index 8eec9b5ff..d5e967b64 100644 --- a/src/modules/hostname.rs +++ b/src/modules/hostname.rs @@ -23,7 +23,7 @@ pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> { return None; } - let os_hostname: OsString = gethostname::gethostname(); + let os_hostname: OsString = whoami::hostname_os(); let host = match os_hostname.into_string() { Ok(host) => host, @@ -87,7 +87,7 @@ mod tests { macro_rules! get_hostname { () => { - if let Ok(hostname) = gethostname::gethostname().into_string() { + if let Ok(hostname) = whoami::hostname_os().into_string() { hostname } else { println!( diff --git a/src/modules/username.rs b/src/modules/username.rs index af3fa6e56..537e552fd 100644 --- a/src/modules/username.rs +++ b/src/modules/username.rs @@ -2,21 +2,15 @@ use super::{Context, Module, ModuleConfig}; use crate::configs::username::UsernameConfig; use crate::formatter::StringFormatter; - -#[cfg(not(target_os = "windows"))] -const USERNAME_ENV_VAR: &str = "USER"; - -#[cfg(target_os = "windows")] +#[cfg(test)] 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); @@ -151,8 +145,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; + let expected = None; assert_eq!(expected, actual); } |