From 8db5cd6989b53c929b4ab628083ac48ae592ca25 Mon Sep 17 00:00:00 2001 From: Patrick Spek Date: Tue, 5 Mar 2024 08:28:40 +0100 Subject: Implement variable references --- CHANGELOG.md | 6 ++++++ lib/util/config.bash | 14 ++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 407e62d..e2f9903 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [UNRELEASED] +### Added + +- Configuration variables can be assigned values of other variables with the + `&=` assignment. This allows a single value to be re-used dynamically, rather + than having to explicitly set the same value several times. + ### Fixed - Passing an empty string as default value to `config` should now properly diff --git a/lib/util/config.bash b/lib/util/config.bash index 9817cf3..703dc4d 100644 --- a/lib/util/config.bash +++ b/lib/util/config.bash @@ -55,6 +55,20 @@ config_for() { [[ ! -f $file ]] && continue + # 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 -- cgit v1.1