nushell/crates/nu-command/src/commands/source.rs
JT 131b5b56d7
Finish removing arg deserialization (#3552)
* WIP remove process

* WIP

* WIP

* Finish removing arg deserialization
2021-06-04 18:23:57 +12:00

71 lines
1.8 KiB
Rust

use crate::prelude::*;
use nu_engine::{script, WholeStreamCommand};
use nu_errors::ShellError;
use nu_parser::expand_path;
use nu_protocol::{Signature, SyntaxShape};
use nu_source::Tagged;
pub struct Source;
#[derive(Deserialize)]
pub struct SourceArgs {
pub filename: Tagged<String>,
}
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."
}
fn run_with_actions(&self, args: CommandArgs) -> Result<ActionStream, ShellError> {
source(args)
}
fn examples(&self) -> Vec<Example> {
vec![]
}
}
pub fn source(args: CommandArgs) -> Result<ActionStream, ShellError> {
let ctx = EvaluationContext::from_args(&args);
let args = args.evaluate_once()?;
let filename: Tagged<String> = args.req(0)?;
// 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);
if let Err(err) = result {
ctx.error(err.into());
}
Ok(ActionStream::empty())
}
Err(_) => {
ctx.error(ShellError::labeled_error(
"Can't load file to source",
"can't load file",
filename.span(),
));
Ok(ActionStream::empty())
}
}
}