feat: init commit
This commit is contained in:
48
src/main.rs
Normal file
48
src/main.rs
Normal file
@@ -0,0 +1,48 @@
|
||||
#[macro_use]
|
||||
extern crate rust_util;
|
||||
|
||||
use notify::{Watcher, RecursiveMode, RawEvent, raw_watcher};
|
||||
use std::sync::mpsc::channel;
|
||||
use clap::{App, Arg};
|
||||
use std::process::Command;
|
||||
|
||||
fn main() {
|
||||
let matches = App::new("Watch and action")
|
||||
.version("1.0")
|
||||
.author("Hatter Jiang <jht5945@gmail.com>")
|
||||
.about("Watch and action")
|
||||
.arg(Arg::with_name("dir")
|
||||
.short("d")
|
||||
.long("dir")
|
||||
.default_value(".")
|
||||
.help("Watch dir")
|
||||
.takes_value(true))
|
||||
.arg(Arg::with_name("action")
|
||||
.short("a")
|
||||
.long("action")
|
||||
.required(true)
|
||||
.help("Action")
|
||||
.takes_value(true))
|
||||
.get_matches();
|
||||
|
||||
let dir = matches.value_of("dir").unwrap();
|
||||
let action = matches.value_of("action").unwrap();
|
||||
let (tx, rx) = channel();
|
||||
let mut watcher = raw_watcher(tx).unwrap();
|
||||
|
||||
information!("Watch dir: {}, and then action: {}", dir, action);
|
||||
watcher.watch(dir, RecursiveMode::Recursive).unwrap();
|
||||
loop {
|
||||
match rx.recv() {
|
||||
Ok(RawEvent { path: Some(path), op: Ok(op), cookie }) => {
|
||||
success!("Success: {:?} {:?} ({:?})", op, path, cookie);
|
||||
match rust_util::util_cmd::run_command_and_wait(&mut Command::new(action)) {
|
||||
Ok(exit_status) => success!("Run action success: {}", exit_status),
|
||||
Err(e) => failure!("Run action: {}, failed: {}", action, e),
|
||||
}
|
||||
}
|
||||
Ok(event) => information!("Broken event: {:?}", event),
|
||||
Err(e) => failure!("Watch error: {:?}", e),
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user