1
0
mirror of https://github.com/jht5945/rust_util.git synced 2026-01-12 07:10:05 +08:00

feat: cmd

This commit is contained in:
2021-05-28 00:59:07 +08:00
parent b4c789aebc
commit 7e752d95b5
3 changed files with 38 additions and 2 deletions

View File

@@ -1,5 +1,35 @@
use std::io::{self, Error, ErrorKind};
use std::process::{Command, ExitStatus};
use std::process::{Command, ExitStatus, Output};
use crate::util_msg::{print_debug, print_error, MessageType};
pub fn run_command_or_exit(cmd: &str, args: &[&str]) -> Output {
let mut c = Command::new(cmd);
c.args(args);
crate::util_msg::when(MessageType::DEBUG, || {
print_debug(&format!("Run command: {:?}", c));
});
let output = c.output();
match output {
Err(e) => {
print_error(&format!("Run command: {:?}, failed: {}", c, e));
std::process::exit(-1);
},
Ok(output) => {
if !output.status.success() {
print_error(&format!(r##"Run command failed, code: {:?}
-----std out---------------------------------------------------------------
{}
-----std err---------------------------------------------------------------
{}
---------------------------------------------------------------------------"##,
output.status.code(),
String::from_utf8_lossy(&output.stdout),
String::from_utf8_lossy(&output.stderr)));
}
output
}
}
}
pub fn run_command_and_wait(cmd: &mut Command) -> io::Result<ExitStatus> {
cmd.spawn()?.wait()