use nu_engine::command_prelude::*;
use nu_protocol::OutDest;
use nu_protocol::shell_error::generic::GenericError;
#[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 {
"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(mut child) = stream.into_child() else {
return Err(ShellError::Generic(GenericError::new(
"Complete only works with external commands",
"complete only works on external commands",
call.head,
)));
};
#[cfg(feature = "os")]
child.ignore_error(true);
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())
}
PipelineData::Value(Value::Error { error, .. }, _) => Err(*error),
_ => Err(ShellError::Generic(GenericError::new(
"Complete only works with external commands",
"complete only works on external commands",
head,
))),
}
}
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))
}
}