From de6ec125dcadab66602a62cdaac440c2d9c5e3ba Mon Sep 17 00:00:00 2001 From: Luke Shumaker Date: Tue, 24 Apr 2018 16:07:25 -0400 Subject: conf.sh: Allow load_conf to take an absolute filepath This is useful in dbscripts. This also has a couple of happy side-effects in the implementation details - list_files and list_envvars are now only called once each - we now do save readarray-based splitting of list_files and list_envvars output --- src/lib/conf.sh.3.ronn | 16 ++++++++++------ src/lib/conf.sh.in | 36 ++++++++++++++++++++++-------------- 2 files changed, 32 insertions(+), 20 deletions(-) diff --git a/src/lib/conf.sh.3.ronn b/src/lib/conf.sh.3.ronn index c17a3ee..fee0529 100644 --- a/src/lib/conf.sh.3.ronn +++ b/src/lib/conf.sh.3.ronn @@ -47,14 +47,18 @@ configuration files; that is the basename of the files. For example, The routines you will likely actually use are: - * `load_conf` .conf ...: + * `load_conf` {.conf|} ...: Loads the configuration files for , loading the files in the correct order, and checking the appropriate environmental - variables. If any are listed, check to make sure that - each of them are defined. If any of them are not defined, it - prints a message telling the user to configure them in the - configuration files for , and returns with a non-zero - status. + variables. Alternatively, instead of giving a , you may + give an absolute filepath to a configuration file (bypassing + `list_files`, see below) which will be loaded with no + environmental overrides (bypassing `list_envvars`, see below). + + If any are listed, check to make sure that each of them + are defined. If any of them are not defined, it prints a message + telling the user to configure them in the appropriate files, and + returns with a non-zero status. * `get_var` : If is set in the configuration for , print its value, considering environmental variables. If it is not set, diff --git a/src/lib/conf.sh.in b/src/lib/conf.sh.in index 15666a6..0ff88f9 100644 --- a/src/lib/conf.sh.in +++ b/src/lib/conf.sh.in @@ -107,43 +107,51 @@ list_envvars() { # High-level generic functions ################################################# -# Usage: load_conf $slug.conf [VAR1 VAR2...] +# Usage: load_conf {$slug.conf|$abspath} [VAR1 VAR2...] # # Loads the configuration files for $slug in the proper order, and # optionally verify that certain variables are set. load_conf() { - [[ "$1" = *.conf ]] || libremessages panic || exit 1 # $EXIT_FAILURE - local slug=${1%.conf} - shift - + [[ $1 = /* || $1 = *.conf ]] || libremessages panic || exit 1 # $EXIT_FAILURE + local files envvars + if [[ $1 = /* ]]; then + files=("$1") + envvars=() + shift + else + local slug=${1%.conf} + shift + readarray -t files < <(list_files "$slug") + readarray -t envvars < <(list_envvars "$slug") + fi local var file # Save the existing versions at _VARNAME - while read -r var; do + for var in "${envvars[@]}"; do [[ -n ${!var:-} ]] && eval "local _$var=\${$var}" - done < <(list_envvars "$slug") + done # Load the files - while read -r file; do + for file in "${files[@]}"; do if [[ -r $file ]]; then . "$file" || return 6 # $EXIT_NOTCONFIGURED fi - done < <(list_files "$slug") + done # Restore the _SAVED versions - while read -r var; do + for var in "${envvars[@]}"; do eval "$var=\${_$var:-\${$var:-}}" - done < <(list_envvars "$slug") + done # Verify that the variables we need were set declare -i ret=0 # $EXIT_SUCCESS for var in "$@"; do if [[ -z ${!var:-} ]]; then - if [[ $(list_files "$slug"|wc -l) -gt 1 ]]; then + if [[ ${#files[@]} -gt 1 ]]; then libremessages _l print "Configure '%s' in one of:" "$var" - list_files "$slug" | sed 's/./ -> &/' + printf ' -> %s\n' "${files[@]}" else - libremessages _l print "Configure '%s' in '%s'" "$var" "$(list_files "$slug")" + libremessages _l print "Configure '%s' in '%s'" "$var" "${files[0]}" fi ret=6 # $EXIT_NOTCONFIGURED fi -- cgit v1.2.2