From 72a21ad619dcd978c66a7149258c9c9c3b7c24c5 Mon Sep 17 00:00:00 2001 From: "Joseph T. Lyons" Date: Thu, 25 Jun 2020 01:51:09 -0400 Subject: [PATCH] Adds random command with uuid subcommand (#2050) --- Cargo.lock | 10 ++++ crates/nu-cli/Cargo.toml | 1 + crates/nu-cli/src/cli.rs | 3 + crates/nu-cli/src/commands.rs | 2 + crates/nu-cli/src/commands/random/command.rs | 32 +++++++++++ crates/nu-cli/src/commands/random/mod.rs | 5 ++ crates/nu-cli/src/commands/random/uuid.rs | 59 ++++++++++++++++++++ crates/nu-cli/tests/commands/mod.rs | 1 + crates/nu-cli/tests/commands/random/mod.rs | 1 + crates/nu-cli/tests/commands/random/uuid.rs | 16 ++++++ docs/commands/random.md | 12 ++++ 11 files changed, 142 insertions(+) create mode 100644 crates/nu-cli/src/commands/random/command.rs create mode 100644 crates/nu-cli/src/commands/random/mod.rs create mode 100644 crates/nu-cli/src/commands/random/uuid.rs create mode 100644 crates/nu-cli/tests/commands/random/mod.rs create mode 100644 crates/nu-cli/tests/commands/random/uuid.rs create mode 100644 docs/commands/random.md diff --git a/Cargo.lock b/Cargo.lock index d8b5ed2d1..40376b747 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2505,6 +2505,7 @@ dependencies = [ "umask", "unicode-xid", "users", + "uuid", "which", ] @@ -4459,6 +4460,15 @@ version = "0.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "936e4b492acfd135421d8dca4b1aa80a7bfc26e702ef3af710e0752684df5372" +[[package]] +name = "uuid" +version = "0.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9fde2f6a4bea1d6e007c4ad38c6839fa71cbb63b6dbf5b595aa38dc9b1093c11" +dependencies = [ + "rand", +] + [[package]] name = "vcpkg" version = "0.2.10" diff --git a/crates/nu-cli/Cargo.toml b/crates/nu-cli/Cargo.toml index 7179811a8..9a8792596 100644 --- a/crates/nu-cli/Cargo.toml +++ b/crates/nu-cli/Cargo.toml @@ -85,6 +85,7 @@ toml = "0.5.6" typetag = "0.1.4" umask = "1.0.0" unicode-xid = "0.2.0" +uuid_crate = { package = "uuid", version = "0.8.1", features = ["v4"] } which = "4.0.1" trash = { version = "1.0.1", optional = true } diff --git a/crates/nu-cli/src/cli.rs b/crates/nu-cli/src/cli.rs index 480a35353..e9f22265c 100644 --- a/crates/nu-cli/src/cli.rs +++ b/crates/nu-cli/src/cli.rs @@ -387,6 +387,9 @@ pub fn create_default_context( whole_stream_command(FromVcf), // "Private" commands (not intended to be accessed directly) whole_stream_command(RunExternalCommand { interactive }), + // Random value generation + whole_stream_command(Random), + whole_stream_command(RandomUUID), ]); cfg_if::cfg_if! { diff --git a/crates/nu-cli/src/commands.rs b/crates/nu-cli/src/commands.rs index 49c708267..9de323cd4 100644 --- a/crates/nu-cli/src/commands.rs +++ b/crates/nu-cli/src/commands.rs @@ -81,6 +81,7 @@ pub(crate) mod plugin; pub(crate) mod prepend; pub(crate) mod prev; pub(crate) mod pwd; +pub(crate) mod random; pub(crate) mod range; #[allow(unused)] pub(crate) mod reduce_by; @@ -212,6 +213,7 @@ pub(crate) use pivot::Pivot; pub(crate) use prepend::Prepend; pub(crate) use prev::Previous; pub(crate) use pwd::Pwd; +pub(crate) use random::{Random, RandomUUID}; pub(crate) use range::Range; #[allow(unused_imports)] pub(crate) use reduce_by::ReduceBy; diff --git a/crates/nu-cli/src/commands/random/command.rs b/crates/nu-cli/src/commands/random/command.rs new file mode 100644 index 000000000..56ea1324a --- /dev/null +++ b/crates/nu-cli/src/commands/random/command.rs @@ -0,0 +1,32 @@ +use crate::commands::WholeStreamCommand; +use crate::prelude::*; +use nu_errors::ShellError; +use nu_protocol::{ReturnSuccess, Signature, UntaggedValue}; + +pub struct Command; + +#[async_trait] +impl WholeStreamCommand for Command { + fn name(&self) -> &str { + "random" + } + + fn signature(&self) -> Signature { + Signature::build("random") + } + + fn usage(&self) -> &str { + "Generate random values" + } + + async fn run( + &self, + _args: CommandArgs, + registry: &CommandRegistry, + ) -> Result { + Ok(OutputStream::one(Ok(ReturnSuccess::Value( + UntaggedValue::string(crate::commands::help::get_help(&Command, ®istry.clone())) + .into_value(Tag::unknown()), + )))) + } +} diff --git a/crates/nu-cli/src/commands/random/mod.rs b/crates/nu-cli/src/commands/random/mod.rs new file mode 100644 index 000000000..b17d5b6c1 --- /dev/null +++ b/crates/nu-cli/src/commands/random/mod.rs @@ -0,0 +1,5 @@ +pub mod command; +pub mod uuid; + +pub use command::Command as Random; +pub use uuid::SubCommand as RandomUUID; diff --git a/crates/nu-cli/src/commands/random/uuid.rs b/crates/nu-cli/src/commands/random/uuid.rs new file mode 100644 index 000000000..84372b9df --- /dev/null +++ b/crates/nu-cli/src/commands/random/uuid.rs @@ -0,0 +1,59 @@ +use crate::commands::WholeStreamCommand; +use crate::prelude::*; +use nu_errors::ShellError; +use nu_protocol::{ReturnSuccess, Signature}; +use uuid_crate::Uuid; + +pub struct SubCommand; + +#[async_trait] +impl WholeStreamCommand for SubCommand { + fn name(&self) -> &str { + "random uuid" + } + + fn signature(&self) -> Signature { + Signature::build("random uuid") + } + + fn usage(&self) -> &str { + "Generate a random uuid4 string" + } + + async fn run( + &self, + args: CommandArgs, + registry: &CommandRegistry, + ) -> Result { + uuid(args, registry).await + } + + fn examples(&self) -> Vec { + vec![Example { + description: "Generate a random uuid4 string", + example: "random uuid", + result: None, + }] + } +} + +pub async fn uuid( + _args: CommandArgs, + _registry: &CommandRegistry, +) -> Result { + let uuid_4 = Uuid::new_v4().to_hyphenated().to_string(); + + Ok(OutputStream::one(ReturnSuccess::value(uuid_4))) +} + +#[cfg(test)] +mod tests { + use super::SubCommand; + + #[test] + fn examples_work_as_expected() { + use crate::examples::test as test_examples; + + test_examples(SubCommand {}) + } +} diff --git a/crates/nu-cli/tests/commands/mod.rs b/crates/nu-cli/tests/commands/mod.rs index 614edd477..6fc9cde72 100644 --- a/crates/nu-cli/tests/commands/mod.rs +++ b/crates/nu-cli/tests/commands/mod.rs @@ -31,6 +31,7 @@ mod mv; mod open; mod parse; mod prepend; +mod random; mod range; mod rename; mod reverse; diff --git a/crates/nu-cli/tests/commands/random/mod.rs b/crates/nu-cli/tests/commands/random/mod.rs new file mode 100644 index 000000000..14aae177b --- /dev/null +++ b/crates/nu-cli/tests/commands/random/mod.rs @@ -0,0 +1 @@ +mod uuid; diff --git a/crates/nu-cli/tests/commands/random/uuid.rs b/crates/nu-cli/tests/commands/random/uuid.rs new file mode 100644 index 000000000..da64a48c9 --- /dev/null +++ b/crates/nu-cli/tests/commands/random/uuid.rs @@ -0,0 +1,16 @@ +use nu_test_support::{nu, pipeline}; +use uuid_crate::Uuid; + +#[test] +fn makes_valid_uuid4() { + let actual = nu!( + cwd: ".", pipeline( + r#" + random uuid + "# + )); + + let result = Uuid::parse_str(actual.out.as_str()); + + assert!(result.is_ok()); +} diff --git a/docs/commands/random.md b/docs/commands/random.md new file mode 100644 index 000000000..6ed796777 --- /dev/null +++ b/docs/commands/random.md @@ -0,0 +1,12 @@ +# random + +Use `random` to generate random values + +* `random uuid`: Generate a random uuid4 string + +## Examples + +```shell +> random uuid +8af4de39-acbc-42f0-94d1-7cfad6c01f8b +```