diff --git a/src/exec.rs b/src/exec.rs index bbd609c..6d8aca4 100644 --- a/src/exec.rs +++ b/src/exec.rs @@ -67,6 +67,7 @@ fn is_cmd_success(cmd: &str, env: Option>) -> bool { pub struct ExecProcess<'a> { test_cmd: &'a str, + negate: bool, cmd: &'a str, env: Option>, poll: Duration, @@ -75,10 +76,12 @@ pub struct ExecProcess<'a> { impl<'a> ExecProcess<'a> { pub fn new(test_cmd: &'a str, cmd: &'a str, + negate: bool, env: Option>, poll: Duration) -> ExecProcess<'a> { ExecProcess { test_cmd: test_cmd, + negate: negate, cmd: cmd, env: env, poll: poll, @@ -90,7 +93,8 @@ impl<'a> Process for ExecProcess<'a> { fn run(&self) -> Result<(), CommandError> { loop { // TODO(jwall): Should we set the environment the same as the other command? - if is_cmd_success(self.test_cmd, None) { + let test_result = is_cmd_success(self.test_cmd, None); + if (test_result && !self.negate) || (!test_result && self.negate) { if let Err(err) = run_cmd(self.cmd, &self.env) { println!("{:?}", err) } diff --git a/src/main.rs b/src/main.rs index 6015f2a..ace66f5 100644 --- a/src/main.rs +++ b/src/main.rs @@ -62,6 +62,8 @@ fn do_flags<'a>() -> clap::ArgMatches<'a> { (about: "Trigger that fires if a command runs successful.") (@arg ifcmd: --if +required +takes_value "The command to test for successful exit from") + (@arg not: --not + "Negate the test command so we run on failure instead of success.") (@arg wait: --poll +takes_value "How frequently to test command (default 5s)") ) @@ -122,9 +124,10 @@ fn main() { } else if let Some(matches) = app.subcommand_matches("success") { // unwrap because this is required. let ifcmd = matches.value_of("ifcmd").expect("ifcmd flag is required"); + let negate = matches.is_present("not"); let dur = humantime::parse_duration(matches.value_of("poll").unwrap_or("5s")); process = match dur { - Ok(duration) => Some(Box::new(ExecProcess::new(ifcmd, cmd, maybe_env, duration))), + Ok(duration) => Some(Box::new(ExecProcess::new(ifcmd, cmd, negate, maybe_env, duration))), Err(msg) => { println!("Malformed poll {:?}", msg); process::exit(1)