mirror of
https://github.com/nushell/nushell.git
synced 2025-07-01 07:00:37 +02:00
open
, rm
, umv
, cp
, rm
and du
: Don't globs if inputs are variables or string interpolation (#11886)
# Description
This is a follow up to
https://github.com/nushell/nushell/pull/11621#issuecomment-1937484322
Also Fixes: #11838
## About the code change
It applys the same logic when we pass variables to external commands:
0487e9ffcb/crates/nu-command/src/system/run_external.rs (L162-L170)
That is: if user input dynamic things(like variables, sub-expression, or
string interpolation), it returns a quoted `NuPath`, then user input
won't be globbed
# User-Facing Changes
Given two input files: `a*c.txt`, `abc.txt`
* `let f = "a*c.txt"; rm $f` will remove one file: `a*c.txt`.
~* `let f = "a*c.txt"; rm --glob $f` will remove `a*c.txt` and
`abc.txt`~
* `let f: glob = "a*c.txt"; rm $f` will remove `a*c.txt` and `abc.txt`
## Rules about globbing with *variable*
Given two files: `a*c.txt`, `abc.txt`
| Cmd Type | example | Result |
| ----- | ------------------ | ------ |
| builtin | let f = "a*c.txt"; rm $f | remove `a*c.txt` |
| builtin | let f: glob = "a*c.txt"; rm $f | remove `a*c.txt` and
`abc.txt`
| builtin | let f = "a*c.txt"; rm ($f \| into glob) | remove `a*c.txt`
and `abc.txt`
| custom | def crm [f: glob] { rm $f }; let f = "a*c.txt"; crm $f |
remove `a*c.txt` and `abc.txt`
| custom | def crm [f: glob] { rm ($f \| into string) }; let f =
"a*c.txt"; crm $f | remove `a*c.txt`
| custom | def crm [f: string] { rm $f }; let f = "a*c.txt"; crm $f |
remove `a*c.txt`
| custom | def crm [f: string] { rm $f }; let f = "a*c.txt"; crm ($f \|
into glob) | remove `a*c.txt` and `abc.txt`
In general, if a variable is annotated with `glob` type, nushell will
expand glob pattern. Or else, we need to use `into | glob` to expand
glob pattern
# Tests + Formatting
Done
# After Submitting
I think `str glob-escape` command will be no-longer required. We can
remove it.
This commit is contained in:
@ -305,7 +305,7 @@ fn describe_value(
|
||||
| Value::Date { .. }
|
||||
| Value::Range { .. }
|
||||
| Value::String { .. }
|
||||
| Value::QuotedString { .. }
|
||||
| Value::Glob { .. }
|
||||
| Value::Nothing { .. } => Value::record(
|
||||
record!(
|
||||
"type" => Value::string(value.get_type().to_string(), head),
|
||||
|
@ -1,7 +1,9 @@
|
||||
use nu_engine::eval_block;
|
||||
use nu_protocol::ast::Call;
|
||||
use nu_protocol::engine::{Command, EngineState, Stack};
|
||||
use nu_protocol::{Category, Example, PipelineData, ShellError, Signature, SyntaxShape, Type};
|
||||
use nu_protocol::{
|
||||
Category, Example, PipelineData, ShellError, Signature, SyntaxShape, Type, Value,
|
||||
};
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct Let;
|
||||
@ -61,8 +63,24 @@ impl Command for Let {
|
||||
.expect("internal error: missing right hand side");
|
||||
|
||||
let block = engine_state.get_block(block_id);
|
||||
|
||||
let pipeline_data = eval_block(engine_state, stack, block, input, true, false)?;
|
||||
stack.add_var(var_id, pipeline_data.into_value(call.head));
|
||||
let mut value = pipeline_data.into_value(call.head);
|
||||
|
||||
// if given variable type is Glob, and our result is string
|
||||
// then nushell need to convert from Value::String to Value::Glob
|
||||
// it's assigned by demand, then it's not quoted, and it's required to expand
|
||||
// if we pass it to other commands.
|
||||
let var_type = &engine_state.get_var(var_id).ty;
|
||||
let val_span = value.span();
|
||||
match value {
|
||||
Value::String { val, .. } if var_type == &Type::Glob => {
|
||||
value = Value::glob(val, false, val_span);
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
|
||||
stack.add_var(var_id, value);
|
||||
Ok(PipelineData::empty())
|
||||
}
|
||||
|
||||
|
@ -228,7 +228,7 @@ impl<'a> std::fmt::Debug for DebuggableValue<'a> {
|
||||
val.from, val.to, val.incr
|
||||
),
|
||||
},
|
||||
Value::String { val, .. } | Value::QuotedString { val, .. } => {
|
||||
Value::String { val, .. } | Value::Glob { val, .. } => {
|
||||
write!(f, "{:?}", val)
|
||||
}
|
||||
Value::Record { val, .. } => {
|
||||
|
Reference in New Issue
Block a user