nu_command/env/
export_env.rs

1use nu_engine::{command_prelude::*, get_eval_block, redirect_env};
2use nu_protocol::engine::CommandType;
3
4#[derive(Clone)]
5pub struct ExportEnv;
6
7impl Command for ExportEnv {
8    fn name(&self) -> &str {
9        "export-env"
10    }
11
12    fn signature(&self) -> Signature {
13        Signature::build("export-env")
14            .input_output_types(vec![(Type::Nothing, Type::Nothing)])
15            .required(
16                "block",
17                SyntaxShape::Block,
18                "The block to run to set the environment.",
19            )
20            .category(Category::Env)
21    }
22
23    fn description(&self) -> &str {
24        "Run a block and preserve its environment in a current scope."
25    }
26
27    fn extra_description(&self) -> &str {
28        r#"This command is a parser keyword. For details, check:
29  https://www.nushell.sh/book/thinking_in_nu.html"#
30    }
31
32    fn command_type(&self) -> CommandType {
33        CommandType::Keyword
34    }
35
36    fn requires_ast_for_arguments(&self) -> bool {
37        true
38    }
39
40    fn run(
41        &self,
42        engine_state: &EngineState,
43        caller_stack: &mut Stack,
44        call: &Call,
45        input: PipelineData,
46    ) -> Result<PipelineData, ShellError> {
47        let block_id = call
48            .positional_nth(caller_stack, 0)
49            .expect("checked through parser")
50            .as_block()
51            .expect("internal error: missing block");
52
53        let block = engine_state.get_block(block_id);
54        let mut callee_stack = caller_stack
55            .gather_captures(engine_state, &block.captures)
56            .reset_pipes();
57
58        let eval_block = get_eval_block(engine_state);
59
60        // Run the block (discard the result)
61        let _ = eval_block(engine_state, &mut callee_stack, block, input)?;
62
63        // Merge the block's environment to the current stack
64        redirect_env(engine_state, caller_stack, &callee_stack);
65
66        Ok(PipelineData::empty())
67    }
68
69    fn examples(&self) -> Vec<Example> {
70        vec![
71            Example {
72                description: "Set an environment variable",
73                example: r#"export-env { $env.SPAM = 'eggs' }"#,
74                result: Some(Value::nothing(Span::test_data())),
75            },
76            Example {
77                description: "Set an environment variable and examine its value",
78                example: r#"export-env { $env.SPAM = 'eggs' }; $env.SPAM"#,
79                result: Some(Value::test_string("eggs")),
80            },
81        ]
82    }
83}
84
85#[cfg(test)]
86mod test {
87    use super::*;
88
89    #[test]
90    fn test_examples() {
91        use crate::test_examples;
92
93        test_examples(ExportEnv {})
94    }
95}