diff options
Diffstat (limited to 'src/lib')
-rw-r--r-- | src/lib/blacklist.sh | 3 | ||||
-rw-r--r-- | src/lib/conf.sh.3.ronn | 11 | ||||
-rw-r--r-- | src/lib/conf.sh.in | 50 |
3 files changed, 27 insertions, 37 deletions
diff --git a/src/lib/blacklist.sh b/src/lib/blacklist.sh index 9597bbc..fc07815 100644 --- a/src/lib/blacklist.sh +++ b/src/lib/blacklist.sh @@ -43,8 +43,7 @@ blacklist-cat() { # Updates (or creates) the cached copy of the blacklist. blacklist-update() ( . "$(librelib messages)" - load_files libretools || return 1 - check_vars libretools BLACKLIST || return 1 + load_conf libretools.conf BLACKLIST || return local remote_blacklist="$BLACKLIST" local local_blacklist="$XDG_CACHE_HOME/libretools/blacklist.txt" diff --git a/src/lib/conf.sh.3.ronn b/src/lib/conf.sh.3.ronn index 0974bdb..c17a3ee 100644 --- a/src/lib/conf.sh.3.ronn +++ b/src/lib/conf.sh.3.ronn @@ -47,14 +47,13 @@ configuration files; that is the basename of the files. For example, The routines you will likely actually use are: - * `load_files` <SLUG>: + * `load_conf` <SLUG>.conf <VARS>...: Loads the configuration files for <SLUG>, loading the files in the correct order, and checking the appropriate environmental - variables. - * `check_vars` <SLUG> <VARS>...: - Checks to see if all of <VARS> are defined. If any of them - aren't, it prints a message telling the user to configure them in - the configuration files for <SLUG>, and returns with a non-zero + 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. * `get_var` <SLUG> <VAR> <DEFAULT>: If <VAR> is set in the configuration for <SLUG>, print its diff --git a/src/lib/conf.sh.in b/src/lib/conf.sh.in index 18760e1..7d1646b 100644 --- a/src/lib/conf.sh.in +++ b/src/lib/conf.sh.in @@ -104,22 +104,26 @@ list_envvars() { # High-level generic functions ################################################# -# Usage: load_files $slug -# Loads the configuration files for $slug in the proper order. -load_files() { - local slug=$1 - local var - local file +# Usage: load_conf $slug.conf [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 + local slug=${1%.conf} + shift + + local var file # Save the existing versions at _VARNAME while read -r var; do - [[ -n ${!var:-} ]] && eval "_$var=\${$var}" + [[ -n ${!var:-} ]] && eval "local _$var=\${$var}" done < <(list_envvars "$slug") # Load the files while read -r file; do if [[ -r $file ]]; then - . "$file" || return 1 + . "$file" || return 6 fi done < <(list_files "$slug") @@ -127,33 +131,21 @@ load_files() { while read -r var; do eval "$var=\${_$var:-\${$var:-}}" done < <(list_envvars "$slug") -} - -# Usage: check_vars $slug VAR1 VAR2... -# Check whether the variables listed are properly set. -# If not, it prints a message saying to set them in the configuration file(s) -# for $slug. -check_vars() { - local slug=$1; shift - local ret=0 - - local VAR - for VAR in "$@"; do - if [[ -z ${!VAR:-} ]]; then + # Verify that the variables we need were set + declare -i ret=0 + for var in "$@"; do + if [[ -z ${!var:-} ]]; then if [[ $(list_files "$slug"|wc -l) -gt 1 ]]; then - libremessages _l print "Configure '%s' in one of:" "$VAR" + libremessages _l print "Configure '%s' in one of:" "$var" list_files "$slug" | sed 's/./ -> &/' else - libremessages _l print "Configure '%s' in '%s'" "$VAR" "$(list_files "$slug")" + libremessages _l print "Configure '%s' in '%s'" "$var" "$(list_files "$slug")" fi - ret=1 + ret=6 fi done >&2 - - if [[ $ret != 0 ]]; then - return 1 - fi + return $ret } # Usage: get_var <slug> <var_name> <default_value> @@ -163,7 +155,7 @@ get_var() ( local slug=$1 local setting=$2 local default=$3 - load_files "$slug" + load_conf "$slug.conf" printf '%s' "${!setting:-${default}}" ) |