summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorLuke Shumaker <lukeshu@lukeshu.com>2018-04-24 16:07:25 -0400
committerLuke Shumaker <lukeshu@lukeshu.com>2018-04-24 16:48:47 -0400
commitde6ec125dcadab66602a62cdaac440c2d9c5e3ba (patch)
treecab7306d08d565cb98ade5f0a3bcb7a6c9725077 /src
parentd106e7d658d1295cc2555b1e3b7d01dba40ddb21 (diff)
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
Diffstat (limited to 'src')
-rw-r--r--src/lib/conf.sh.3.ronn16
-rw-r--r--src/lib/conf.sh.in36
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` <SLUG>.conf <VARS>...:
+ * `load_conf` {<SLUG>.conf|<ABSPATH>} <VARS>...:
Loads the configuration files for <SLUG>, loading the files in
the correct order, and checking the appropriate environmental
- variables. If any <VARS> 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 <SLUG>, and returns with a non-zero
- status.
+ variables. Alternatively, instead of giving a <SLUG>, 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 <VARS> 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` <SLUG> <VAR> <DEFAULT>:
If <VAR> is set in the configuration for <SLUG>, 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