mirror of
https://github.com/nushell/nushell.git
synced 2025-06-30 22:50:14 +02:00
Impl one configurable function to run scripts (#3242)
* Impl one func to run scripts * Add exit_on_err * Remove run_standalone * Make the compiler happy :)
This commit is contained in:
@ -505,6 +505,8 @@ mod tests {
|
||||
};
|
||||
#[cfg(feature = "which")]
|
||||
use super::{run_external_command, InputStream};
|
||||
#[cfg(feature = "which")]
|
||||
use nu_engine::filesystem::filesystem_shell::FilesystemShellMode;
|
||||
|
||||
#[cfg(feature = "which")]
|
||||
use futures::executor::block_on;
|
||||
@ -533,8 +535,8 @@ mod tests {
|
||||
let cmd = ExternalBuilder::for_name("i_dont_exist.exe").build();
|
||||
|
||||
let input = InputStream::empty();
|
||||
let mut ctx =
|
||||
basic_evaluation_context().expect("There was a problem creating a basic context.");
|
||||
let mut ctx = basic_evaluation_context(FilesystemShellMode::Cli)
|
||||
.expect("There was a problem creating a basic context.");
|
||||
|
||||
assert!(
|
||||
run_external_command(cmd, &mut ctx, input, ExternalRedirection::Stdout)
|
||||
|
@ -1,10 +1,13 @@
|
||||
use crate::prelude::*;
|
||||
use nu_engine::basic_evaluation_context;
|
||||
use nu_engine::whole_stream_command;
|
||||
use nu_engine::{basic_evaluation_context, filesystem::filesystem_shell::FilesystemShellMode};
|
||||
use std::error::Error;
|
||||
|
||||
pub fn create_default_context(interactive: bool) -> Result<EvaluationContext, Box<dyn Error>> {
|
||||
let context = basic_evaluation_context()?;
|
||||
pub fn create_default_context(
|
||||
mode: FilesystemShellMode,
|
||||
interactive: bool,
|
||||
) -> Result<EvaluationContext, Box<dyn Error>> {
|
||||
let context = basic_evaluation_context(mode)?;
|
||||
|
||||
{
|
||||
use crate::commands::*;
|
||||
|
@ -1,9 +1,11 @@
|
||||
use std::path::PathBuf;
|
||||
|
||||
use crate::prelude::*;
|
||||
use nu_engine::{script, WholeStreamCommand};
|
||||
|
||||
use nu_errors::ShellError;
|
||||
use nu_parser::expand_path;
|
||||
use nu_protocol::{Signature, SyntaxShape};
|
||||
use nu_protocol::{NuScript, RunScriptOptions, Signature, SyntaxShape};
|
||||
use nu_source::Tagged;
|
||||
|
||||
pub struct Source;
|
||||
@ -44,27 +46,12 @@ pub async fn source(args: CommandArgs) -> Result<OutputStream, ShellError> {
|
||||
let ctx = EvaluationContext::from_args(&args);
|
||||
let (SourceArgs { filename }, _) = args.process().await?;
|
||||
|
||||
// Note: this is a special case for setting the context from a command
|
||||
// In this case, if we don't set it now, we'll lose the scope that this
|
||||
// variable should be set into.
|
||||
let contents = std::fs::read_to_string(expand_path(&filename.item).into_owned());
|
||||
match contents {
|
||||
Ok(contents) => {
|
||||
let result = script::run_script_standalone(contents, true, &ctx, false).await;
|
||||
let script = NuScript::File(PathBuf::from(expand_path(&filename.item).to_string()));
|
||||
let options = RunScriptOptions::default()
|
||||
.use_existing_scope(true)
|
||||
.redirect_stdin(true)
|
||||
.exit_on_error(false);
|
||||
script::run_script(script, &options, &ctx).await;
|
||||
|
||||
if let Err(err) = result {
|
||||
ctx.error(err.into());
|
||||
}
|
||||
Ok(OutputStream::empty())
|
||||
}
|
||||
Err(_) => {
|
||||
ctx.error(ShellError::labeled_error(
|
||||
"Can't load file to source",
|
||||
"can't load file",
|
||||
filename.span(),
|
||||
));
|
||||
|
||||
Ok(OutputStream::empty())
|
||||
}
|
||||
}
|
||||
Ok(OutputStream::empty())
|
||||
}
|
||||
|
@ -6,6 +6,7 @@ mod stub_generate;
|
||||
|
||||
use double_echo::Command as DoubleEcho;
|
||||
use double_ls::Command as DoubleLs;
|
||||
use nu_engine::filesystem::filesystem_shell::FilesystemShellMode;
|
||||
use stub_generate::{mock_path, Command as StubOpen};
|
||||
|
||||
use nu_engine::basic_evaluation_context;
|
||||
@ -26,7 +27,7 @@ use futures::executor::block_on;
|
||||
pub fn test_examples(cmd: Command) -> Result<(), ShellError> {
|
||||
let examples = cmd.examples();
|
||||
|
||||
let base_context = basic_evaluation_context()?;
|
||||
let base_context = basic_evaluation_context(FilesystemShellMode::Cli)?;
|
||||
|
||||
base_context.add_commands(vec![
|
||||
// Command Doubles
|
||||
@ -92,7 +93,7 @@ pub fn test_examples(cmd: Command) -> Result<(), ShellError> {
|
||||
pub fn test(cmd: impl WholeStreamCommand + 'static) -> Result<(), ShellError> {
|
||||
let examples = cmd.examples();
|
||||
|
||||
let base_context = basic_evaluation_context()?;
|
||||
let base_context = basic_evaluation_context(FilesystemShellMode::Cli)?;
|
||||
|
||||
base_context.add_commands(vec![
|
||||
whole_stream_command(Echo {}),
|
||||
@ -149,7 +150,7 @@ pub fn test(cmd: impl WholeStreamCommand + 'static) -> Result<(), ShellError> {
|
||||
pub fn test_anchors(cmd: Command) -> Result<(), ShellError> {
|
||||
let examples = cmd.examples();
|
||||
|
||||
let base_context = basic_evaluation_context()?;
|
||||
let base_context = basic_evaluation_context(FilesystemShellMode::Cli)?;
|
||||
|
||||
base_context.add_commands(vec![
|
||||
// Minimal restricted commands to aid in testing
|
||||
|
Reference in New Issue
Block a user