From 838fc7e09897d99fda554aa094ebe74a3b7537cf Mon Sep 17 00:00:00 2001 From: David Matos Date: Mon, 25 Mar 2024 22:51:50 +0100 Subject: [PATCH] Initial implementation for uutils uname (#11684) Hi, This PR aims at implementing the first iteration for `uname` using `uutils`. Couple of things: * Currently my [PR](https://github.com/uutils/coreutils/pull/5921) to make the required changes is pending in `uutils` repo. * I guess the number of flags has to be investigated. Still the tests cover all of them. # Description # User-Facing Changes # Tests + Formatting Don't forget to add tests that cover your changes. Make sure you've run and fixed any issues with these commands: - [X] `cargo fmt --all -- --check` to check standard code formatting (`cargo fmt --all` applies these changes) - [X] `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used` to check that you're using the standard code style - [X] `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)) - [X] `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 --------- Co-authored-by: Darren Schroeder <343840+fdncred@users.noreply.github.com> --- Cargo.lock | 74 +++++++++++----- Cargo.toml | 13 +-- crates/nu-command/Cargo.toml | 1 + crates/nu-command/src/default_context.rs | 2 + crates/nu-command/src/system/mod.rs | 2 + crates/nu-command/src/system/uname.rs | 100 ++++++++++++++++++++++ crates/nu-command/tests/commands/mod.rs | 2 + crates/nu-command/tests/commands/uname.rs | 12 +++ 8 files changed, 180 insertions(+), 26 deletions(-) create mode 100644 crates/nu-command/src/system/uname.rs create mode 100644 crates/nu-command/tests/commands/uname.rs diff --git a/Cargo.lock b/Cargo.lock index 96eed21aa8..b01b845271 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2176,9 +2176,9 @@ dependencies = [ [[package]] name = "libc" -version = "0.2.152" +version = "0.2.153" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "13e3bf6590cbc649f4d1a3eefc9d5d6eb746f5200ffb04e5e142700b8faa56e7" +checksum = "9c198f91728a82281a64e1f4f9eeb25d82cb32a5de251c6bd1b5154d63a8e7bd" [[package]] name = "libflate" @@ -2636,6 +2636,18 @@ dependencies = [ "libc", ] +[[package]] +name = "nix" +version = "0.28.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ab2156c4fce2f8df6c499cc1c763e4394b7482525bf2a9701c9d79d215f519e4" +dependencies = [ + "bitflags 2.4.2", + "cfg-if", + "cfg_aliases", + "libc", +] + [[package]] name = "nom" version = "7.1.3" @@ -2957,6 +2969,7 @@ dependencies = [ "uu_mkdir", "uu_mktemp", "uu_mv", + "uu_uname", "uu_whoami", "uucore", "uuid", @@ -3780,6 +3793,16 @@ dependencies = [ "array-init-cursor", ] +[[package]] +name = "platform-info" +version = "2.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d6259c4860e53bf665016f1b2f46a8859cadfa717581dc9d597ae4069de6300f" +dependencies = [ + "libc", + "winapi", +] + [[package]] name = "polars" version = "0.37.0" @@ -5510,9 +5533,9 @@ checksum = "cfb5fa503293557c5158bd215fdc225695e567a77e453f5d4452a50a193969bd" [[package]] name = "tempfile" -version = "3.10.0" +version = "3.10.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a365e8cd18e44762ef95d87f284f4b5cd04107fec2ff3052bd6a3e6069669e67" +checksum = "85b77fafb263dd9d05cbeac119526425676db3784113aa9295c88498cbf8bff1" dependencies = [ "cfg-if", "fastrand", @@ -5983,9 +6006,9 @@ checksum = "711b9620af191e0cdc7468a8d14e709c3dcdb115b36f838e601583af800a370a" [[package]] name = "uu_cp" -version = "0.0.24" +version = "0.0.25" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "927869e2df48ac2f09ebc083459e96efc97028863a41dce96cfe8164c5c722ba" +checksum = "fcbe045dc92209114afdfd366bd18f7b95dbf999f3eaa85ad6dca910b0be3d56" dependencies = [ "clap", "filetime", @@ -5999,9 +6022,9 @@ dependencies = [ [[package]] name = "uu_mkdir" -version = "0.0.24" +version = "0.0.25" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9d303b73c3a1ede83101c4a01c7f2f72f000ec9241a5d788ceba6a91a9ce1cb3" +checksum = "040aa4584036b2f65e05387b0ea9ac468afce1db325743ce5f350689fd9ce4ae" dependencies = [ "clap", "uucore", @@ -6009,9 +6032,9 @@ dependencies = [ [[package]] name = "uu_mktemp" -version = "0.0.24" +version = "0.0.25" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c7e25b0928c96297836d3400c049d7c772073d0534720e4d13dd0e4a6ed52349" +checksum = "f240a99c36d768153874d198c43605a45c86996b576262689a0f18248cc3bc57" dependencies = [ "clap", "rand", @@ -6021,9 +6044,9 @@ dependencies = [ [[package]] name = "uu_mv" -version = "0.0.24" +version = "0.0.25" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8ec472139edb0e91d9706779bfdf45814b139d8b23bea2ad0feaaac1fb6c1523" +checksum = "0c99fd7c75e6e85553c92537314be3d9a64b4927051aa1608513feea2f933022" dependencies = [ "clap", "fs_extra", @@ -6032,10 +6055,21 @@ dependencies = [ ] [[package]] -name = "uu_whoami" -version = "0.0.24" +name = "uu_uname" +version = "0.0.25" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "521f16cb67ba9f55341e00cd6b806556cefef430e9d80ea425607628da3a40e5" +checksum = "5951832d73199636bde6c0d61cf960932b3c4450142c290375bc10c7abed6db5" +dependencies = [ + "clap", + "platform-info", + "uucore", +] + +[[package]] +name = "uu_whoami" +version = "0.0.25" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e3b44166eb6335aeac42744ea368cc4c32d3f2287a4ff765a5ce44d927ab8bb4" dependencies = [ "clap", "libc", @@ -6045,15 +6079,15 @@ dependencies = [ [[package]] name = "uucore" -version = "0.0.24" +version = "0.0.25" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d5de2eba1364f6274f35f121eb8671b98ac5fa8fe1271694721e17034e85e8bc" +checksum = "23994a722acb43dbc56877e271c9723f167ae42c4c089f909b2d7dd106c3a9b4" dependencies = [ "clap", "dunce", "glob", "libc", - "nix 0.27.1", + "nix 0.28.0", "once_cell", "os_display", "uucore_procs", @@ -6162,9 +6196,9 @@ dependencies = [ [[package]] name = "walkdir" -version = "2.4.0" +version = "2.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d71d857dc86794ca4c280d616f7da00d2dbfd8cd788846559a6813e6aa4b54ee" +checksum = "29790946404f91d9c5d06f9874efddea1dc06c5efe94541a7d6863108e3a5e4b" dependencies = [ "same-file", "winapi-util", diff --git a/Cargo.toml b/Cargo.toml index b42db1cc3f..fb2089af18 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -147,12 +147,13 @@ unicode-segmentation = "1.11" unicode-width = "0.1" ureq = { version = "2.9", default-features = false } url = "2.2" -uu_cp = "0.0.24" -uu_mkdir = "0.0.24" -uu_mktemp = "0.0.24" -uu_mv = "0.0.24" -uu_whoami = "0.0.24" -uucore = "0.0.24" +uu_cp = "0.0.25" +uu_mkdir = "0.0.25" +uu_mktemp = "0.0.25" +uu_mv = "0.0.25" +uu_whoami = "0.0.25" +uu_uname = "0.0.25" +uucore = "0.0.25" uuid = "1.8.0" v_htmlescape = "0.15.0" wax = "0.6" diff --git a/crates/nu-command/Cargo.toml b/crates/nu-command/Cargo.toml index cabf8bd8bf..2edb8f0571 100644 --- a/crates/nu-command/Cargo.toml +++ b/crates/nu-command/Cargo.toml @@ -91,6 +91,7 @@ uu_cp = { workspace = true } uu_mkdir = { workspace = true } uu_mktemp = { workspace = true } uu_mv = { workspace = true } +uu_uname = { workspace = true } uu_whoami = { workspace = true } uuid = { workspace = true, features = ["v4"] } v_htmlescape = { workspace = true } diff --git a/crates/nu-command/src/default_context.rs b/crates/nu-command/src/default_context.rs index c7a5f862ca..dc27d5d2ce 100644 --- a/crates/nu-command/src/default_context.rs +++ b/crates/nu-command/src/default_context.rs @@ -122,6 +122,8 @@ pub fn add_shell_command_context(mut engine_state: EngineState) -> EngineState { Exec, NuCheck, Sys, + UName, + }; // Help diff --git a/crates/nu-command/src/system/mod.rs b/crates/nu-command/src/system/mod.rs index 775de72a0c..32141832e9 100644 --- a/crates/nu-command/src/system/mod.rs +++ b/crates/nu-command/src/system/mod.rs @@ -13,6 +13,7 @@ mod ps; mod registry_query; mod run_external; mod sys; +mod uname; mod which_; pub use complete::Complete; @@ -30,4 +31,5 @@ pub use ps::Ps; pub use registry_query::RegistryQuery; pub use run_external::{External, ExternalCommand}; pub use sys::Sys; +pub use uname::UName; pub use which_::Which; diff --git a/crates/nu-command/src/system/uname.rs b/crates/nu-command/src/system/uname.rs new file mode 100644 index 0000000000..04d01cc693 --- /dev/null +++ b/crates/nu-command/src/system/uname.rs @@ -0,0 +1,100 @@ +use nu_protocol::record; +use nu_protocol::Value; +use nu_protocol::{ + ast::Call, + engine::{Command, EngineState, Stack}, + Category, Example, PipelineData, ShellError, Signature, Type, +}; + +#[derive(Clone)] +pub struct UName; + +impl Command for UName { + fn name(&self) -> &str { + "uname" + } + + fn signature(&self) -> Signature { + Signature::build("uname") + .input_output_types(vec![(Type::Nothing, Type::Table(vec![]))]) + .category(Category::System) + } + + fn usage(&self) -> &str { + "Print certain system information." + } + + fn search_terms(&self) -> Vec<&str> { + // add other terms? + vec!["system"] + } + + fn run( + &self, + _engine_state: &EngineState, + _stack: &mut Stack, + call: &Call, + _input: PipelineData, + ) -> Result { + let span = call.head; + // Simulate `uname -all` is called every time + let opts = uu_uname::Options { + all: true, + kernel_name: false, + nodename: false, + kernel_release: false, + kernel_version: false, + machine: false, + processor: false, + hardware_platform: false, + os: false, + }; + let output = uu_uname::UNameOutput::new(&opts).map_err(|e| ShellError::GenericError { + error: format!("{}", e), + msg: format!("{}", e), + span: None, + help: None, + inner: Vec::new(), + })?; + let outputs = [ + output.kernel_name, + output.nodename, + output.kernel_release, + output.kernel_version, + output.machine, + output.os, + ]; + let outputs = outputs + .iter() + .map(|name| { + Ok(name + .as_ref() + .ok_or("unknown") + .map_err(|_| ShellError::NotFound { span })? + .to_string()) + }) + .collect::, ShellError>>()?; + Ok(PipelineData::Value( + Value::record( + record! { + "kernel-name" => Value::string(outputs[0].clone(), span), + "nodename" => Value::string(outputs[1].clone(), span), + "kernel-release" => Value::string(outputs[2].clone(), span), + "kernel-version" => Value::string(outputs[3].clone(), span), + "machine" => Value::string(outputs[4].clone(), span), + "operating-system" => Value::string(outputs[5].clone(), span), + }, + span, + ), + None, + )) + } + + fn examples(&self) -> Vec { + vec![Example { + description: "Print all information", + example: "uname", + result: None, + }] + } +} diff --git a/crates/nu-command/tests/commands/mod.rs b/crates/nu-command/tests/commands/mod.rs index 7dbcf7e40e..956f26f083 100644 --- a/crates/nu-command/tests/commands/mod.rs +++ b/crates/nu-command/tests/commands/mod.rs @@ -111,7 +111,9 @@ mod try_; mod ucp; #[cfg(unix)] mod ulimit; + mod umkdir; +mod uname; mod uniq; mod uniq_by; mod update; diff --git a/crates/nu-command/tests/commands/uname.rs b/crates/nu-command/tests/commands/uname.rs new file mode 100644 index 0000000000..2291bb518b --- /dev/null +++ b/crates/nu-command/tests/commands/uname.rs @@ -0,0 +1,12 @@ +use nu_test_support::nu; +use nu_test_support::playground::Playground; +#[test] +fn test_uname_all() { + Playground::setup("uname_test_1", |dirs, _| { + let actual = nu!( + cwd: dirs.test(), + "uname" + ); + assert!(actual.status.success()) + }) +}