nu_command/env/config/
config_nu.rs

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