aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPatrick Spek <p.spek@tyil.nl>2022-05-23 13:02:23 +0200
committerPatrick Spek <p.spek@tyil.nl>2022-05-23 13:02:23 +0200
commit50b197935ee39ac9efd472dd7b3de5e497db4ea4 (patch)
treec024fd1208f38d354a5f2ed9012900b428ec0f7e
parentea843ed1bcc863ba8aef8fa972ce0732f386e550 (diff)
Add config_subkeys
-rw-r--r--CHANGELOG.md3
-rw-r--r--lib/util/config.bash44
2 files changed, 47 insertions, 0 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 7930672..3342642 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -18,6 +18,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- The `var` subcommand is now referenced in `usage()`.
- A `pkg` subcommand has been added, to allow for direct interaction with the
`pkg_*()` utilities provided by Bashtard.
+- `config_subkeys()` and `config_subkeys_for` have been added, to look up
+ subkeys defined in config files. These can help when you want to use a list
+ somewhere in your configuration.
### Changed
diff --git a/lib/util/config.bash b/lib/util/config.bash
index 3f939ff..91d5c83 100644
--- a/lib/util/config.bash
+++ b/lib/util/config.bash
@@ -10,6 +10,10 @@ config() {
config_for "${BASHTARD_PLATFORM[fqdn]}" "$@"
}
+config_subkeys() {
+ config_subkeys_for "${BASHTARD_PLATFORM[fqdn]}" "$@"
+}
+
config_for() {
local host=$1 ; shift
local key=$1 ; shift
@@ -68,3 +72,43 @@ config_for() {
# Error
alert "bashtard/config_for" "No configuration value for $key"
}
+
+config_subkeys_for() {
+ local host=$1 ; shift
+ local key=$1 ; shift
+
+ local file
+ local files
+
+ files=(
+ "$BASHTARD_ETCDIR/secrets"
+ "$BASHTARD_ETCDIR/hosts.d/$host"
+ "$BASHTARD_ETCDIR/os.d/${BASHTARD_PLATFORM[key]}"
+ "$BASHTARD_ETCDIR/defaults"
+ )
+
+ if [[ -n "$BASHTARD_PLAYBOOK" ]]
+ 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"
+ )
+ fi
+
+ files+=(
+ "$BASHTARD_SHAREDIR/os.d/${BASHTARD_PLATFORM[key]}"
+ "$BASHTARD_SHAREDIR/defaults"
+ )
+
+ # Check configuration files
+ for file in "${files[@]}"
+ do
+ debug "bashtard/config_subkeys" "Checking for subkeys of '$key' in '$file'"
+
+ [[ ! -f $file ]] && continue
+
+ grep "^$key\." "$file" \
+ | awk -F= '{ print $1 }'
+ done
+}