nu_command/env/config/
config_env.rs

1use nu_engine::command_prelude::*;
2
3#[derive(Clone)]
4pub struct ConfigEnv;
5
6impl Command for ConfigEnv {
7    fn name(&self) -> &str {
8        "config env"
9    }
10
11    fn signature(&self) -> Signature {
12        Signature::build(self.name())
13            .category(Category::Env)
14            .input_output_types(vec![(Type::Nothing, Type::Any)])
15            .switch(
16                "default",
17                "Print the internal default `env.nu` file instead.",
18                Some('d'),
19            )
20            .switch(
21                "doc",
22                "Print a commented `env.nu` with documentation instead.",
23                Some('s'),
24            )
25        // TODO: Signature narrower than what run actually supports theoretically
26    }
27
28    fn description(&self) -> &str {
29        "Edit nu environment configurations."
30    }
31
32    fn examples(&self) -> Vec<Example> {
33        vec![
34            Example {
35                description: "open user's env.nu in the default editor",
36                example: "config env",
37                result: None,
38            },
39            Example {
40                description: "pretty-print a commented `env.nu` that explains common settings",
41                example: "config env --doc | nu-highlight,",
42                result: None,
43            },
44            Example {
45                description: "pretty-print the internal `env.nu` file which is loaded before the user's environment",
46                example: "config env --default | nu-highlight,",
47                result: None,
48            },
49        ]
50    }
51
52    fn run(
53        &self,
54        engine_state: &EngineState,
55        stack: &mut Stack,
56        call: &Call,
57        _input: PipelineData,
58    ) -> Result<PipelineData, ShellError> {
59        let default_flag = call.has_flag(engine_state, stack, "default")?;
60        let doc_flag = call.has_flag(engine_state, stack, "doc")?;
61        if default_flag && doc_flag {
62            return Err(ShellError::IncompatibleParameters {
63                left_message: "can't use `--default` at the same time".into(),
64                left_span: call.get_flag_span(stack, "default").expect("has flag"),
65                right_message: "because of `--doc`".into(),
66                right_span: call.get_flag_span(stack, "doc").expect("has flag"),
67            });
68        }
69        // `--default` flag handling
70        if call.has_flag(engine_state, stack, "default")? {
71            let head = call.head;
72            return Ok(Value::string(nu_utils::get_default_env(), head).into_pipeline_data());
73        }
74
75        // `--doc` flag handling
76        if doc_flag {
77            let head = call.head;
78            return Ok(Value::string(nu_utils::get_doc_env(), head).into_pipeline_data());
79        }
80
81        super::config_::start_editor("env-path", engine_state, stack, call)
82    }
83}