Rex
Rex copied to clipboard
Execute command before moving file into its final location
User story
As a rex user, I would like to execute some commands before a file is moved to its final location, so I can to validate file content, or show diff before the changes are applied.
Describe the solution you'd like
Currently we can use file hooks for similar purposes, but those hook apply for all file
commands globally. We could either allow general file hooks on a case-by-case basis:
file '/etc/ssh/sshd_config',
source => 'files/etc/ssh/sshd_config',
before_change => sub { run 'sshd -t <%= $filename %>'}; # template format
or introduce a new, purpose-made validate
option:
file '/etc/ssh/sshd_config',
source => 'files/etc/ssh/sshd_config',
validate => sub { run 'sshd -t %s'}; # string placeholder format
It would only copy the temporary file to its final location if this hook returns success.
Describe alternatives you've considered
Filename matching is possible inside the global hooks too, but it feels like the configuration for that would be far away from its context.
Additional context
Ansible has a similar validate
logic too, but I think I would prefer a more generic solution, especially given we already have various file hooks in place, it's just hard to configure those on a per-file basis.