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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
use nu_engine::command_prelude::*;

use crate::util::{canonicalize_possible_filename_arg, modify_plugin_file};

#[derive(Clone)]
pub struct PluginRm;

impl Command for PluginRm {
    fn name(&self) -> &str {
        "plugin rm"
    }

    fn signature(&self) -> Signature {
        Signature::build(self.name())
            .input_output_type(Type::Nothing, Type::Nothing)
            // This matches the option to `nu`
            .named(
                "plugin-config",
                SyntaxShape::Filepath,
                "Use a plugin registry file other than the one set in `$nu.plugin-path`",
                None,
            )
            .switch(
                "force",
                "Don't cause an error if the plugin name wasn't found in the file",
                Some('f'),
            )
            .required(
                "name",
                SyntaxShape::String,
                "The name, or filename, of the plugin to remove",
            )
            .category(Category::Plugin)
    }

    fn usage(&self) -> &str {
        "Remove a plugin from the plugin registry file."
    }

    fn extra_usage(&self) -> &str {
        r#"
This does not remove the plugin commands from the current scope or from `plugin
list` in the current shell. It instead removes the plugin from the plugin
registry file (by default, `$nu.plugin-path`). The changes will be apparent the
next time `nu` is launched with that plugin registry file.

This can be useful for removing an invalid plugin signature, if it can't be
fixed with `plugin add`.
"#
        .trim()
    }

    fn search_terms(&self) -> Vec<&str> {
        vec!["remove", "delete", "signature"]
    }

    fn examples(&self) -> Vec<Example> {
        vec![
            Example {
                example: "plugin rm inc",
                description: "Remove the installed signatures for the `inc` plugin.",
                result: None,
            },
            Example {
                example: "plugin rm ~/.cargo/bin/nu_plugin_inc",
                description: "Remove the installed signatures for the plugin with the filename `~/.cargo/bin/nu_plugin_inc`.",
                result: None,
            },
            Example {
                example: "plugin rm --plugin-config polars.msgpackz polars",
                description: "Remove the installed signatures for the `polars` plugin from the \"polars.msgpackz\" plugin registry file.",
                result: None,
            },
        ]
    }

    fn run(
        &self,
        engine_state: &EngineState,
        stack: &mut Stack,
        call: &Call,
        _input: PipelineData,
    ) -> Result<PipelineData, ShellError> {
        let name: Spanned<String> = call.req(engine_state, stack, 0)?;
        let custom_path = call.get_flag(engine_state, stack, "plugin-config")?;
        let force = call.has_flag(engine_state, stack, "force")?;

        let filename = canonicalize_possible_filename_arg(engine_state, stack, &name.item);

        modify_plugin_file(engine_state, stack, call.head, custom_path, |contents| {
            if let Some(index) = contents
                .plugins
                .iter()
                .position(|p| p.name == name.item || p.filename == filename)
            {
                contents.plugins.remove(index);
                Ok(())
            } else if force {
                Ok(())
            } else {
                Err(ShellError::GenericError {
                    error: format!("Failed to remove the `{}` plugin", name.item),
                    msg: "couldn't find a plugin with this name in the registry file".into(),
                    span: Some(name.span),
                    help: None,
                    inner: vec![],
                })
            }
        })?;

        Ok(Value::nothing(call.head).into_pipeline_data())
    }
}