1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
use crate::prelude::*;
use nu_engine::WholeStreamCommand;
use nu_errors::ShellError;
use nu_protocol::{ReturnSuccess, Signature, UntaggedValue};

pub struct SubCommand;

impl WholeStreamCommand for SubCommand {
    fn name(&self) -> &str {
        "config clear"
    }

    fn signature(&self) -> Signature {
        Signature::build("config clear")
    }

    fn usage(&self) -> &str {
        "clear the config"
    }

    fn run_with_actions(&self, args: CommandArgs) -> Result<ActionStream, ShellError> {
        clear(args)
    }

    fn examples(&self) -> Vec<Example> {
        vec![Example {
            description: "Clear the config (be careful!)",
            example: "config clear",
            result: None,
        }]
    }
}

pub fn clear(args: CommandArgs) -> Result<ActionStream, ShellError> {
    let ctx = EvaluationContext::from_args(&args);

    let result = if let Some(global_cfg) = &mut args.configs.lock().global_config {
        global_cfg.vars.clear();
        global_cfg.write()?;
        ctx.reload_config(global_cfg)?;
        Ok(ActionStream::one(ReturnSuccess::value(
            UntaggedValue::Row(global_cfg.vars.clone().into()).into_value(args.call_info.name_tag),
        )))
    } else {
        Ok(vec![ReturnSuccess::value(UntaggedValue::Error(
            crate::commands::config::err_no_global_cfg_present(),
        ))]
        .into_iter()
        .to_action_stream())
    };

    result
}