mirror of
https://github.com/zaphar/runwhen.git
synced 2025-07-22 20:39:49 -04:00
Add a negation flag for the success command.
Modifies the trigger to run when the command fails instead of when it succeeds.
This commit is contained in:
parent
07120d141f
commit
b2b842d12f
@ -67,6 +67,7 @@ fn is_cmd_success(cmd: &str, env: Option<Vec<&str>>) -> bool {
|
||||
|
||||
pub struct ExecProcess<'a> {
|
||||
test_cmd: &'a str,
|
||||
negate: bool,
|
||||
cmd: &'a str,
|
||||
env: Option<Vec<&'a str>>,
|
||||
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<Vec<&'a str>>,
|
||||
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)
|
||||
}
|
||||
|
@ -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)
|
||||
|
Loading…
x
Reference in New Issue
Block a user