1
0
mirror of https://github.com/jht5945/rust_util.git synced 2025-12-27 07:30: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,6 +1,6 @@
[package]
name = "rust_util"
version = "0.6.33"
version = "0.6.34"
authors = ["Hatter Jiang <jht5945@gmail.com>"]
edition = "2018"
description = "Hatter's Rust Util"

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()

View File

@@ -92,6 +92,12 @@ pub fn is_logger_level_enabled(mt: MessageType) -> bool {
mt.get_u8_value() >= logger_level.get_u8_value()
}
pub fn when<F>(mt: MessageType, f: F) where F: Fn() -> () {
if is_logger_level_enabled(mt) {
f();
}
}
pub fn print_message(mt: MessageType, message: &str) {
if is_logger_level_enabled(mt) {
match mt {