1
0
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:
2019-08-08 00:26:30 +08:00
parent 390a62d88b
commit f5fe205b78
3 changed files with 125 additions and 110 deletions

26
src/util_cmd.rs Normal file
View 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)
}