2021-04-03 21:31:53 +02:00
|
|
|
use std::path::PathBuf;
|
|
|
|
|
2020-12-19 08:47:34 +01:00
|
|
|
use crate::prelude::*;
|
2021-03-12 06:20:54 +01:00
|
|
|
use nu_engine::{script, WholeStreamCommand};
|
2020-12-19 08:47:34 +01:00
|
|
|
|
|
|
|
use nu_errors::ShellError;
|
2021-02-15 09:41:49 +01:00
|
|
|
use nu_parser::expand_path;
|
2021-04-03 21:31:53 +02:00
|
|
|
use nu_protocol::{NuScript, RunScriptOptions, Signature, SyntaxShape};
|
2020-12-19 08:47:34 +01:00
|
|
|
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> {
|
2021-01-03 07:44:21 +01:00
|
|
|
let ctx = EvaluationContext::from_args(&args);
|
2020-12-19 08:47:34 +01:00
|
|
|
let (SourceArgs { filename }, _) = args.process().await?;
|
|
|
|
|
2021-04-03 21:31:53 +02:00
|
|
|
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;
|
2021-01-03 07:44:21 +01:00
|
|
|
|
2021-04-03 21:31:53 +02:00
|
|
|
Ok(OutputStream::empty())
|
2020-12-19 08:47:34 +01:00
|
|
|
}
|