aboutsummaryrefslogtreecommitdiff
path: root/lib/util/config.bash
diff options
context:
space:
mode:
Diffstat (limited to 'lib/util/config.bash')
-rw-r--r--lib/util/config.bash45
1 files changed, 37 insertions, 8 deletions
diff --git a/lib/util/config.bash b/lib/util/config.bash
index 78841e1..ff20bd0 100644
--- a/lib/util/config.bash
+++ b/lib/util/config.bash
@@ -1,6 +1,6 @@
#!/usr/bin/env bash
-# SPDX-FileCopyrightText: 2022 Patrick Spek <p.spek@tyil.nl>
+# SPDX-FileCopyrightText: 2023 Patrick Spek <p.spek@tyil.nl>
#
# SPDX-License-Identifier: AGPL-3.0-or-later
@@ -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
@@ -34,8 +38,8 @@ config_for() {
then
debug "bashtard/config_for" "BASHTARD_PLAYBOOK=$BASHTARD_PLAYBOOK, adding etc entries"
files+=(
- "$BASHTARD_ETCDIR/playbooks.d/$BASHTARD_PLAYBOOK/etc/os.d/${BASHTARD_PLATFORM[key]}"
- "$BASHTARD_ETCDIR/playbooks.d/$BASHTARD_PLAYBOOK/etc/defaults"
+ "$(playbook_path "base")/etc/os.d/${BASHTARD_PLATFORM[key]}"
+ "$(playbook_path "base")/etc/defaults"
)
fi
@@ -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
@@ -111,7 +134,13 @@ config_subkeys_for() {
while read -r result
do
- local subkey="$(awk -F. '{ print $1 }' <<< "${result#"$key."}")"
+ local subkey
+
+ 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 }')