Artemiy 1867bb1a88
Fix incorrect handling of boolean flags for builtin commands (#11492)
# 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:

![image](https://github.com/nushell/nushell/assets/17511668/f9fbabb2-3cfd-43f9-ba9e-ece76d80043c)
After:

![image](https://github.com/nushell/nushell/assets/17511668/21867596-2075-437f-9c85-45563ac70083)

Another example:
Before:

![image](https://github.com/nushell/nushell/assets/17511668/efdbc5ca-5227-45a4-ac5b-532cdc2bbf5f)
After:

![image](https://github.com/nushell/nushell/assets/17511668/2907d5c5-aa93-404d-af1c-21cdc3d44646)


# Tests + Formatting
Added test reproducing some variants of original issue.
2024-01-11 17:19:48 +02:00

288 lines
11 KiB
Rust

use nu_engine::{current_dir, eval_block, CallExt};
use nu_path::expand_to_real_path;
use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::util::BufferedReader;
use nu_protocol::{
Category, DataSource, Example, IntoInterruptiblePipelineData, PipelineData, PipelineMetadata,
RawStream, ShellError, Signature, Spanned, SyntaxShape, Type, Value,
};
use std::io::BufReader;
#[cfg(feature = "sqlite")]
use crate::database::SQLiteDatabase;
#[cfg(feature = "sqlite")]
use nu_protocol::IntoPipelineData;
#[cfg(unix)]
use std::os::unix::fs::PermissionsExt;
use std::path::Path;
#[derive(Clone)]
pub struct Open;
impl Command for Open {
fn name(&self) -> &str {
"open"
}
fn usage(&self) -> &str {
"Load a file into a cell, converting to table if possible (avoid by appending '--raw')."
}
fn extra_usage(&self) -> &str {
"Support to automatically parse files with an extension `.xyz` can be provided by a `from xyz` command in scope."
}
fn search_terms(&self) -> Vec<&str> {
vec!["load", "read", "load_file", "read_file"]
}
fn signature(&self) -> nu_protocol::Signature {
Signature::build("open")
.input_output_types(vec![(Type::Nothing, Type::Any), (Type::String, Type::Any)])
.optional("filename", SyntaxShape::Filepath, "The filename to use.")
.rest(
"filenames",
SyntaxShape::Filepath,
"Optional additional files to open.",
)
.switch("raw", "open file as raw binary", Some('r'))
.category(Category::FileSystem)
}
fn run(
&self,
engine_state: &EngineState,
stack: &mut Stack,
call: &Call,
input: PipelineData,
) -> Result<PipelineData, ShellError> {
let raw = call.has_flag(engine_state, stack, "raw")?;
let call_span = call.head;
let ctrlc = engine_state.ctrlc.clone();
let cwd = current_dir(engine_state, stack)?;
let req_path = call.opt::<Spanned<String>>(engine_state, stack, 0)?;
let mut path_params = call.rest::<Spanned<String>>(engine_state, stack, 1)?;
// FIXME: JT: what is this doing here?
if let Some(filename) = req_path {
path_params.insert(0, filename);
} else {
let filename = match input {
PipelineData::Value(Value::Nothing { .. }, ..) => {
return Err(ShellError::MissingParameter {
param_name: "needs filename".to_string(),
span: call.head,
})
}
PipelineData::Value(val, ..) => val.as_spanned_string()?,
_ => {
return Err(ShellError::MissingParameter {
param_name: "needs filename".to_string(),
span: call.head,
});
}
};
path_params.insert(0, filename);
}
let mut output = vec![];
for path in path_params.into_iter() {
//FIXME: `open` should not have to do this
let path = {
Spanned {
item: nu_utils::strip_ansi_string_unlikely(path.item),
span: path.span,
}
};
let arg_span = path.span;
// let path_no_whitespace = &path.item.trim_end_matches(|x| matches!(x, '\x09'..='\x0d'));
for path in nu_engine::glob_from(&path, &cwd, call_span, None)
.map_err(|err| match err {
ShellError::DirectoryNotFound { span, .. } => ShellError::FileNotFound { span },
_ => err,
})?
.1
{
let path = path?;
let path = Path::new(&path);
if permission_denied(path) {
#[cfg(unix)]
let error_msg = match path.metadata() {
Ok(md) => format!(
"The permissions of {:o} does not allow access for this user",
md.permissions().mode() & 0o0777
),
Err(e) => e.to_string(),
};
#[cfg(not(unix))]
let error_msg = String::from("Permission denied");
return Err(ShellError::GenericError {
error: "Permission denied".into(),
msg: error_msg,
span: Some(arg_span),
help: None,
inner: vec![],
});
} else {
#[cfg(feature = "sqlite")]
if !raw {
let res = SQLiteDatabase::try_from_path(path, arg_span, ctrlc.clone())
.map(|db| db.into_value(call.head).into_pipeline_data());
if res.is_ok() {
return res;
}
}
let file = match std::fs::File::open(path) {
Ok(file) => file,
Err(err) => {
return Err(ShellError::GenericError {
error: "Permission denied".into(),
msg: err.to_string(),
span: Some(arg_span),
help: None,
inner: vec![],
});
}
};
let buf_reader = BufReader::new(file);
let real_path = expand_to_real_path(path);
let file_contents = PipelineData::ExternalStream {
stdout: Some(RawStream::new(
Box::new(BufferedReader { input: buf_reader }),
ctrlc.clone(),
call_span,
None,
)),
stderr: None,
exit_code: None,
span: call_span,
metadata: Some(PipelineMetadata {
data_source: DataSource::FilePath(real_path),
}),
trim_end_newline: false,
};
let exts_opt: Option<Vec<String>> = if raw {
None
} else {
let path_str = path
.file_name()
.unwrap_or(std::ffi::OsStr::new(path))
.to_string_lossy()
.to_lowercase();
Some(extract_extensions(path_str.as_str()))
};
let converter = exts_opt.and_then(|exts| {
exts.iter().find_map(|ext| {
engine_state
.find_decl(format!("from {}", ext).as_bytes(), &[])
.map(|id| (id, ext.to_string()))
})
});
match converter {
Some((converter_id, ext)) => {
let decl = engine_state.get_decl(converter_id);
let command_output = if let Some(block_id) = decl.get_block_id() {
let block = engine_state.get_block(block_id);
eval_block(engine_state, stack, block, file_contents, false, false)
} else {
decl.run(engine_state, stack, &Call::new(call_span), file_contents)
};
output.push(command_output.map_err(|inner| {
ShellError::GenericError{
error: format!("Error while parsing as {ext}"),
msg: format!("Could not parse '{}' with `from {}`", path.display(), ext),
span: Some(arg_span),
help: Some(format!("Check out `help from {}` or `help from` for more options or open raw data with `open --raw '{}'`", ext, path.display())),
inner: vec![inner],
}
})?);
}
None => output.push(file_contents),
}
}
}
}
if output.is_empty() {
Ok(PipelineData::Empty)
} else if output.len() == 1 {
Ok(output.remove(0))
} else {
Ok(output.into_iter().flatten().into_pipeline_data(ctrlc))
}
}
fn examples(&self) -> Vec<nu_protocol::Example> {
vec![
Example {
description: "Open a file, with structure (based on file extension or SQLite database header)",
example: "open myfile.json",
result: None,
},
Example {
description: "Open a file, as raw bytes",
example: "open myfile.json --raw",
result: None,
},
Example {
description: "Open a file, using the input to get filename",
example: "'myfile.txt' | open",
result: None,
},
Example {
description: "Open a file, and decode it by the specified encoding",
example: "open myfile.txt --raw | decode utf-8",
result: None,
},
Example {
description: "Create a custom `from` parser to open newline-delimited JSON files with `open`",
example: r#"def "from ndjson" [] { from json -o }; open myfile.ndjson"#,
result: None,
},
]
}
}
fn permission_denied(dir: impl AsRef<Path>) -> bool {
match dir.as_ref().read_dir() {
Err(e) => matches!(e.kind(), std::io::ErrorKind::PermissionDenied),
Ok(_) => false,
}
}
fn extract_extensions(filename: &str) -> Vec<String> {
let parts: Vec<&str> = filename.split('.').collect();
let mut extensions: Vec<String> = Vec::new();
let mut current_extension = String::new();
for part in parts.iter().rev() {
if current_extension.is_empty() {
current_extension.push_str(part);
} else {
current_extension = format!("{}.{}", part, current_extension);
}
extensions.push(current_extension.clone());
}
extensions.pop();
extensions.reverse();
extensions
}