diff --git a/src/cli.rs b/src/cli.rs index beaf9ee..8340a03 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -14,6 +14,7 @@ pub struct Args { pub debug: bool, pub run_initially: bool, pub no_vcs_ignore: bool, + pub once: bool, pub poll: bool, pub poll_interval: u32, } @@ -86,6 +87,9 @@ pub fn get_args() -> Args { .help("Send SIGKILL to child processes") .short("k") .long("kill")) + .arg(Arg::with_name("once") + .short("1") + .hidden(true)) .get_matches(); let cmd = values_t!(args.values_of("command"), String).unwrap().join(" "); @@ -125,6 +129,7 @@ pub fn get_args() -> Args { debug: args.is_present("debug"), run_initially: !args.is_present("postpone"), no_vcs_ignore: args.is_present("no-vcs-ignore"), + once: args.is_present("once"), poll: args.occurrences_of("poll") > 0, poll_interval: poll_interval, } diff --git a/src/main.rs b/src/main.rs index d3a5ed0..8ed8a7a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -108,7 +108,7 @@ fn main() { } // Start child process initially, if necessary - if args.run_initially { + if args.run_initially && !args.once { if args.clear_screen { cli::clear_screen(); } @@ -119,29 +119,13 @@ fn main() { loop { debug!("Waiting for filesystem activity"); - let paths = wait(&rx, &filter); + let paths = wait_fs(&rx, &filter); if let Some(path) = paths.get(0) { debug!("Path updated: {:?}", path); } // Wait for current child process to exit - { - let guard = child_process.read().unwrap(); - - if let Some(ref child) = *guard { - if args.restart { - debug!("Stopping child process"); - if kill { - child.kill(); - } else { - child.terminate(); - } - } - - debug!("Waiting for process to exit..."); - child.wait(); - } - } + wait_process(&child_process, kill, args.restart); // Launch child process if args.clear_screen { @@ -153,10 +137,16 @@ fn main() { let mut guard = child_process.write().unwrap(); *guard = Some(process::spawn(&args.cmd, paths)); } + + // Handle once option for integration testing + if args.once { + wait_process(&child_process, kill, false); + break; + } } } -fn wait(rx: &Receiver, filter: &NotificationFilter) -> Vec { +fn wait_fs(rx: &Receiver, filter: &NotificationFilter) -> Vec { let mut paths = vec![]; let mut cache = HashMap::new(); @@ -199,3 +189,21 @@ fn wait(rx: &Receiver, filter: &NotificationFilter) -> Vec { paths } + +fn wait_process(process: &RwLock>, kill: bool, restart: bool) { + let guard = process.read().unwrap(); + + if let Some(ref child) = *guard { + if restart { + debug!("Stopping child process"); + if kill { + child.kill(); + } else { + child.terminate(); + } + } + + debug!("Waiting for process to exit..."); + child.wait(); + } +}