just/src/output.rs
Casey Rodarmor ad212c77bf
Fix output \r\n stripping (#2035)
Test if output ends with `\r\n` before `\n`, since the test for `\n` will
succeed if output is terminated with `\r\n`, so `\r\n` is properly stripped
from output text.
2024-05-14 23:30:19 +00:00

35 lines
1019 B
Rust

use super::*;
/// Run a command and return the data it wrote to stdout as a string
pub(crate) fn output(mut command: Command) -> Result<String, OutputError> {
match command.output() {
Ok(output) => {
if let Some(code) = output.status.code() {
if code != 0 {
return Err(OutputError::Code(code));
}
} else {
let signal = Platform::signal_from_exit_status(output.status);
return Err(match signal {
Some(signal) => OutputError::Signal(signal),
None => OutputError::Unknown,
});
}
match str::from_utf8(&output.stdout) {
Err(error) => Err(OutputError::Utf8(error)),
Ok(output) => Ok(
if output.ends_with("\r\n") {
&output[0..output.len() - 2]
} else if output.ends_with('\n') {
&output[0..output.len() - 1]
} else {
output
}
.to_owned(),
),
}
}
Err(io_error) => Err(OutputError::Io(io_error)),
}
}