Tie "post" hooks to yadm exits

There are many cases where yadm may exit early (particularly when
encountering an error).
This commit is contained in:
Tim Byrne 2017-07-03 16:25:23 -05:00
parent 880964e2b2
commit 35743e3711
No known key found for this signature in database
GPG Key ID: 14DB4FC2465A4B12
1 changed files with 22 additions and 12 deletions

34
yadm
View File

@ -30,6 +30,8 @@ YADM_ENCRYPT="encrypt"
YADM_ARCHIVE="files.gpg" YADM_ARCHIVE="files.gpg"
YADM_BOOTSTRAP="bootstrap" YADM_BOOTSTRAP="bootstrap"
HOOK_COMMAND=""
GPG_PROGRAM="gpg" GPG_PROGRAM="gpg"
GIT_PROGRAM="git" GIT_PROGRAM="git"
LS_PROGRAM="/bin/ls" LS_PROGRAM="/bin/ls"
@ -97,14 +99,14 @@ function main() {
shift shift
done done
[ ! -d "$YADM_WORK" ] && error_out "Work tree does not exist: [$YADM_WORK]" [ ! -d "$YADM_WORK" ] && error_out "Work tree does not exist: [$YADM_WORK]"
invoke_hook "pre_$YADM_COMMAND" HOOK_COMMAND="$YADM_COMMAND"
invoke_hook "pre"
$YADM_COMMAND "${YADM_ARGS[@]}" $YADM_COMMAND "${YADM_ARGS[@]}"
invoke_hook "post_$YADM_COMMAND"
else else
#; any other commands are simply passed through to git #; any other commands are simply passed through to git
invoke_hook "pre_$1" HOOK_COMMAND="$1"
invoke_hook "pre"
git_command "$@" git_command "$@"
invoke_hook "post_$1"
retval="$?" retval="$?"
fi fi
@ -113,7 +115,7 @@ function main() {
auto_perms auto_perms
auto_bootstrap auto_bootstrap
exit $retval exit_with_hook $retval
} }
@ -546,7 +548,7 @@ Files:
Use "man yadm" for complete documentation. Use "man yadm" for complete documentation.
EOF EOF
exit 1 exit_with_hook 1
} }
@ -698,7 +700,7 @@ function perms() {
function version() { function version() {
echo "yadm $VERSION" echo "yadm $VERSION"
exit 0 exit_with_hook 0
} }
@ -853,17 +855,25 @@ function debug() {
function error_out() { function error_out() {
echo -e "ERROR: $*" echo -e "ERROR: $*"
exit 1 exit_with_hook 1
}
function exit_with_hook() {
invoke_hook "post"
exit "$1"
} }
function invoke_hook() { function invoke_hook() {
hook_name="$1" mode="$1"
hook_command="$YADM_DIR/hooks/${mode}_$HOOK_COMMAND"
if [ -x "$YADM_DIR/hooks/$hook_name" ] ; then if [ -x "$hook_command" ] ; then
debug "Invoking hook: $hook_name" debug "Invoking hook: $hook_command"
"$YADM_DIR/hooks/$hook_name" "$hook_command"
fi fi
} }