mirror of
https://github.com/nushell/nushell.git
synced 2025-05-30 19:32:46 +00:00
# Description Partialy addresses #13868. `try` does not catch non-zero exit code errors from the last command in a pipeline if the result is assigned to a variable using `let` (or `mut`). This was fixed by adding a new `OutDest::Value` case. This is used when the pipeline is in a "value" position. I.e., it will be collected into a value. This ended up replacing most of the usages of `OutDest::Capture`. So, this PR also renames `OutDest::Capture` to `OutDest::PipeSeparate` to better fit the few remaining use cases for it. # User-Facing Changes Bug fix. # Tests + Formatting Added two tests.
99 lines
3.3 KiB
Rust
99 lines
3.3 KiB
Rust
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))
|
|
}
|
|
}
|