From e8bc319f087221a26af3a918a3ab4cfc82830b7d Mon Sep 17 00:00:00 2001 From: Michael Nitschinger Date: Wed, 14 Apr 2021 20:21:50 +0200 Subject: [PATCH] Make sure that scripts can also have custom commands. (#3309) With the current code it is possible to attach custom commands from a custom binary, but only for interactive mode. This change makes it possible to also customize the evaluation context for commands and scripts. --- crates/nu-cli/src/cli.rs | 5 +---- src/main.rs | 6 ++++-- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/crates/nu-cli/src/cli.rs b/crates/nu-cli/src/cli.rs index 6d24c49ff..09a4f65f9 100644 --- a/crates/nu-cli/src/cli.rs +++ b/crates/nu-cli/src/cli.rs @@ -1,6 +1,5 @@ use crate::line_editor::configure_ctrl_c; use nu_ansi_term::Color; -use nu_command::commands::default_context::create_default_context; use nu_engine::{maybe_print_errors, run_block, script::run_script_standalone, EvaluationContext}; #[allow(unused_imports)] @@ -124,9 +123,7 @@ pub fn search_paths() -> Vec { search_paths } -pub fn run_script_file(options: Options) -> Result<(), Box> { - let context = create_default_context(false)?; - +pub fn run_script_file(context: EvaluationContext, options: Options) -> Result<(), Box> { if let Some(cfg) = options.config { load_cfg_as_global_cfg(&context, PathBuf::from(cfg)); } else { diff --git a/src/main.rs b/src/main.rs index 737a04acd..5f98ea6b4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -150,7 +150,8 @@ fn main() -> Result<(), Box> { Some(values) => { options.scripts = vec![NuScript::code(values)?]; - nu_cli::run_script_file(options)?; + let context = create_default_context(false)?; + nu_cli::run_script_file(context, options)?; return Ok(()); } } @@ -161,7 +162,8 @@ fn main() -> Result<(), Box> { options.scripts = vec![NuScript::source_file(filepath.as_os_str())?]; - nu_cli::run_script_file(options)?; + let context = create_default_context(false)?; + nu_cli::run_script_file(context, options)?; return Ok(()); }