From e2fb0e5b82794e3ff4743ab4e99ab876bc9ee5db Mon Sep 17 00:00:00 2001 From: Terts Diepraam Date: Wed, 25 Oct 2023 16:53:52 +0200 Subject: [PATCH] implement whoami using uutils (#10488) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit # Description Implements `whoami` using the `whoami` command from uutils as backend. This is a draft because it depends on https://github.com/uutils/coreutils/pull/5310 and a new release of uutils needs to be made (and the paths in `Cargo.toml` should be updated). At this point, this is more of a proof of concept 😄 Additionally, this implements a (simple and naive) conversion from the uutils `UResult` to the nushell `ShellError`, which should help with the integration of other utils, too. I can split that off into a separate PR if desired. I put this command in the "platform" category. If it should go somewhere else, let me know! The tests will currently fail, because I've used a local path to uutils. Once the PR on the uutils side is merged, I'll update it to a git path so that it can be tested and runs on more machines than just mine. # User-Facing Changes New `whoami` command. This might break some users who expect the system `whoami` command. However, the result of this new command should be very close, just with a nicer help message, at least for Linux users. The default `whoami` on Windows is quite different from this implementation: https://learn.microsoft.com/en-us/windows-server/administration/windows-commands/whoami # Tests + Formatting # After Submitting --------- Co-authored-by: Darren Schroeder <343840+fdncred@users.noreply.github.com> --- Cargo.lock | 13 +++++ crates/nu-command/Cargo.toml | 1 + crates/nu-command/src/default_context.rs | 1 + crates/nu-command/src/platform/mod.rs | 2 + crates/nu-command/src/platform/whoami.rs | 71 ++++++++++++++++++++++++ 5 files changed, 88 insertions(+) create mode 100644 crates/nu-command/src/platform/whoami.rs diff --git a/Cargo.lock b/Cargo.lock index 2bc9833ba7..467b200e3d 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 49e25a4765..226639369e 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 0bd8cc41a6..5a2c9f89a3 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 1a5bc50e03..d6a6b3a5ad 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 0000000000..016f31b608 --- /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 {}) + } +}