From 6e24462cae3e60fd925ee39db0dcb0d0fa549fcd Mon Sep 17 00:00:00 2001 From: teesh3rt Date: Tue, 25 Mar 2025 13:18:35 +0200 Subject: [PATCH] feat: add the whoami command --- coreutils/src/commands/mod.rs | 1 + coreutils/src/commands/whoami.rs | 22 ++++++++++++++++++++++ src/registry.rs | 1 + 3 files changed, 24 insertions(+) create mode 100644 coreutils/src/commands/whoami.rs diff --git a/coreutils/src/commands/mod.rs b/coreutils/src/commands/mod.rs index fac4439..10da5b2 100644 --- a/coreutils/src/commands/mod.rs +++ b/coreutils/src/commands/mod.rs @@ -17,3 +17,4 @@ command!(test::Test); command!(yes::Yes); command!(pwd::Pwd); command!(sleep::Sleep); +command!(whoami::WhoAmI); diff --git a/coreutils/src/commands/whoami.rs b/coreutils/src/commands/whoami.rs new file mode 100644 index 0000000..dd67b7a --- /dev/null +++ b/coreutils/src/commands/whoami.rs @@ -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); + } +} diff --git a/src/registry.rs b/src/registry.rs index a3ae5e4..06f2b40 100644 --- a/src/registry.rs +++ b/src/registry.rs @@ -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 });