aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPatrick Spek <p.spek@tyil.nl>2024-03-05 08:28:40 +0100
committerPatrick Spek <p.spek@tyil.nl>2024-03-05 08:28:40 +0100
commit8db5cd6989b53c929b4ab628083ac48ae592ca25 (patch)
tree91c4836a28c196f22d525ae5f6e78d6d1788afa3
parentce95a02b1aa618dda4b24c62b19107995cd08a69 (diff)
Implement variable referencesHEADmaster
-rw-r--r--CHANGELOG.md6
-rw-r--r--lib/util/config.bash14
2 files changed, 20 insertions, 0 deletions
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