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,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
match std::str::from_utf8(&output.stdout) {
|
|
|
|
Err(error) => Err(OutputError::Utf8(error)),
|
|
|
|
Ok(utf8) => Ok(
|
|
|
|
if utf8.ends_with('\n') {
|
|
|
|
&utf8[0..utf8.len() - 1]
|
|
|
|
} else if utf8.ends_with("\r\n") {
|
|
|
|
&utf8[0..utf8.len() - 2]
|
|
|
|
} else {
|
|
|
|
utf8
|
|
|
|
}
|
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)),
|
|
|
|
}
|
|
|
|
}
|