2022-06-18 21:56:31 -07:00
|
|
|
use super::*;
|
2019-07-13 01:55:06 -07:00
|
|
|
|
|
|
|
/// Run a command and return the data it wrote to stdout as a string
|
2019-09-21 15:35:03 -07:00
|
|
|
pub(crate) fn output(mut command: Command) -> Result<String, OutputError> {
|
2019-07-13 01:55:06 -07:00
|
|
|
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,
|
|
|
|
});
|
|
|
|
}
|
2023-10-16 20:07:09 -07:00
|
|
|
match str::from_utf8(&output.stdout) {
|
2019-07-13 01:55:06 -07:00
|
|
|
Err(error) => Err(OutputError::Utf8(error)),
|
2024-05-14 16:30:19 -07:00
|
|
|
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]
|
2019-07-13 01:55:06 -07:00
|
|
|
} else {
|
2024-05-14 16:30:19 -07:00
|
|
|
output
|
2019-07-13 01:55:06 -07:00
|
|
|
}
|
2021-02-15 01:18:31 -08:00
|
|
|
.to_owned(),
|
2019-07-13 01:55:06 -07:00
|
|
|
),
|
|
|
|
}
|
2021-09-16 06:44:40 -07:00
|
|
|
}
|
2019-07-13 01:55:06 -07:00
|
|
|
Err(io_error) => Err(OutputError::Io(io_error)),
|
|
|
|
}
|
|
|
|
}
|