mirror of
https://github.com/nushell/nushell.git
synced 2024-12-22 15:13:01 +01:00
implement whoami using uutils (#10488)
<!-- 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 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 <!-- 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>
This commit is contained in:
parent
a11e41332c
commit
e2fb0e5b82
13
Cargo.lock
generated
13
Cargo.lock
generated
@ -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"
|
||||
|
@ -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 }
|
||||
|
@ -229,6 +229,7 @@ pub fn add_shell_command_context(mut engine_state: EngineState) -> EngineState {
|
||||
Kill,
|
||||
Sleep,
|
||||
TermSize,
|
||||
Whoami,
|
||||
};
|
||||
|
||||
// Date
|
||||
|
@ -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;
|
||||
|
71
crates/nu-command/src/platform/whoami.rs
Normal file
71
crates/nu-command/src/platform/whoami.rs
Normal file
@ -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 {})
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user