allow glob to take a glob or a string as the input (#13612)

# Description

This PR changes glob to take either a string or a glob as a parameter.

Closes #13611

# User-Facing Changes
<!-- List of all changes that impact the user experience here. This
helps us keep track of breaking 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:

- `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 toolkit.nu; toolkit test stdlib"` 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.
-->
This commit is contained in:
Darren Schroeder 2024-08-13 14:27:42 -05:00 committed by GitHub
parent 5f45f6c223
commit 4732507f46
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -13,7 +13,7 @@ impl Command for Glob {
fn signature(&self) -> Signature {
Signature::build("glob")
.input_output_types(vec![(Type::Nothing, Type::List(Box::new(Type::String)))])
.required("glob", SyntaxShape::String, "The glob expression.")
.required("glob", SyntaxShape::OneOf(vec![SyntaxShape::String, SyntaxShape::GlobPattern]), "The glob expression.")
.named(
"depth",
SyntaxShape::Int,
@ -126,12 +126,12 @@ impl Command for Glob {
_input: PipelineData,
) -> Result<PipelineData, ShellError> {
let span = call.head;
let glob_pattern: Spanned<String> = call.req(engine_state, stack, 0)?;
let glob_pattern_input: Value = call.req(engine_state, stack, 0)?;
let glob_span = glob_pattern_input.span();
let depth = call.get_flag(engine_state, stack, "depth")?;
let no_dirs = call.has_flag(engine_state, stack, "no-dir")?;
let no_files = call.has_flag(engine_state, stack, "no-file")?;
let no_symlinks = call.has_flag(engine_state, stack, "no-symlink")?;
let paths_to_exclude: Option<Value> = call.get_flag(engine_state, stack, "exclude")?;
let (not_patterns, not_pattern_span): (Vec<String>, Span) = match paths_to_exclude {
@ -148,15 +148,40 @@ impl Command for Glob {
}
};
if glob_pattern.item.is_empty() {
return Err(ShellError::GenericError {
error: "glob pattern must not be empty".into(),
msg: "glob pattern is empty".into(),
span: Some(glob_pattern.span),
help: Some("add characters to the glob pattern".into()),
inner: vec![],
});
}
let glob_pattern = match glob_pattern_input {
Value::String { val, .. } => {
if val.is_empty() {
return Err(ShellError::GenericError {
error: "glob pattern must not be empty".into(),
msg: "glob pattern is empty".into(),
span: Some(glob_span),
help: Some("add characters to the glob pattern".into()),
inner: vec![],
});
} else {
val
}
}
Value::Glob { val, .. } => {
if val.is_empty() {
return Err(ShellError::GenericError {
error: "glob pattern must not be empty".into(),
msg: "glob pattern is empty".into(),
span: Some(glob_span),
help: Some("add characters to the glob pattern".into()),
inner: vec![],
});
} else {
val
}
}
_ => {
return Err(ShellError::IncompatibleParametersSingle {
msg: "Incorrect parameter type".to_string(),
span: glob_span,
})
}
};
let folder_depth = if let Some(depth) = depth {
depth
@ -164,13 +189,13 @@ impl Command for Glob {
usize::MAX
};
let (prefix, glob) = match WaxGlob::new(&glob_pattern.item) {
let (prefix, glob) = match WaxGlob::new(&glob_pattern) {
Ok(p) => p.partition(),
Err(e) => {
return Err(ShellError::GenericError {
error: "error with glob pattern".into(),
msg: format!("{e}"),
span: Some(glob_pattern.span),
span: Some(glob_span),
help: None,
inner: vec![],
})
@ -189,7 +214,7 @@ impl Command for Glob {
return Err(ShellError::GenericError {
error: "error in canonicalize".into(),
msg: format!("{e}"),
span: Some(glob_pattern.span),
span: Some(glob_span),
help: None,
inner: vec![],
})