# Description
Possible fix of #11456
This PR fixes a bug where builtin commands did not respect the logic of
dynamically passed boolean flags. The reason is
[has_flag](6f59abaf43/crates/nu-protocol/src/ast/call.rs (L204C5-L212C6)
)
method did not evaluate and take into consideration expression used with
flag.
To address this issue a solution is proposed:
1. `has_flag` method is moved to `CallExt` and new logic to evaluate
expression and check if it is a boolean value is added
2. `has_flag_const` method is added to `CallExt` which is a constant
version of `has_flag`
3. `has_named` method is added to `Call` which is basically the old
logic of `has_flag`
4. All usages of `has_flag` in code are updated, mostly to pass
`engine_state` and `stack` to new `has_flag`. In `run_const` commands it
is replaced with `has_flag_const`. And in a few select places: parser,
`to nuon` and `into string` old logic via `has_named` is used.
# User-Facing Changes
Explicit values of boolean flags are now respected in builtin commands.
Before:

After:

Another example:
Before:

After:

# Tests + Formatting
Added test reproducing some variants of original issue.
134 lines
4.7 KiB
Rust
134 lines
4.7 KiB
Rust
use nu_engine::env::current_dir;
|
|
use nu_engine::CallExt;
|
|
use nu_protocol::ast::Call;
|
|
use nu_protocol::engine::{Command, EngineState, Stack};
|
|
use nu_protocol::{
|
|
Category, Example, PipelineData, ShellError, Signature, Spanned, SyntaxShape, Type, Value,
|
|
};
|
|
use std::path::PathBuf;
|
|
|
|
#[derive(Clone)]
|
|
pub struct Mktemp;
|
|
|
|
impl Command for Mktemp {
|
|
fn name(&self) -> &str {
|
|
"mktemp"
|
|
}
|
|
|
|
fn usage(&self) -> &str {
|
|
"Create temporary files or directories using uutils/coreutils mktemp."
|
|
}
|
|
|
|
fn search_terms(&self) -> Vec<&str> {
|
|
vec![
|
|
"coreutils",
|
|
"create",
|
|
"directory",
|
|
"file",
|
|
"folder",
|
|
"temporary",
|
|
]
|
|
}
|
|
|
|
fn signature(&self) -> Signature {
|
|
Signature::build("mktemp")
|
|
.input_output_types(vec![(Type::Nothing, Type::String)])
|
|
.optional(
|
|
"template",
|
|
SyntaxShape::String,
|
|
"Optional pattern from which the name of the file or directory is derived. Must contain at least three 'X's in last component.",
|
|
)
|
|
.named("suffix", SyntaxShape::String, "Append suffix to template; must not contain a slash.", None)
|
|
.named("tmpdir-path", SyntaxShape::Filepath, "Interpret TEMPLATE relative to tmpdir-path. If tmpdir-path is not set use $TMPDIR", Some('p'))
|
|
.switch("tmpdir", "Interpret TEMPLATE relative to the system temporary directory.", Some('t'))
|
|
.switch("directory", "Create a directory instead of a file.", Some('d'))
|
|
.category(Category::FileSystem)
|
|
}
|
|
|
|
fn examples(&self) -> Vec<Example> {
|
|
vec![
|
|
Example {
|
|
description: "Make a temporary file with the given suffix in the current working directory.",
|
|
example: "mktemp --suffix .txt",
|
|
result: Some(Value::test_string("<WORKING_DIR>/tmp.lekjbhelyx.txt")),
|
|
},
|
|
Example {
|
|
description: "Make a temporary file named testfile.XXX with the 'X's as random characters in the current working directory.",
|
|
example: "mktemp testfile.XXX",
|
|
result: Some(Value::test_string("<WORKING_DIR>/testfile.4kh")),
|
|
},
|
|
Example {
|
|
description: "Make a temporary file with a template in the system temp directory.",
|
|
example: "mktemp -t testfile.XXX",
|
|
result: Some(Value::test_string("/tmp/testfile.4kh")),
|
|
},
|
|
Example {
|
|
description: "Make a temporary directory with randomly generated name in the temporary directory.",
|
|
example: "mktemp -d",
|
|
result: Some(Value::test_string("/tmp/tmp.NMw9fJr8K0")),
|
|
},
|
|
]
|
|
}
|
|
|
|
fn run(
|
|
&self,
|
|
engine_state: &EngineState,
|
|
stack: &mut Stack,
|
|
call: &Call,
|
|
_input: PipelineData,
|
|
) -> Result<PipelineData, ShellError> {
|
|
let span = call.head;
|
|
let template = call
|
|
.rest(engine_state, stack, 0)?
|
|
.first()
|
|
.cloned()
|
|
.map(|i: Spanned<String>| i.item)
|
|
.unwrap_or("tmp.XXXXXXXXXX".to_string()); // same as default in coreutils
|
|
let directory = call.has_flag(engine_state, stack, "directory")?;
|
|
let suffix = call.get_flag(engine_state, stack, "suffix")?;
|
|
let tmpdir = call.has_flag(engine_state, stack, "tmpdir")?;
|
|
let tmpdir_path = call
|
|
.get_flag(engine_state, stack, "tmpdir-path")?
|
|
.map(|i: Spanned<PathBuf>| i.item);
|
|
|
|
let tmpdir = if tmpdir_path.is_some() {
|
|
tmpdir_path
|
|
} else if directory || tmpdir {
|
|
Some(std::env::temp_dir())
|
|
} else {
|
|
Some(current_dir(engine_state, stack)?)
|
|
};
|
|
|
|
let options = uu_mktemp::Options {
|
|
directory,
|
|
dry_run: false,
|
|
quiet: false,
|
|
suffix,
|
|
template,
|
|
tmpdir,
|
|
treat_as_template: true,
|
|
};
|
|
|
|
let res = match uu_mktemp::mktemp(&options) {
|
|
Ok(res) => {
|
|
res.into_os_string()
|
|
.into_string()
|
|
.map_err(|e| ShellError::IOErrorSpanned {
|
|
msg: e.to_string_lossy().to_string(),
|
|
span,
|
|
})?
|
|
}
|
|
Err(e) => {
|
|
return Err(ShellError::GenericError {
|
|
error: format!("{}", e),
|
|
msg: format!("{}", e),
|
|
span: None,
|
|
help: None,
|
|
inner: vec![],
|
|
});
|
|
}
|
|
};
|
|
Ok(PipelineData::Value(Value::string(res, span), None))
|
|
}
|
|
}
|