-
Notifications
You must be signed in to change notification settings - Fork 499
/
output.rs
34 lines (33 loc) · 1019 Bytes
/
output.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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)),
}
}