nu_command/system/
complete.rs

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

#[derive(Clone)]
pub struct Complete;

impl Command for Complete {
    fn name(&self) -> &str {
        "complete"
    }

    fn signature(&self) -> Signature {
        Signature::build("complete")
            .category(Category::System)
            .input_output_types(vec![(Type::Any, Type::record())])
    }

    fn description(&self) -> &str {
        "Capture the outputs and exit code from an external piped in command in a nushell table."
    }

    fn extra_description(&self) -> &str {
        r#"In order to capture stdout, stderr, and exit_code, externally piped in commands need to be wrapped with `do`"#
    }

    fn run(
        &self,
        _engine_state: &EngineState,
        _stack: &mut Stack,
        call: &Call,
        input: PipelineData,
    ) -> Result<PipelineData, ShellError> {
        let head = call.head;
        match input {
            PipelineData::ByteStream(stream, ..) => {
                let Ok(child) = stream.into_child() else {
                    return Err(ShellError::GenericError {
                        error: "Complete only works with external commands".into(),
                        msg: "complete only works on external commands".into(),
                        span: Some(call.head),
                        help: None,
                        inner: vec![],
                    });
                };

                let output = child.wait_with_output()?;
                let exit_code = output.exit_status.code();
                let mut record = Record::new();

                if let Some(stdout) = output.stdout {
                    record.push(
                        "stdout",
                        match String::from_utf8(stdout) {
                            Ok(str) => Value::string(str, head),
                            Err(err) => Value::binary(err.into_bytes(), head),
                        },
                    );
                }

                if let Some(stderr) = output.stderr {
                    record.push(
                        "stderr",
                        match String::from_utf8(stderr) {
                            Ok(str) => Value::string(str, head),
                            Err(err) => Value::binary(err.into_bytes(), head),
                        },
                    );
                }

                record.push("exit_code", Value::int(exit_code.into(), head));

                Ok(Value::record(record, call.head).into_pipeline_data())
            }
            // bubble up errors from the previous command
            PipelineData::Value(Value::Error { error, .. }, _) => Err(*error),
            _ => Err(ShellError::GenericError {
                error: "Complete only works with external commands".into(),
                msg: "complete only works on external commands".into(),
                span: Some(head),
                help: None,
                inner: vec![],
            }),
        }
    }

    fn examples(&self) -> Vec<Example> {
        vec![Example {
            description:
                "Run the external command to completion, capturing stdout, stderr, and exit_code",
            example: "^external arg1 | complete",
            result: None,
        }]
    }

    fn pipe_redirection(&self) -> (Option<OutDest>, Option<OutDest>) {
        (Some(OutDest::PipeSeparate), Some(OutDest::PipeSeparate))
    }
}