Adds random command with uuid subcommand (#2050)

This commit is contained in:
Joseph T. Lyons
2020-06-25 01:51:09 -04:00
committed by GitHub
parent 6372d2a18c
commit 72a21ad619
11 changed files with 142 additions and 0 deletions

View File

@ -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! {

View File

@ -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;

View File

@ -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<OutputStream, ShellError> {
Ok(OutputStream::one(Ok(ReturnSuccess::Value(
UntaggedValue::string(crate::commands::help::get_help(&Command, &registry.clone()))
.into_value(Tag::unknown()),
))))
}
}

View File

@ -0,0 +1,5 @@
pub mod command;
pub mod uuid;
pub use command::Command as Random;
pub use uuid::SubCommand as RandomUUID;

View File

@ -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<OutputStream, ShellError> {
uuid(args, registry).await
}
fn examples(&self) -> Vec<Example> {
vec![Example {
description: "Generate a random uuid4 string",
example: "random uuid",
result: None,
}]
}
}
pub async fn uuid(
_args: CommandArgs,
_registry: &CommandRegistry,
) -> Result<OutputStream, ShellError> {
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 {})
}
}