mirror of
https://github.com/jht5945/rust_util.git
synced 2025-12-27 15:40:03 +08:00
add util_cmd.rs, util_file.rs
This commit is contained in:
26
src/util_cmd.rs
Normal file
26
src/util_cmd.rs
Normal file
@@ -0,0 +1,26 @@
|
||||
|
||||
use std::{
|
||||
io::{self, Error, ErrorKind},
|
||||
process::Command,
|
||||
};
|
||||
|
||||
pub fn run_command_and_wait(cmd: &mut Command) -> io::Result<()> {
|
||||
cmd.spawn()?.wait()?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn extract_package_and_wait(dir: &str, file_name: &str) -> io::Result<()> {
|
||||
let mut cmd: Command;
|
||||
if file_name.ends_with(".zip") {
|
||||
cmd = Command::new("unzip");
|
||||
} else if file_name.ends_with(".tar.gz") {
|
||||
cmd = Command::new("tar");
|
||||
cmd.arg("-xzvf");
|
||||
} else {
|
||||
let m: &str = &format!("Unknown file type: {}", file_name);
|
||||
return Err(Error::new(ErrorKind::Other, m));
|
||||
}
|
||||
cmd.arg(file_name).current_dir(dir);
|
||||
run_command_and_wait(&mut cmd)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user