Skip to content

Commit

Permalink
implement whoami using uutils (#10488)
Browse files Browse the repository at this point in the history
<!--
if this PR closes one or more issues, you can automatically link the PR
with
them by using one of the [*linking
keywords*](https://docs.github.com/en/issues/tracking-your-work-with-issues/linking-a-pull-request-to-an-issue#linking-a-pull-request-to-an-issue-using-a-keyword),
e.g.
- this PR should close #xxxx
- fixes #xxxx

you can also mention related issues, PRs or discussions!
-->

# Description
<!--
Thank you for improving Nushell. Please, check our [contributing
guide](../CONTRIBUTING.md) and talk to the core team before making major
changes.

Description of your pull request goes here. **Provide examples and/or
screenshots** if your changes affect the user experience.
-->

Implements `whoami` using the `whoami` command from uutils as backend.
This is a draft because it depends on
uutils/coreutils#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
<!-- List of all changes that impact the user experience here. This
helps us keep track of breaking 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
<!--
Don't forget to add tests that cover your changes.

Make sure you've run and fixed any issues with these commands:

- `cargo fmt --all -- --check` to check standard code formatting (`cargo
fmt --all` applies these changes)
- `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used` to
check that you're using the standard code style
- `cargo test --workspace` to check that all tests pass (on Windows make
sure to [enable developer
mode](https://learn.microsoft.com/en-us/windows/apps/get-started/developer-mode-features-and-debugging))
- `cargo run -- -c "use std testing; testing run-tests --path
crates/nu-std"` to run the tests for the standard library

> **Note**
> from `nushell` you can also use the `toolkit` as follows
> ```bash
> use toolkit.nu # or use an `env_change` hook to activate it
automatically
> toolkit check pr
> ```
-->

# After Submitting
<!-- If your PR had any user-facing changes, update [the
documentation](https://github.com/nushell/nushell.github.io) after the
PR is merged, if necessary. This will help us keep the docs up to date.
-->

---------

Co-authored-by: Darren Schroeder <343840+fdncred@users.noreply.github.com>
  • Loading branch information
tertsdiepraam and fdncred committed Oct 25, 2023
1 parent a11e413 commit e2fb0e5
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 0 deletions.
13 changes: 13 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/nu-command/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
1 change: 1 addition & 0 deletions crates/nu-command/src/default_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,7 @@ pub fn add_shell_command_context(mut engine_state: EngineState) -> EngineState {
Kill,
Sleep,
TermSize,
Whoami,
};

// Date
Expand Down
2 changes: 2 additions & 0 deletions crates/nu-command/src/platform/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
71 changes: 71 additions & 0 deletions crates/nu-command/src/platform/whoami.rs
Original file line number Diff line number Diff line change
@@ -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<PipelineData, ShellError> {
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<Example> {
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 {})
}
}

0 comments on commit e2fb0e5

Please sign in to comment.