diff --git a/Cargo.lock b/Cargo.lock index 2bc9833ba..467b200e3 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2884,6 +2884,7 @@ dependencies = [ "ureq", "url", "uu_cp", + "uu_whoami", "uuid", "wax", "which 5.0.0", @@ -5621,6 +5622,18 @@ dependencies = [ "xattr", ] +[[package]] +name = "uu_whoami" +version = "0.0.22" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "585d70c283e4c741889ec8575c23f91b350e4d12bfe39b938d18083816830c5d" +dependencies = [ + "clap", + "libc", + "uucore", + "windows-sys 0.48.0", +] + [[package]] name = "uucore" version = "0.0.22" diff --git a/crates/nu-command/Cargo.toml b/crates/nu-command/Cargo.toml index 49e25a476..226639369 100644 --- a/crates/nu-command/Cargo.toml +++ b/crates/nu-command/Cargo.toml @@ -88,6 +88,7 @@ unicode-segmentation = "1.10" ureq = { version = "2.8", default-features = false, features = ["charset", "gzip", "json", "native-tls"] } url = "2.2" uu_cp = "0.0.22" +uu_whoami = "0.0.22" uuid = { version = "1.5", features = ["v4"] } wax = { version = "0.6" } which = { version = "5.0", optional = true } diff --git a/crates/nu-command/src/default_context.rs b/crates/nu-command/src/default_context.rs index 0bd8cc41a..5a2c9f89a 100644 --- a/crates/nu-command/src/default_context.rs +++ b/crates/nu-command/src/default_context.rs @@ -229,6 +229,7 @@ pub fn add_shell_command_context(mut engine_state: EngineState) -> EngineState { Kill, Sleep, TermSize, + Whoami, }; // Date diff --git a/crates/nu-command/src/platform/mod.rs b/crates/nu-command/src/platform/mod.rs index 1a5bc50e0..d6a6b3a5a 100644 --- a/crates/nu-command/src/platform/mod.rs +++ b/crates/nu-command/src/platform/mod.rs @@ -6,6 +6,7 @@ mod input; mod kill; mod sleep; mod term_size; +mod whoami; pub use ansi::{Ansi, AnsiLink, AnsiStrip}; pub use clear::Clear; @@ -17,3 +18,4 @@ pub use input::InputListen; pub use kill::Kill; pub use sleep::Sleep; pub use term_size::TermSize; +pub use whoami::Whoami; diff --git a/crates/nu-command/src/platform/whoami.rs b/crates/nu-command/src/platform/whoami.rs new file mode 100644 index 000000000..016f31b60 --- /dev/null +++ b/crates/nu-command/src/platform/whoami.rs @@ -0,0 +1,71 @@ +use nu_protocol::ast::Call; +use nu_protocol::engine::{Command, EngineState, Stack}; +use nu_protocol::{ + Category, Example, IntoPipelineData, PipelineData, ShellError, Signature, Type, Value, +}; + +#[derive(Clone)] +pub struct Whoami; + +impl Command for Whoami { + fn name(&self) -> &str { + "whoami" + } + + fn usage(&self) -> &str { + "Get the current username using uutils/coreutils whoami." + } + + fn signature(&self) -> Signature { + Signature::build("whoami") + .input_output_types(vec![(Type::Nothing, Type::String)]) + .allow_variants_without_examples(true) + .category(Category::Platform) + } + + fn search_terms(&self) -> Vec<&str> { + vec!["username", "coreutils"] + } + + fn run( + &self, + _engine_state: &EngineState, + _stack: &mut Stack, + call: &Call, + _input: PipelineData, + ) -> Result { + let output = match uu_whoami::whoami() { + Ok(username) => username.to_string_lossy().to_string(), + Err(err) => { + return Err(ShellError::GenericError( + "Failed to get username".into(), + err.to_string(), + Some(call.head), + None, + Vec::new(), + )) + } + }; + + Ok(Value::string(output, call.head).into_pipeline_data()) + } + + fn examples(&self) -> Vec { + vec![Example { + description: "Get the current username", + example: "whoami", + result: None, + }] + } +} + +#[cfg(test)] +mod tests { + use super::Whoami; + + #[test] + fn examples_work_as_expected() { + use crate::test_examples; + test_examples(Whoami {}) + } +}