Support the `unless` parameter on a task to run a little script for idempotency

This commit is contained in:
R Tyler Croy 2020-12-31 22:42:23 -08:00
parent bc4396a0de
commit d605bc4229
2 changed files with 20 additions and 4 deletions

View File

@ -48,6 +48,9 @@ definitions.
| `provides`
| A relative or absolute path to a file that the task provides. If the file exists, then the task will be skipped.
| `unless`
| A script snippet which can determine whether the task should execute. A non-zero exit status causes the task to execute.
|===
.echo.ztask

View File

@ -113,13 +113,26 @@ impl Transport for Ssh {
}
}
if let Some(unless) = &command.parameters.get("unless") {
debug!("An `unless` parameter was given, running {}", unless);
}
let remote_script = "._zap_command";
let args_file = "._zap_args.json";
if let Some(unless) = &command.parameters.get("unless") {
debug!("An `unless` parameter was given, running {}", unless);
if self.send_bytes(Path::new(remote_script), &unless.as_bytes().to_vec(), 0o700) {
let mut channel = self.session.channel_session().unwrap();
channel.exec(&format!("./{}", remote_script));
let mut s = String::new();
channel.read_to_string(&mut s).unwrap();
print!("{}", s);
channel.wait_close().expect("Failed to close the channel");
let exit = channel.exit_status().unwrap();
if exit == 0 {
debug!("Unless script returned success, so bailing out early");
return 0;
}
}
}
if let Some(script) = command.task.script.as_bytes(Some(&command.parameters)) {
if dry_run {
println!("{}", "Dry-run\n----".yellow());