From 8ba3e3570c2dd4219f523471477d83b0d27a6aa7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20=C5=BD=C3=A1dn=C3=ADk?= Date: Wed, 22 Dec 2021 10:13:05 +0200 Subject: [PATCH] Interpret lists as series of args for externals (#550) * Interpret lists as series of args for externals * Fix clippy warnings --- crates/nu-command/src/system/run_external.rs | 29 ++++++++++++++++++-- crates/nu-engine/src/eval.rs | 8 +++--- 2 files changed, 31 insertions(+), 6 deletions(-) diff --git a/crates/nu-command/src/system/run_external.rs b/crates/nu-command/src/system/run_external.rs index c8fc256bd1..66efc61e52 100644 --- a/crates/nu-command/src/system/run_external.rs +++ b/crates/nu-command/src/system/run_external.rs @@ -50,7 +50,7 @@ impl Command for External { input: PipelineData, ) -> Result { let mut name: Spanned = call.req(engine_state, stack, 0)?; - let args: Vec = call.rest(engine_state, stack, 1)?; + let args: Vec = call.rest(engine_state, stack, 1)?; let last_expression = call.has_flag("last_expression"); // Translate environment variables from Values to Strings @@ -85,9 +85,34 @@ impl Command for External { 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 { name, - args, + args: args_strs, last_expression, env_vars: env_vars_str, call, diff --git a/crates/nu-engine/src/eval.rs b/crates/nu-engine/src/eval.rs index b8bd124427..e85e921c7a 100644 --- a/crates/nu-engine/src/eval.rs +++ b/crates/nu-engine/src/eval.rs @@ -470,12 +470,12 @@ pub fn eval_variable( let mut output_vals = vec![]; let env_vars = stack.get_env_vars(); - let env_columns: Vec = env_vars.keys().map(|x| x.to_string()).collect(); - let env_values: Vec = env_vars.values().cloned().collect(); + let env_columns = env_vars.keys(); + let env_values = env_vars.values(); let mut pairs = env_columns - .into_iter() - .zip(env_values.into_iter()) + .map(|x| x.to_string()) + .zip(env_values.cloned()) .collect::>(); pairs.sort_by(|a, b| a.0.cmp(&b.0));