feat: add get_username to user
This commit is contained in:
parent
900f84f7ec
commit
51659bde16
3 changed files with 15 additions and 7 deletions
|
|
@ -1,6 +1,6 @@
|
|||
use boxutils::args::ArgParser;
|
||||
use boxutils::commands::Command;
|
||||
use std::env;
|
||||
use boxutils::cross::user;
|
||||
|
||||
pub struct WhoAmI;
|
||||
|
||||
|
|
@ -13,9 +13,7 @@ impl Command for WhoAmI {
|
|||
return;
|
||||
}
|
||||
|
||||
let username = env::var("USER") // Unix
|
||||
.or_else(|_| env::var("USERNAME")) // Windows
|
||||
.unwrap_or_else(|_| "unknown".to_string());
|
||||
let username = user::get_username().unwrap_or_else(|| "unknown".to_string());
|
||||
|
||||
println!("{}", username);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -35,3 +35,7 @@ impl UserPrivileges {
|
|||
pub fn is_admin() -> bool {
|
||||
UserPrivileges::is_root()
|
||||
}
|
||||
|
||||
pub fn get_username() -> Option<String> {
|
||||
std::env::var("USER").ok()
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,10 +1,10 @@
|
|||
#![allow(dead_code)]
|
||||
#![cfg(windows)]
|
||||
|
||||
use std::ptr;
|
||||
use std::ffi::c_void;
|
||||
use std::mem::{size_of, zeroed};
|
||||
use std::os::raw::c_ulong;
|
||||
use std::ptr;
|
||||
|
||||
#[link(name = "advapi32")]
|
||||
unsafe extern "system" {
|
||||
|
|
@ -44,7 +44,9 @@ impl AccessToken {
|
|||
return None;
|
||||
}
|
||||
|
||||
Some(Self { handle: token_handle })
|
||||
Some(Self {
|
||||
handle: token_handle,
|
||||
})
|
||||
}
|
||||
|
||||
/// Checks if the token is elevated (i.e., running as an administrator).
|
||||
|
|
@ -80,4 +82,8 @@ pub fn is_admin() -> bool {
|
|||
} else {
|
||||
false
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_username() -> Option<String> {
|
||||
std::env::var("USERNAME").ok()
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue