mirror of
https://github.com/nushell/nushell.git
synced 2025-06-30 22:50:14 +02:00
Add export-env
command (#6355)
* WIP Start export-env * Add missing file * Do not modify the parser Let's leave that for later * Enable tests for export-env; Fmt
This commit is contained in:
74
crates/nu-command/src/env/export_env.rs
vendored
Normal file
74
crates/nu-command/src/env/export_env.rs
vendored
Normal file
@ -0,0 +1,74 @@
|
||||
use nu_engine::{eval_block, redirect_env, CallExt};
|
||||
use nu_protocol::{
|
||||
ast::Call,
|
||||
engine::{CaptureBlock, Command, EngineState, Stack},
|
||||
Category, Example, PipelineData, Signature, Span, SyntaxShape, Value,
|
||||
};
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct ExportEnv;
|
||||
|
||||
impl Command for ExportEnv {
|
||||
fn name(&self) -> &str {
|
||||
"export-env"
|
||||
}
|
||||
|
||||
fn signature(&self) -> Signature {
|
||||
Signature::build("export-env")
|
||||
.required(
|
||||
"block",
|
||||
SyntaxShape::Block(Some(vec![])),
|
||||
"the block to run to set the environment",
|
||||
)
|
||||
.category(Category::Env)
|
||||
}
|
||||
|
||||
fn usage(&self) -> &str {
|
||||
"Run a block and preserve its environment in a current scope."
|
||||
}
|
||||
|
||||
fn run(
|
||||
&self,
|
||||
engine_state: &EngineState,
|
||||
caller_stack: &mut Stack,
|
||||
call: &Call,
|
||||
input: PipelineData,
|
||||
) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> {
|
||||
let capture_block: CaptureBlock = call.req(engine_state, caller_stack, 0)?;
|
||||
let block = engine_state.get_block(capture_block.block_id);
|
||||
let mut callee_stack = caller_stack.captures_to_stack(&capture_block.captures);
|
||||
|
||||
let _ = eval_block(
|
||||
engine_state,
|
||||
&mut callee_stack,
|
||||
block,
|
||||
input,
|
||||
call.redirect_stdout,
|
||||
call.redirect_stderr,
|
||||
);
|
||||
|
||||
redirect_env(engine_state, caller_stack, &callee_stack);
|
||||
|
||||
Ok(PipelineData::new(call.head))
|
||||
}
|
||||
|
||||
fn examples(&self) -> Vec<Example> {
|
||||
vec![Example {
|
||||
description: "Set an environment",
|
||||
example: r#"export-env { let-env SPAM = 'eggs' }; $env.SPAM"#,
|
||||
result: Some(Value::string("eggs", Span::test_data())),
|
||||
}]
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_examples() {
|
||||
use crate::test_examples;
|
||||
|
||||
test_examples(ExportEnv {})
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user