feat: add the whoami command
This commit is contained in:
parent
8fccfd25c0
commit
6e24462cae
3 changed files with 24 additions and 0 deletions
|
|
@ -17,3 +17,4 @@ command!(test::Test);
|
|||
command!(yes::Yes);
|
||||
command!(pwd::Pwd);
|
||||
command!(sleep::Sleep);
|
||||
command!(whoami::WhoAmI);
|
||||
|
|
|
|||
22
coreutils/src/commands/whoami.rs
Normal file
22
coreutils/src/commands/whoami.rs
Normal 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);
|
||||
}
|
||||
}
|
||||
|
|
@ -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
|
||||
});
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue