nushell/crates/nu-command/src/core_commands/source.rs

44 lines
1.1 KiB
Rust
Raw Normal View History

2021-10-06 04:29:05 +02:00
use nu_engine::{eval_block, CallExt};
2021-10-02 04:17:32 +02:00
use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EvaluationContext};
2021-10-03 20:23:23 +02:00
use nu_protocol::{ShellError, Signature, SyntaxShape, Value};
2021-09-11 06:54:24 +02:00
/// Source a file for environment variables.
pub struct Source;
impl Command for Source {
fn name(&self) -> &str {
"source"
}
fn signature(&self) -> Signature {
Signature::build("source").required(
"filename",
SyntaxShape::Filepath,
2021-09-11 06:54:24 +02:00
"the filepath to the script file to source",
)
}
fn usage(&self) -> &str {
"Runs a script file in the current context."
}
fn run(
&self,
2021-10-06 04:29:05 +02:00
context: &EvaluationContext,
2021-09-11 06:54:24 +02:00
call: &Call,
input: Value,
2021-10-03 20:23:23 +02:00
) -> Result<Value, ShellError> {
2021-10-06 04:29:05 +02:00
// Note: this hidden positional is the block_id that corresponded to the 0th position
// it is put here by the parser
let block_id: i64 = call.req(context, 1)?;
let block = context
.engine_state
.borrow()
.get_block(block_id as usize)
.clone();
eval_block(context, &block, input)
2021-09-11 06:54:24 +02:00
}
}