pinch/hooks/_init.sh

73 lines
1.2 KiB
Bash
Raw Normal View History

2022-04-18 17:50:40 +02:00
#!/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 $@"
}
2022-04-18 18:33:08 +02:00
else
2022-04-18 17:50:40 +02:00
pdebug() {
:
}
fi
pinfo() {
2022-04-18 18:33:08 +02:00
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() {
perror "Error during execution of $EXEC_NAME."
2022-04-18 17:50:40 +02:00
}
2022-04-18 18:33:08 +02:00
export -f pinfo pwarn perror
2022-04-18 17:50:40 +02:00
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
2022-04-18 17:50:40 +02:00
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"
2022-04-18 18:33:08 +02:00
export EXEC_NAME="custom::$(basename $0)::$hook"
2022-04-18 17:50:40 +02:00
$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"
2022-04-18 18:33:08 +02:00
export EXEC_NAME="$(basename $0)::$file"
2022-04-18 17:50:40 +02:00
$1/$file $HOOK_ARGS
fi
done
}
pdebug "Hook type: $(basename $0)"