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:
@@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "rust_util"
|
name = "rust_util"
|
||||||
version = "0.6.33"
|
version = "0.6.34"
|
||||||
authors = ["Hatter Jiang <jht5945@gmail.com>"]
|
authors = ["Hatter Jiang <jht5945@gmail.com>"]
|
||||||
edition = "2018"
|
edition = "2018"
|
||||||
description = "Hatter's Rust Util"
|
description = "Hatter's Rust Util"
|
||||||
|
|||||||
@@ -1,5 +1,35 @@
|
|||||||
use std::io::{self, Error, ErrorKind};
|
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> {
|
pub fn run_command_and_wait(cmd: &mut Command) -> io::Result<ExitStatus> {
|
||||||
cmd.spawn()?.wait()
|
cmd.spawn()?.wait()
|
||||||
|
|||||||
@@ -92,6 +92,12 @@ pub fn is_logger_level_enabled(mt: MessageType) -> bool {
|
|||||||
mt.get_u8_value() >= logger_level.get_u8_value()
|
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) {
|
pub fn print_message(mt: MessageType, message: &str) {
|
||||||
if is_logger_level_enabled(mt) {
|
if is_logger_level_enabled(mt) {
|
||||||
match mt {
|
match mt {
|
||||||
|
|||||||
Reference in New Issue
Block a user