skonfig / base

explorer and types for general use
GNU General Public License v3.0
4 stars 4 forks source link

__file: --prechange parameter? #78

Open sideeffect42 opened 1 year ago

sideeffect42 commented 1 year ago

Sometimes, when I use __file to put config files onto the target, I want to run a tool/script to check if the contents of the file are valid before reloading the service using the config and possibly breaking it.

Currently, it is possible to trigger a service reload conditionally using --onchange 'checkconf && service reload'. However, checkconf is only executed after the config file was copied to the target. The service will not be reloaded immediately, but the service will likely fail to come up on the next reboot at the latest.

So my idea would be to add a --prechange parameter to __file which, if set, makes the __file type only copy the file to the destination if the command given in --prechange exits with status 0.

Example use case:

__file /etc/service/config \
    --owner 0 --group 0 --mode 0644 \
    --source "${__files:?}/someconfig" \
    --prechange "checkconf-cmd %s" \
    --onchange "/etc/init.d/service reload"

I'm not sure about %s yet. Complex commands may have to use the path multiple times, or some services might not have the possibility to use an arbitrary path to check. Maybe a "try or undo" logic would need to be implemented.