Interpret lists as series of args for externals (#550)

* Interpret lists as series of args for externals

* Fix clippy warnings
This commit is contained in:
Jakub Žádník 2021-12-22 10:13:05 +02:00 committed by GitHub
parent ea6912c3f7
commit 8ba3e3570c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 31 additions and 6 deletions

View File

@ -50,7 +50,7 @@ impl Command for External {
input: PipelineData, input: PipelineData,
) -> Result<PipelineData, ShellError> { ) -> Result<PipelineData, ShellError> {
let mut name: Spanned<String> = call.req(engine_state, stack, 0)?; let mut name: Spanned<String> = call.req(engine_state, stack, 0)?;
let args: Vec<String> = call.rest(engine_state, stack, 1)?; let args: Vec<Value> = call.rest(engine_state, stack, 1)?;
let last_expression = call.has_flag("last_expression"); let last_expression = call.has_flag("last_expression");
// Translate environment variables from Values to Strings // Translate environment variables from Values to Strings
@ -85,9 +85,34 @@ impl Command for External {
return Ok(PipelineData::new(call.head)); return Ok(PipelineData::new(call.head));
} }
let mut args_strs = vec![];
for arg in args {
if let Ok(s) = arg.as_string() {
args_strs.push(s);
} else if let Value::List { vals, .. } = arg {
// Interpret a list as a series of arguments
for val in vals {
if let Ok(s) = val.as_string() {
args_strs.push(s);
} else {
return Err(ShellError::ExternalCommand(
"Cannot convert argument to a string".into(),
val.span()?,
));
}
}
} else {
return Err(ShellError::ExternalCommand(
"Cannot convert argument to a string".into(),
arg.span()?,
));
}
}
let command = ExternalCommand { let command = ExternalCommand {
name, name,
args, args: args_strs,
last_expression, last_expression,
env_vars: env_vars_str, env_vars: env_vars_str,
call, call,

View File

@ -470,12 +470,12 @@ pub fn eval_variable(
let mut output_vals = vec![]; let mut output_vals = vec![];
let env_vars = stack.get_env_vars(); let env_vars = stack.get_env_vars();
let env_columns: Vec<String> = env_vars.keys().map(|x| x.to_string()).collect(); let env_columns = env_vars.keys();
let env_values: Vec<Value> = env_vars.values().cloned().collect(); let env_values = env_vars.values();
let mut pairs = env_columns let mut pairs = env_columns
.into_iter() .map(|x| x.to_string())
.zip(env_values.into_iter()) .zip(env_values.cloned())
.collect::<Vec<(String, Value)>>(); .collect::<Vec<(String, Value)>>();
pairs.sort_by(|a, b| a.0.cmp(&b.0)); pairs.sort_by(|a, b| a.0.cmp(&b.0));