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

42 lines
1.2 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;
2021-10-25 08:31:39 +02:00
use nu_protocol::engine::{Command, EngineState, EvaluationContext, Stack};
2021-10-25 06:01:02 +02:00
use nu_protocol::{PipelineData, ShellError, Signature, SyntaxShape, Value};
2021-09-11 06:54:24 +02:00
/// Source a file for environment variables.
2021-10-25 06:01:02 +02:00
#[derive(Clone)]
2021-09-11 06:54:24 +02:00
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-25 08:31:39 +02:00
engine_state: &EngineState,
stack: &mut Stack,
2021-09-11 06:54:24 +02:00
call: &Call,
2021-10-25 06:01:02 +02:00
input: PipelineData,
) -> Result<PipelineData, 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
2021-10-25 08:31:39 +02:00
let block_id: i64 = call.req(engine_state, stack, 1)?;
2021-10-06 04:29:05 +02:00
2021-10-25 08:31:39 +02:00
let block = engine_state.get_block(block_id as usize).clone();
eval_block(engine_state, stack, &block, input)
2021-09-11 06:54:24 +02:00
}
}