mirror of
https://github.com/nushell/nushell.git
synced 2025-06-30 22:50:14 +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:
@ -115,7 +115,7 @@ pub fn value_to_json_value(v: &Value) -> Result<nu_json::Value, ShellError> {
|
||||
Value::Int { val, .. } => nu_json::Value::I64(*val),
|
||||
Value::Nothing { .. } => nu_json::Value::Null,
|
||||
Value::String { val, .. } => nu_json::Value::String(val.to_string()),
|
||||
Value::QuotedString { val, .. } => nu_json::Value::String(val.to_string()),
|
||||
Value::Glob { val, .. } => nu_json::Value::String(val.to_string()),
|
||||
Value::CellPath { val, .. } => nu_json::Value::Array(
|
||||
val.members
|
||||
.iter()
|
||||
|
@ -279,7 +279,7 @@ pub fn value_to_string(
|
||||
// All strings outside data structures are quoted because they are in 'command position'
|
||||
// (could be mistaken for commands by the Nu parser)
|
||||
Value::String { val, .. } => Ok(escape_quote_string(val)),
|
||||
Value::QuotedString { val, .. } => Ok(escape_quote_string(val)),
|
||||
Value::Glob { val, .. } => Ok(escape_quote_string(val)),
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -127,7 +127,7 @@ fn local_into_string(value: Value, separator: &str, config: &Config) -> String {
|
||||
)
|
||||
}
|
||||
Value::String { val, .. } => val,
|
||||
Value::QuotedString { val, .. } => val,
|
||||
Value::Glob { val, .. } => val,
|
||||
Value::List { vals: val, .. } => val
|
||||
.into_iter()
|
||||
.map(|x| local_into_string(x, ", ", config))
|
||||
|
@ -57,9 +57,7 @@ fn helper(engine_state: &EngineState, v: &Value) -> Result<toml::Value, ShellErr
|
||||
}
|
||||
Value::Range { .. } => toml::Value::String("<Range>".to_string()),
|
||||
Value::Float { val, .. } => toml::Value::Float(*val),
|
||||
Value::String { val, .. } | Value::QuotedString { val, .. } => {
|
||||
toml::Value::String(val.clone())
|
||||
}
|
||||
Value::String { val, .. } | Value::Glob { val, .. } => toml::Value::String(val.clone()),
|
||||
Value::Record { val, .. } => {
|
||||
let mut m = toml::map::Map::new();
|
||||
for (k, v) in val {
|
||||
|
@ -52,7 +52,7 @@ pub fn value_to_yaml_value(v: &Value) -> Result<serde_yaml::Value, ShellError> {
|
||||
Value::Date { val, .. } => serde_yaml::Value::String(val.to_string()),
|
||||
Value::Range { .. } => serde_yaml::Value::Null,
|
||||
Value::Float { val, .. } => serde_yaml::Value::Number(serde_yaml::Number::from(*val)),
|
||||
Value::String { val, .. } | Value::QuotedString { val, .. } => {
|
||||
Value::String { val, .. } | Value::Glob { val, .. } => {
|
||||
serde_yaml::Value::String(val.clone())
|
||||
}
|
||||
Value::Record { val, .. } => {
|
||||
|
Reference in New Issue
Block a user