2019-07-13 01:55:06 -07:00
|
|
|
use crate::common::*;
|
|
|
|
|
|
|
|
/// 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
|
|
|
|
}
|
|
|
|
.to_string(),
|
|
|
|
),
|
|
|
|
}
|
2020-02-10 20:07:06 -08:00
|
|
|
},
|
2019-07-13 01:55:06 -07:00
|
|
|
Err(io_error) => Err(OutputError::Io(io_error)),
|
|
|
|
}
|
|
|
|
}
|