Skip to content

Commit

Permalink
Exit if pre-up or pre-down hook exits non-zero
Browse files Browse the repository at this point in the history
This allows scripts to bail early if a check fails.
  • Loading branch information
pbrisbin authored and mike-burns committed Dec 30, 2022
1 parent 832e70b commit 453e88b
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 0 deletions.
2 changes: 2 additions & 0 deletions Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,11 @@ TESTS = \
test/rcup-symlink-existing.t \
test/rcup-usage.t \
test/rcdn-hooks.t \
test/rcdn-hooks-failure.t \
test/rcdn-hooks-run-in-situ.t \
test/rcdn-hooks-run-in-order.t \
test/rcup-hooks.t \
test/rcup-hooks-failure.t \
test/rcup-hooks-run-in-situ.t \
test/rcup-hooks-run-in-order.t \
test/rcup-spaces.t
Expand Down
5 changes: 5 additions & 0 deletions bin/rcdn.in
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,11 @@ handle_command_line "$@"
: ${DOTFILES_DIRS:=$DOTFILES_DIRS $DEFAULT_DOTFILES_DIR}

run_hooks pre down
pre_dn_ret=$?

if [ "$pre_dn_ret" -ne 0 ]; then
exit "$pre_dn_ret"
fi

dests_and_srcs="$(eval "lsrc $LS_ARGS")"

Expand Down
5 changes: 5 additions & 0 deletions bin/rcup.in
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,11 @@ handle_command_line "$@"
: ${DOTFILES_DIRS:=$DOTFILES_DIRS $DEFAULT_DOTFILES_DIR}

run_hooks pre up
pre_up_ret=$?

if [ "$pre_up_ret" -ne 0 ]; then
exit "$pre_up_ret"
fi

dests_and_srcs="$(eval "lsrc $LS_ARGS")"

Expand Down
6 changes: 6 additions & 0 deletions share/rcm.sh.in
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,12 @@ run_hooks() {
find "$hook_file" -type f \( \( -user $LOGNAME -perm -100 \) -o -perm -001 \) \
| sort | while read file; do
sh -c 'cd -- "`dirname $1`" && ./"`basename $1`"' arg0 "$file"
hook_ret=$?

if [ "$hook_ret" -ne 0 ]; then
echo "$when-$direction hook $file exited non-zero ($hook_ret)" >&2
exit $hook_ret # NB. this only exits the while-read; caller must still check and exit
fi
done
else
$DEBUG "no $when-$direction hook present for $dotfiles_dir, skipping"
Expand Down
16 changes: 16 additions & 0 deletions test/rcdn-hooks-failure.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
$ . "$TESTDIR/helper.sh"

Pre-down hooks failing should prevent rcdn continuing

$ mkdir -p .dotfiles/hooks/pre-down
> printf "#!/bin/sh\necho '1'\nexit 7\n" > .dotfiles/hooks/pre-down/00-test.sh
> printf "#!/bin/sh\necho '2'\n" > .dotfiles/hooks/pre-down/01-test.sh

$ chmod +x .dotfiles/hooks/pre-down/00-test.sh
> chmod +x .dotfiles/hooks/pre-down/01-test.sh

$ rcdn
1
pre-down hook */.dotfiles/hooks/pre-down/00-test.sh exited non-zero (7) (glob)
[7]

15 changes: 15 additions & 0 deletions test/rcup-hooks-failure.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
$ . "$TESTDIR/helper.sh"

Pre-up hooks failing should prevent rcup continuing

$ mkdir -p .dotfiles/hooks/pre-up
> printf "#!/bin/sh\necho '1'\nexit 7\n" > .dotfiles/hooks/pre-up/00-test.sh
> printf "#!/bin/sh\necho '2'\n" > .dotfiles/hooks/pre-up/01-test.sh

$ chmod +x .dotfiles/hooks/pre-up/00-test.sh
> chmod +x .dotfiles/hooks/pre-up/01-test.sh

$ rcup
1
pre-up hook */.dotfiles/hooks/pre-up/00-test.sh exited non-zero (7) (glob)
[7]

0 comments on commit 453e88b

Please sign in to comment.