mirror of
https://github.com/nushell/nushell.git
synced 2025-08-12 13:27:47 +02:00
nu-cli refactor moving commands into their own crate nu-command (#2910)
* move commands, futures.rs, script.rs, utils * move over maybe_print_errors * add nu_command crate references to nu_cli * in commands.rs open up to pub mod from pub(crate) * nu-cli, nu-command, and nu tests are now passing * cargo fmt * clean up nu-cli/src/prelude.rs * code cleanup * for some reason lex.rs was not formatted, may be causing my error * remove mod completion from lib.rs which was not being used along with quickcheck macros * add in allow unused imports * comment out one failing external test; comment out one failing internal test * revert commenting out failing tests; something else might be going on; someone with a windows machine should check and see what is going on with these failing windows tests * Update Cargo.toml Extend the optional features to nu-command Co-authored-by: Jonathan Turner <jonathandturner@users.noreply.github.com>
This commit is contained in:
91
crates/nu-command/src/commands/random/bool.rs
Normal file
91
crates/nu-command/src/commands/random/bool.rs
Normal file
@ -0,0 +1,91 @@
|
||||
use crate::prelude::*;
|
||||
use nu_engine::WholeStreamCommand;
|
||||
use nu_errors::ShellError;
|
||||
use nu_protocol::{ReturnSuccess, Signature, SyntaxShape, UntaggedValue};
|
||||
use nu_source::Tagged;
|
||||
use rand::prelude::{thread_rng, Rng};
|
||||
|
||||
pub struct SubCommand;
|
||||
|
||||
#[derive(Deserialize)]
|
||||
pub struct BoolArgs {
|
||||
bias: Option<Tagged<f64>>,
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl WholeStreamCommand for SubCommand {
|
||||
fn name(&self) -> &str {
|
||||
"random bool"
|
||||
}
|
||||
|
||||
fn signature(&self) -> Signature {
|
||||
Signature::build("random bool").named(
|
||||
"bias",
|
||||
SyntaxShape::Number,
|
||||
"Adjusts the probability of a \"true\" outcome",
|
||||
Some('b'),
|
||||
)
|
||||
}
|
||||
|
||||
fn usage(&self) -> &str {
|
||||
"Generate a random boolean value"
|
||||
}
|
||||
|
||||
async fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
|
||||
bool_command(args).await
|
||||
}
|
||||
|
||||
fn examples(&self) -> Vec<Example> {
|
||||
vec![
|
||||
Example {
|
||||
description: "Generate a random boolean value",
|
||||
example: "random bool",
|
||||
result: None,
|
||||
},
|
||||
Example {
|
||||
description: "Generate a random boolean value with a 75% chance of \"true\"",
|
||||
example: "random bool --bias 0.75",
|
||||
result: None,
|
||||
},
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn bool_command(args: CommandArgs) -> Result<OutputStream, ShellError> {
|
||||
let (BoolArgs { bias }, _) = args.process().await?;
|
||||
|
||||
let mut probability = 0.5;
|
||||
|
||||
if let Some(prob) = bias {
|
||||
probability = *prob as f64;
|
||||
|
||||
let probability_is_valid = (0.0..=1.0).contains(&probability);
|
||||
|
||||
if !probability_is_valid {
|
||||
return Err(ShellError::labeled_error(
|
||||
"The probability is invalid",
|
||||
"invalid probability",
|
||||
prob.span(),
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
let mut rng = thread_rng();
|
||||
let bool_result: bool = rng.gen_bool(probability);
|
||||
let bool_untagged_value = UntaggedValue::boolean(bool_result);
|
||||
|
||||
Ok(OutputStream::one(ReturnSuccess::value(bool_untagged_value)))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::ShellError;
|
||||
use super::SubCommand;
|
||||
|
||||
#[test]
|
||||
fn examples_work_as_expected() -> Result<(), ShellError> {
|
||||
use crate::examples::test as test_examples;
|
||||
|
||||
Ok(test_examples(SubCommand {})?)
|
||||
}
|
||||
}
|
82
crates/nu-command/src/commands/random/chars.rs
Normal file
82
crates/nu-command/src/commands/random/chars.rs
Normal file
@ -0,0 +1,82 @@
|
||||
use crate::prelude::*;
|
||||
use nu_engine::WholeStreamCommand;
|
||||
use nu_errors::ShellError;
|
||||
use nu_protocol::{ReturnSuccess, Signature, SyntaxShape, UntaggedValue};
|
||||
use nu_source::Tagged;
|
||||
use rand::distributions::Alphanumeric;
|
||||
use rand::prelude::{thread_rng, Rng};
|
||||
|
||||
pub struct SubCommand;
|
||||
|
||||
#[derive(Deserialize)]
|
||||
pub struct CharsArgs {
|
||||
length: Option<Tagged<u32>>,
|
||||
}
|
||||
|
||||
const DEFAULT_CHARS_LENGTH: u32 = 25;
|
||||
|
||||
#[async_trait]
|
||||
impl WholeStreamCommand for SubCommand {
|
||||
fn name(&self) -> &str {
|
||||
"random chars"
|
||||
}
|
||||
|
||||
fn signature(&self) -> Signature {
|
||||
Signature::build("random chars").named(
|
||||
"length",
|
||||
SyntaxShape::Int,
|
||||
"Number of chars",
|
||||
Some('l'),
|
||||
)
|
||||
}
|
||||
|
||||
fn usage(&self) -> &str {
|
||||
"Generate random chars"
|
||||
}
|
||||
|
||||
async fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
|
||||
chars(args).await
|
||||
}
|
||||
|
||||
fn examples(&self) -> Vec<Example> {
|
||||
vec![
|
||||
Example {
|
||||
description: "Generate random chars",
|
||||
example: "random chars",
|
||||
result: None,
|
||||
},
|
||||
Example {
|
||||
description: "Generate random chars with specified length",
|
||||
example: "random chars -l 20",
|
||||
result: None,
|
||||
},
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn chars(args: CommandArgs) -> Result<OutputStream, ShellError> {
|
||||
let (CharsArgs { length }, _) = args.process().await?;
|
||||
|
||||
let chars_length = length.map_or(DEFAULT_CHARS_LENGTH, |l| l.item);
|
||||
|
||||
let random_string: String = thread_rng()
|
||||
.sample_iter(&Alphanumeric)
|
||||
.take(chars_length as usize)
|
||||
.collect();
|
||||
|
||||
let result = UntaggedValue::string(random_string);
|
||||
Ok(OutputStream::one(ReturnSuccess::value(result)))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::ShellError;
|
||||
use super::SubCommand;
|
||||
|
||||
#[test]
|
||||
fn examples_work_as_expected() -> Result<(), ShellError> {
|
||||
use crate::examples::test as test_examples;
|
||||
|
||||
Ok(test_examples(SubCommand {})?)
|
||||
}
|
||||
}
|
27
crates/nu-command/src/commands/random/command.rs
Normal file
27
crates/nu-command/src/commands/random/command.rs
Normal file
@ -0,0 +1,27 @@
|
||||
use crate::prelude::*;
|
||||
use nu_engine::WholeStreamCommand;
|
||||
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) -> Result<OutputStream, ShellError> {
|
||||
Ok(OutputStream::one(Ok(ReturnSuccess::Value(
|
||||
UntaggedValue::string(get_help(&Command, &args.scope)).into_value(Tag::unknown()),
|
||||
))))
|
||||
}
|
||||
}
|
104
crates/nu-command/src/commands/random/decimal.rs
Normal file
104
crates/nu-command/src/commands/random/decimal.rs
Normal file
@ -0,0 +1,104 @@
|
||||
use crate::prelude::*;
|
||||
use nu_engine::deserializer::NumericRange;
|
||||
use nu_engine::WholeStreamCommand;
|
||||
use nu_errors::ShellError;
|
||||
use nu_protocol::{ReturnSuccess, Signature, SyntaxShape, UntaggedValue};
|
||||
use nu_source::Tagged;
|
||||
use rand::prelude::{thread_rng, Rng};
|
||||
use std::cmp::Ordering;
|
||||
|
||||
pub struct SubCommand;
|
||||
|
||||
#[derive(Deserialize)]
|
||||
pub struct DecimalArgs {
|
||||
range: Option<Tagged<NumericRange>>,
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl WholeStreamCommand for SubCommand {
|
||||
fn name(&self) -> &str {
|
||||
"random decimal"
|
||||
}
|
||||
|
||||
fn signature(&self) -> Signature {
|
||||
Signature::build("random decimal").optional("range", SyntaxShape::Range, "Range of values")
|
||||
}
|
||||
|
||||
fn usage(&self) -> &str {
|
||||
"Generate a random decimal within a range [min..max]"
|
||||
}
|
||||
|
||||
async fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
|
||||
decimal(args).await
|
||||
}
|
||||
|
||||
fn examples(&self) -> Vec<Example> {
|
||||
vec![
|
||||
Example {
|
||||
description: "Generate a default decimal value between 0 and 1",
|
||||
example: "random decimal",
|
||||
result: None,
|
||||
},
|
||||
Example {
|
||||
description: "Generate a random decimal less than or equal to 500",
|
||||
example: "random decimal ..500",
|
||||
result: None,
|
||||
},
|
||||
Example {
|
||||
description: "Generate a random decimal greater than or equal to 100000",
|
||||
example: "random decimal 100000..",
|
||||
result: None,
|
||||
},
|
||||
Example {
|
||||
description: "Generate a random decimal between 1 and 10",
|
||||
example: "random decimal 1..10",
|
||||
result: None,
|
||||
},
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn decimal(args: CommandArgs) -> Result<OutputStream, ShellError> {
|
||||
let (DecimalArgs { range }, _) = args.process().await?;
|
||||
|
||||
let (min, max) = if let Some(range) = &range {
|
||||
(range.item.min() as f64, range.item.max() as f64)
|
||||
} else {
|
||||
(0.0, 1.0)
|
||||
};
|
||||
|
||||
match min.partial_cmp(&max) {
|
||||
Some(Ordering::Greater) => Err(ShellError::labeled_error(
|
||||
format!("Invalid range {}..{}", min, max),
|
||||
"expected a valid range",
|
||||
range
|
||||
.expect("Unexpected ordering error in random decimal")
|
||||
.span(),
|
||||
)),
|
||||
Some(Ordering::Equal) => {
|
||||
let untagged_result = UntaggedValue::decimal_from_float(min, Span::new(64, 64));
|
||||
Ok(OutputStream::one(ReturnSuccess::value(untagged_result)))
|
||||
}
|
||||
_ => {
|
||||
let mut thread_rng = thread_rng();
|
||||
let result: f64 = thread_rng.gen_range(min, max);
|
||||
|
||||
let untagged_result = UntaggedValue::decimal_from_float(result, Span::new(64, 64));
|
||||
|
||||
Ok(OutputStream::one(ReturnSuccess::value(untagged_result)))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::ShellError;
|
||||
use super::SubCommand;
|
||||
|
||||
#[test]
|
||||
fn examples_work_as_expected() -> Result<(), ShellError> {
|
||||
use crate::examples::test as test_examples;
|
||||
|
||||
Ok(test_examples(SubCommand {})?)
|
||||
}
|
||||
}
|
97
crates/nu-command/src/commands/random/dice.rs
Normal file
97
crates/nu-command/src/commands/random/dice.rs
Normal file
@ -0,0 +1,97 @@
|
||||
use crate::prelude::*;
|
||||
use nu_engine::WholeStreamCommand;
|
||||
use nu_errors::ShellError;
|
||||
use nu_protocol::{Signature, SyntaxShape, UntaggedValue};
|
||||
use nu_source::Tagged;
|
||||
use rand::prelude::{thread_rng, Rng};
|
||||
|
||||
pub struct SubCommand;
|
||||
|
||||
#[derive(Deserialize)]
|
||||
pub struct DiceArgs {
|
||||
dice: Option<Tagged<u32>>,
|
||||
sides: Option<Tagged<u32>>,
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl WholeStreamCommand for SubCommand {
|
||||
fn name(&self) -> &str {
|
||||
"random dice"
|
||||
}
|
||||
|
||||
fn signature(&self) -> Signature {
|
||||
Signature::build("random dice")
|
||||
.named(
|
||||
"dice",
|
||||
SyntaxShape::Int,
|
||||
"The amount of dice being rolled",
|
||||
Some('d'),
|
||||
)
|
||||
.named(
|
||||
"sides",
|
||||
SyntaxShape::Int,
|
||||
"The amount of sides a die has",
|
||||
Some('s'),
|
||||
)
|
||||
}
|
||||
|
||||
fn usage(&self) -> &str {
|
||||
"Generate a random dice roll"
|
||||
}
|
||||
|
||||
async fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
|
||||
dice(args).await
|
||||
}
|
||||
|
||||
fn examples(&self) -> Vec<Example> {
|
||||
vec![
|
||||
Example {
|
||||
description: "Roll 1 dice with 6 sides each",
|
||||
example: "random dice",
|
||||
result: None,
|
||||
},
|
||||
Example {
|
||||
description: "Roll 10 dice with 12 sides each",
|
||||
example: "random dice -d 10 -s 12",
|
||||
result: None,
|
||||
},
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn dice(args: CommandArgs) -> Result<OutputStream, ShellError> {
|
||||
let tag = args.call_info.name_tag.clone();
|
||||
let (DiceArgs { dice, sides }, _) = args.process().await?;
|
||||
|
||||
let dice = if let Some(dice_tagged) = dice {
|
||||
*dice_tagged
|
||||
} else {
|
||||
1
|
||||
};
|
||||
|
||||
let sides = if let Some(sides_tagged) = sides {
|
||||
*sides_tagged
|
||||
} else {
|
||||
6
|
||||
};
|
||||
|
||||
let iter = (0..dice).map(move |_| {
|
||||
let mut thread_rng = thread_rng();
|
||||
UntaggedValue::int(thread_rng.gen_range(1, sides + 1)).into_value(tag.clone())
|
||||
});
|
||||
|
||||
Ok(futures::stream::iter(iter).to_output_stream())
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::ShellError;
|
||||
use super::SubCommand;
|
||||
|
||||
#[test]
|
||||
fn examples_work_as_expected() -> Result<(), ShellError> {
|
||||
use crate::examples::test as test_examples;
|
||||
|
||||
Ok(test_examples(SubCommand {})?)
|
||||
}
|
||||
}
|
106
crates/nu-command/src/commands/random/integer.rs
Normal file
106
crates/nu-command/src/commands/random/integer.rs
Normal file
@ -0,0 +1,106 @@
|
||||
use crate::prelude::*;
|
||||
use nu_engine::deserializer::NumericRange;
|
||||
use nu_engine::WholeStreamCommand;
|
||||
use nu_errors::ShellError;
|
||||
use nu_protocol::{ReturnSuccess, Signature, SyntaxShape, UntaggedValue};
|
||||
use nu_source::Tagged;
|
||||
use rand::prelude::{thread_rng, Rng};
|
||||
use std::cmp::Ordering;
|
||||
|
||||
pub struct SubCommand;
|
||||
|
||||
#[derive(Deserialize)]
|
||||
pub struct IntegerArgs {
|
||||
range: Option<Tagged<NumericRange>>,
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl WholeStreamCommand for SubCommand {
|
||||
fn name(&self) -> &str {
|
||||
"random integer"
|
||||
}
|
||||
|
||||
fn signature(&self) -> Signature {
|
||||
Signature::build("random integer").optional("range", SyntaxShape::Range, "Range of values")
|
||||
}
|
||||
|
||||
fn usage(&self) -> &str {
|
||||
"Generate a random integer [min..max]"
|
||||
}
|
||||
|
||||
async fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
|
||||
integer(args).await
|
||||
}
|
||||
|
||||
fn examples(&self) -> Vec<Example> {
|
||||
vec![
|
||||
Example {
|
||||
description: "Generate an unconstrained random integer",
|
||||
example: "random integer",
|
||||
result: None,
|
||||
},
|
||||
Example {
|
||||
description: "Generate a random integer less than or equal to 500",
|
||||
example: "random integer ..500",
|
||||
result: None,
|
||||
},
|
||||
Example {
|
||||
description: "Generate a random integer greater than or equal to 100000",
|
||||
example: "random integer 100000..",
|
||||
result: None,
|
||||
},
|
||||
Example {
|
||||
description: "Generate a random integer between 1 and 10",
|
||||
example: "random integer 1..10",
|
||||
result: None,
|
||||
},
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn integer(args: CommandArgs) -> Result<OutputStream, ShellError> {
|
||||
let (IntegerArgs { range }, _) = args.process().await?;
|
||||
|
||||
let (min, max) = if let Some(range) = &range {
|
||||
(range.item.min(), range.item.max())
|
||||
} else {
|
||||
(0, u64::MAX)
|
||||
};
|
||||
|
||||
match min.cmp(&max) {
|
||||
Ordering::Greater => Err(ShellError::labeled_error(
|
||||
format!("Invalid range {}..{}", min, max),
|
||||
"expected a valid range",
|
||||
range
|
||||
.expect("Unexpected ordering error in random integer")
|
||||
.span(),
|
||||
)),
|
||||
Ordering::Equal => {
|
||||
let untagged_result = UntaggedValue::int(min).into_value(Tag::unknown());
|
||||
Ok(OutputStream::one(ReturnSuccess::value(untagged_result)))
|
||||
}
|
||||
_ => {
|
||||
let mut thread_rng = thread_rng();
|
||||
// add 1 to max, because gen_range is right-exclusive
|
||||
let max = max.saturating_add(1);
|
||||
let result: u64 = thread_rng.gen_range(min, max);
|
||||
|
||||
let untagged_result = UntaggedValue::int(result).into_value(Tag::unknown());
|
||||
|
||||
Ok(OutputStream::one(ReturnSuccess::value(untagged_result)))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::ShellError;
|
||||
use super::SubCommand;
|
||||
|
||||
#[test]
|
||||
fn examples_work_as_expected() -> Result<(), ShellError> {
|
||||
use crate::examples::test as test_examples;
|
||||
|
||||
Ok(test_examples(SubCommand {})?)
|
||||
}
|
||||
}
|
19
crates/nu-command/src/commands/random/mod.rs
Normal file
19
crates/nu-command/src/commands/random/mod.rs
Normal file
@ -0,0 +1,19 @@
|
||||
pub mod command;
|
||||
|
||||
pub mod bool;
|
||||
pub mod chars;
|
||||
pub mod decimal;
|
||||
pub mod dice;
|
||||
pub mod integer;
|
||||
#[cfg(feature = "uuid_crate")]
|
||||
pub mod uuid;
|
||||
|
||||
pub use command::Command as Random;
|
||||
|
||||
pub use self::bool::SubCommand as RandomBool;
|
||||
pub use chars::SubCommand as RandomChars;
|
||||
pub use decimal::SubCommand as RandomDecimal;
|
||||
pub use dice::SubCommand as RandomDice;
|
||||
pub use integer::SubCommand as RandomInteger;
|
||||
#[cfg(feature = "uuid_crate")]
|
||||
pub use uuid::SubCommand as RandomUUID;
|
53
crates/nu-command/src/commands/random/uuid.rs
Normal file
53
crates/nu-command/src/commands/random/uuid.rs
Normal file
@ -0,0 +1,53 @@
|
||||
use crate::prelude::*;
|
||||
use nu_engine::WholeStreamCommand;
|
||||
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) -> Result<OutputStream, ShellError> {
|
||||
uuid(args).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) -> 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::ShellError;
|
||||
use super::SubCommand;
|
||||
|
||||
#[test]
|
||||
fn examples_work_as_expected() -> Result<(), ShellError> {
|
||||
use crate::examples::test as test_examples;
|
||||
|
||||
Ok(test_examples(SubCommand {})?)
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user