feat: add the whoami command

This commit is contained in:
Teesh 2025-03-25 13:18:35 +02:00
parent 8fccfd25c0
commit 6e24462cae
3 changed files with 24 additions and 0 deletions

View file

@ -17,3 +17,4 @@ command!(test::Test);
command!(yes::Yes);
command!(pwd::Pwd);
command!(sleep::Sleep);
command!(whoami::WhoAmI);

View file

@ -0,0 +1,22 @@
use boxutils::args::ArgParser;
use boxutils::commands::Command;
use std::env;
pub struct WhoAmI;
impl Command for WhoAmI {
fn execute(&self) {
let args = ArgParser::builder().add_flag("--help").parse_args("whoami");
if args.get_flag("--help") {
println!("Usage: whoami");
return;
}
let username = env::var("USER") // Unix
.or_else(|_| env::var("USERNAME")) // Windows
.unwrap_or_else(|_| "unknown".to_string());
println!("{}", username);
}
}

View file

@ -27,6 +27,7 @@ pub fn get_registry() -> CommandRegistry {
"yes" => coreutils::commands::Yes,
"pwd" => coreutils::commands::Pwd,
"sleep" => coreutils::commands::Sleep,
"whoami" => coreutils::commands::WhoAmI,
"box" => Boxcmd
});