Add script sourcing (#2803)

* Add script sourcing

* clippy
This commit is contained in:
Jonathan Turner
2020-12-19 20:47:34 +13:00
committed by GitHub
parent 058ef69da3
commit e5b136f70d
21 changed files with 98 additions and 42 deletions

View File

@ -35,7 +35,6 @@ impl WholeStreamCommand for Command {
ctrl_c: args.ctrl_c,
current_errors: args.current_errors,
name: args.call_info.name_tag,
raw_input: args.raw_input,
})
.await
}

View File

@ -47,7 +47,7 @@ pub(crate) async fn run_internal_command(
.then(move |item| {
let head = head.clone();
let command = command.clone();
let mut context = context.clone();
let context = context.clone();
async move {
match item {
Ok(ReturnSuccess::Action(action)) => match action {
@ -188,6 +188,29 @@ pub(crate) async fn run_internal_command(
context.scope.add_env_var(name, value);
InputStream::from_stream(futures::stream::iter(vec![]))
}
CommandAction::SourceScript(filename) => {
let contents = std::fs::read_to_string(&filename);
if let Ok(contents) = contents {
let result = crate::cli::run_script_standalone(
contents, true, &context, false,
)
.await;
if let Err(err) = result {
return InputStream::one(
UntaggedValue::Error(err.into()).into_untagged_value(),
);
}
InputStream::from_stream(futures::stream::iter(vec![]))
} else {
InputStream::one(
UntaggedValue::Error(ShellError::untagged_runtime_error(
format!("could not source '{}'", filename),
))
.into_untagged_value(),
)
}
}
CommandAction::AddPlugins(path) => {
match crate::plugin::scan(vec![std::path::PathBuf::from(path)]) {
Ok(plugins) => {

View File

@ -43,7 +43,6 @@ pub struct CommandArgs {
pub call_info: UnevaluatedCallInfo,
pub scope: Scope,
pub input: InputStream,
pub raw_input: String,
}
#[derive(Getters, Clone)]
@ -67,7 +66,6 @@ impl RawCommandArgs {
call_info: self.call_info,
scope: self.scope,
input: input.into(),
raw_input: String::default(),
}
}
}
@ -116,7 +114,6 @@ pub struct RunnableContext {
pub current_errors: Arc<Mutex<Vec<ShellError>>>,
pub scope: Scope,
pub name: Tag,
pub raw_input: String,
}
impl RunnableContext {

View File

@ -38,7 +38,6 @@ impl WholeStreamCommand for SubCommand {
ctrl_c: args.ctrl_c,
current_errors: args.current_errors,
name: args.call_info.name_tag,
raw_input: args.raw_input,
},
average,
)

View File

@ -31,7 +31,6 @@ impl WholeStreamCommand for SubCommand {
ctrl_c: args.ctrl_c,
current_errors: args.current_errors,
name: args.call_info.name_tag,
raw_input: args.raw_input,
},
ceil_big_int,
ceil_big_decimal,

View File

@ -31,7 +31,6 @@ impl WholeStreamCommand for SubCommand {
ctrl_c: args.ctrl_c,
current_errors: args.current_errors,
name: args.call_info.name_tag,
raw_input: args.raw_input,
},
floor_big_int,
floor_big_decimal,

View File

@ -31,7 +31,6 @@ impl WholeStreamCommand for SubCommand {
ctrl_c: args.ctrl_c,
current_errors: args.current_errors,
name: args.call_info.name_tag,
raw_input: args.raw_input,
},
maximum,
)

View File

@ -35,7 +35,6 @@ impl WholeStreamCommand for SubCommand {
ctrl_c: args.ctrl_c,
current_errors: args.current_errors,
name: args.call_info.name_tag,
raw_input: args.raw_input,
},
median,
)

View File

@ -31,7 +31,6 @@ impl WholeStreamCommand for SubCommand {
ctrl_c: args.ctrl_c,
current_errors: args.current_errors,
name: args.call_info.name_tag,
raw_input: args.raw_input,
},
minimum,
)

View File

@ -31,7 +31,6 @@ impl WholeStreamCommand for SubCommand {
ctrl_c: args.ctrl_c,
current_errors: args.current_errors,
name: args.call_info.name_tag,
raw_input: args.raw_input,
},
mode,
)

View File

@ -34,7 +34,6 @@ impl WholeStreamCommand for SubCommand {
ctrl_c: args.ctrl_c,
current_errors: args.current_errors,
name: args.call_info.name_tag,
raw_input: args.raw_input,
},
product,
)

View File

@ -35,7 +35,6 @@ impl WholeStreamCommand for SubCommand {
ctrl_c: args.ctrl_c,
current_errors: args.current_errors,
name: args.call_info.name_tag,
raw_input: args.raw_input,
},
summation,
)

View File

@ -88,7 +88,6 @@ impl WholeStreamCommand for RunExternalCommand {
ctrl_c: args.ctrl_c.clone(),
current_errors: Arc::new(Mutex::new(vec![])),
windows_drives_previous_cwd: Arc::new(Mutex::new(std::collections::HashMap::new())),
raw_input: String::default(),
}
};

View File

@ -165,7 +165,7 @@ async fn save(raw_args: CommandArgs) -> Result<OutputStream, ShellError> {
let host = raw_args.host.clone();
let ctrl_c = raw_args.ctrl_c.clone();
let current_errors = raw_args.current_errors.clone();
let mut shell_manager = raw_args.shell_manager.clone();
let shell_manager = raw_args.shell_manager.clone();
let head = raw_args.call_info.args.head.clone();
let (

View File

@ -0,0 +1,48 @@
use crate::commands::WholeStreamCommand;
use crate::prelude::*;
use nu_errors::ShellError;
use nu_protocol::{CommandAction, ReturnSuccess, Signature, SyntaxShape};
use nu_source::Tagged;
pub struct Source;
#[derive(Deserialize)]
pub struct SourceArgs {
pub filename: Tagged<String>,
}
#[async_trait]
impl WholeStreamCommand for Source {
fn name(&self) -> &str {
"source"
}
fn signature(&self) -> Signature {
Signature::build("source").required(
"filename",
SyntaxShape::String,
"the filepath to the script file to source",
)
}
fn usage(&self) -> &str {
"Runs a script file in the current context."
}
async fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
source(args).await
}
fn examples(&self) -> Vec<Example> {
vec![]
}
}
pub async fn source(args: CommandArgs) -> Result<OutputStream, ShellError> {
let (SourceArgs { filename }, _) = args.process().await?;
Ok(OutputStream::one(ReturnSuccess::action(
CommandAction::SourceScript(filename.item),
)))
}