forked from extern/nushell
d06f457b2a
* move commands, futures.rs, script.rs, utils * move over maybe_print_errors * add nu_command crate references to nu_cli * in commands.rs open up to pub mod from pub(crate) * nu-cli, nu-command, and nu tests are now passing * cargo fmt * clean up nu-cli/src/prelude.rs * code cleanup * for some reason lex.rs was not formatted, may be causing my error * remove mod completion from lib.rs which was not being used along with quickcheck macros * add in allow unused imports * comment out one failing external test; comment out one failing internal test * revert commenting out failing tests; something else might be going on; someone with a windows machine should check and see what is going on with these failing windows tests * Update Cargo.toml Extend the optional features to nu-command Co-authored-by: Jonathan Turner <jonathandturner@users.noreply.github.com>
70 lines
1.8 KiB
Rust
70 lines
1.8 KiB
Rust
use crate::prelude::*;
|
|
use nu_engine::WholeStreamCommand;
|
|
|
|
use nu_errors::ShellError;
|
|
use nu_protocol::{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 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(&filename.item);
|
|
match contents {
|
|
Ok(contents) => {
|
|
let result = crate::script::run_script_standalone(contents, true, &ctx, false).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())
|
|
}
|
|
}
|
|
}
|