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
use nu_engine::command_prelude::*;

use crate::util::canonicalize_possible_filename_arg;

#[derive(Clone)]
pub struct PluginStop;

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

    fn signature(&self) -> Signature {
        Signature::build("plugin stop")
            .input_output_type(Type::Nothing, Type::Nothing)
            .required(
                "name",
                SyntaxShape::String,
                "The name, or filename, of the plugin to stop",
            )
            .category(Category::Plugin)
    }

    fn usage(&self) -> &str {
        "Stop an installed plugin if it was running."
    }

    fn examples(&self) -> Vec<nu_protocol::Example> {
        vec![
            Example {
                example: "plugin stop inc",
                description: "Stop the plugin named `inc`.",
                result: None,
            },
            Example {
                example: "plugin stop ~/.cargo/bin/nu_plugin_inc",
                description: "Stop the plugin with the filename `~/.cargo/bin/nu_plugin_inc`.",
                result: None,
            },
            Example {
                example: "plugin list | each { |p| plugin stop $p.name }",
                description: "Stop all plugins.",
                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 filename = canonicalize_possible_filename_arg(engine_state, stack, &name.item);

        let mut found = false;
        for plugin in engine_state.plugins() {
            let id = &plugin.identity();
            if id.name() == name.item || id.filename() == filename {
                plugin.stop()?;
                found = true;
            }
        }

        if found {
            Ok(PipelineData::Empty)
        } else {
            Err(ShellError::GenericError {
                error: format!("Failed to stop the `{}` plugin", name.item),
                msg: "couldn't find a plugin with this name".into(),
                span: Some(name.span),
                help: Some("you may need to `register` the plugin first".into()),
                inner: vec![],
            })
        }
    }
}