pinch/hooks/_init.sh
2022-12-28 11:04:44 +01:00

77 lines
1.3 KiB
Bash
Executable file

#!/usr/bin/env bash
HOOK_ARGS=$@
HOOK_PATH=$(realpath $0)
HOOK_PATH=${HOOK_PATH%/*}
export HOOK_PATH
if [[ $PINCH_DEBUG -eq 1 ]]; then
pdebug() {
echo -e "\033[33m(DEBUG)\033[0m $@"
}
else
pdebug() {
:
}
fi
pinfo() {
echo -e "\033[1;32m=>\033[0m $@"
}
pwarn() {
echo -e "\033[1;33m(warning) =>\033[0m $@"
}
perror() {
echo -e "\033[1;31m(error) =>\033[0m $@"
}
err_trap() {
LAST=$?
if [[ $LAST -lt 0 ]]; then
perror "Error during execution of $EXEC_NAME."
elif [[ $LAST -gt 0 ]]; then
pwarn "Hook $EXEC_NAME finished unsuccessfully."
fi
}
export -f pinfo pwarn perror
export -f pdebug
export -f err_trap
trap err_trap ERR
pdebug "Retrieving configuration..."
set -a
for cff in $(ls $HOOK_PATH/../config.d/)
do
pdebug "Sourcing $cff"
source $HOOK_PATH/../config.d/$cff
done
set +a
exec_custom() {
local hooks_path=$HOOK_PATH/../custom.d/$1
for hook in $(ls $hooks_path); do
if [ -x $hooks_path/$hook ]; then
pdebug "Executing custom hook $hooks_path/$hook"
export EXEC_NAME="custom::$(basename $0)::$hook"
$hooks_path/$hook $HOOK_ARGS
fi
done
}
exec_all() {
for file in $(ls $1 | sort); do
if [ -x $1/$file ]; then
pdebug "Executing hook $1/$file"
export EXEC_NAME="$(basename $0)::$file"
$1/$file $HOOK_ARGS
fi
done
}
pdebug "Hook type: $(basename $0)"