diff options
Diffstat (limited to 'lib/util/config.bash')
-rw-r--r-- | lib/util/config.bash | 34 |
1 files changed, 30 insertions, 4 deletions
diff --git a/lib/util/config.bash b/lib/util/config.bash index 4727706..ff20bd0 100644 --- a/lib/util/config.bash +++ b/lib/util/config.bash @@ -17,7 +17,11 @@ config_subkeys() { config_for() { local host=$1 ; shift local key=$1 ; shift - local default=$1 ; shift + + # Use a variable definition test to define default, in order to ensure + # it is _not_ defined if no argument for it was passed, but _is_ + # defined even if an empty string was passed. + test -v 1 && { local default=$1 ; shift ; } local default local file @@ -51,7 +55,26 @@ config_for() { [[ ! -f $file ]] && continue - value="$(awk -F= '$1 == "'"$key"'" { print $NF }' "$file")" + # Check if the lookup is a reference variable, defined by using + # &= instead of just a single = as seperator. If this exists, + # do a new config_for lookup, this time using the value as the + # key for the new lookup. + value="$(awk -F= '$1 == "'"$key"'&" { print $0 }' "$file" | cut -d'=' -f 2-)" + + if [[ -n $value ]] + then + debug "bashtard/config_for" "Found reference for $key to $value in $file" + + printf "%s" "$(config_for "$host" "$value")" + return + fi + + # Use awk to find the right line, then use cut to get the + # actual value. Cutting it out with awk _is_ possible, but + # comes with whitespace issues or having to deal with values + # containing the seperator (=), so using cut is much simpler + # and easier to understand. + value="$(awk -F= '$1 == "'"$key"'" { print $0 }' "$file" | cut -d'=' -f 2-)" if [[ -n $value ]] then @@ -62,8 +85,8 @@ config_for() { fi done - # Return default value - if [[ -n $default ]] + # Return default value, if one has been defined + if test -v default then printf "%s" "$default" return @@ -115,6 +138,9 @@ config_subkeys_for() { subkey="$(awk -F. '{ print $1 }' <<< "${result#"$key."}")" + # Remove trailing ampersand if it exists + [[ "${subkey: -1}" == "&" ]] && subkey="${subkey:0:-1}" + debug "bashtard/config_subkeys" "Found '$subkey' as subkey of '$key' through '$result'" results+=("$subkey") done < <(grep "^$key\." "$file" | awk -F= '{ print $1 }') |