Finish rest of initialization framework

This commit is contained in:
Nick Chambers 2021-07-04 04:57:52 -05:00 committed by Nick Chambers
parent 729f2fa0d1
commit 2f01b5c44c
1 changed files with 76 additions and 24 deletions

100
rowbot
View File

@ -6,26 +6,6 @@
shopt -s dotglob extglob lastpipe nullglob
###
# utility and helper functions
###
has() {
if (( $# )); then
hash "$1" 2>/dev/null
else
return 1
fi
}
is_parent() {
(( BASHPID == $$ ))
}
is_reloaded() {
[[ $RELOADED = yes ]] || (( LORE_LIVES > 1 ))
}
###
# configure rowbot
###
@ -78,9 +58,13 @@ done
# apply custom configuration files
for file do
# These files (if any) are provided dynamically at run-time.
# shellcheck disable=SC1090
. "$file" # ha, dot file
if [[ -f $file ]]; then
# These files (if any) are provided dynamically at run-time.
# shellcheck disable=SC1090
. "$file" # ha, dot file
else
die "could not locate config file %s" "$file"
fi
done
for setting in "${!config[@]}"; do
@ -110,10 +94,78 @@ if [[ -z ${config[port]} ]]; then
fi
fi
# apply any reloaded values
for setting in "${!config[@]}"; do
env_name=${setting//-/_} env_name=${env_name^^}
declare -n env_var=$env_name
if [[ -v $env_name ]]; then
# This variable is only used for assignment.
# shellcheck disable=SC2034
config[$setting]=$env_var
fi
done
# cleanup
unset key file setting setting_var
unset key file setting setting_var env_name env_var
###
# utility and helper functions
###
die() {
# error "$@"
exit "${STATUS:-42}"
}
is_reloaded() {
[[ $RELOADED = yes ]] || (( LORE_LIVES > 1 ))
}
run_callbacks() {
if (( ! $# )); then
return 1
fi
local filter=$1
shift
while IFS= read -r; do
"$REPLY" "$@"
done < <(compgen -A function "$filter")
return 0
}
###
# live code reloader
###
reload() {
run_callbacks on_before_reload_
exec "${cmd_line[@]}"
}
hup_reload() {
reload
}
trap hup_reload HUP
on_init_debug() {
declare -p config
}
###
# initialization sequence
###
run_callbacks on_init_
if is_reloaded; then
run_callbacks on_after_reload_
else
run_callbacks on_first_
fi