aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPatrick Spek <p.spek@tyil.nl>2023-03-09 13:30:52 +0100
committerPatrick Spek <p.spek@tyil.nl>2023-03-09 13:30:52 +0100
commit5a3a5e2778c33031c42088fcddd4b5b2eb960bd2 (patch)
tree572a640f2d892206e3b5fb2c0d7cfc66e7562219
parent9575bf831765033243743f026fcaabc9a02e9f78 (diff)
Implement BASHTARD_PLAYBOOK_VARS
-rw-r--r--CHANGELOG.md5
-rw-r--r--lib/main.bash2
-rw-r--r--lib/subcommands/add.bash24
-rw-r--r--lib/subcommands/sync.bash24
4 files changed, 55 insertions, 0 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 5156976..d91ec56 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -38,6 +38,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
get an overview of all pending changes.
- A `pull` subcommand has been added to only pull the latest changes into the
`$BASHTARD_ETCDIR`, without running `sync` on all the playbooks.
+- A new global variable, `$BASHTARD_PLAYBOOK_VARS` has been added. Currently,
+ its only purpose is to check for "required" variables to be used in the
+ playbook. Before an `add` or `sync`, any variables declared to be `required`
+ in the `$BASHTARD_PLAYBOOK_VARS` array will be checked to be non-empty. If any
+ are empty, an error will be thrown and the playbook will not be ran.
### Changed
diff --git a/lib/main.bash b/lib/main.bash
index d8d4cc4..afcd58d 100644
--- a/lib/main.bash
+++ b/lib/main.bash
@@ -34,6 +34,7 @@ main() {
# Declare some global variables
declare -A BASHTARD_PLATFORM
+ declare -A BASHTARD_PLAYBOOK_VARS
# Figure out system details
debug "$BASHTARD_NAME/main" "Discovering system information"
@@ -41,6 +42,7 @@ main() {
# Export BASHTARD_ variables
export BASHTARD_PLATFORM
+ export BASHTARD_PLAYBOOK_VARS
# Source the file defining the subcommand.
debug "$BASHTARD_NAME/main" "Sourcing $subcommand_src"
diff --git a/lib/subcommands/add.bash b/lib/subcommands/add.bash
index 2ae6a43..ff1e5e9 100644
--- a/lib/subcommands/add.bash
+++ b/lib/subcommands/add.bash
@@ -7,6 +7,7 @@
subcommand()
{
local buffer
+ local missing_vars=0
export BASHTARD_PLAYBOOK="$1" ; shift
@@ -43,6 +44,29 @@ subcommand()
# shellcheck disable=SC1090,SC1091
. "$playbook_base/playbook.bash"
+ # Ensure all required vars are non-empty
+ debug "bashtard/add" "Checking for \$BASHTARD_PLAYBOOK_VARS"
+
+ for key in "${!BASHTARD_PLAYBOOK_VARS[@]}"
+ do
+ # shellcheck disable=SC2086
+ in_args "required" ${BASHTARD_PLAYBOOK_VARS[$key]} || continue
+
+ debug "bashtard/add" "Checking \$BASHTARD_PLAYBOOK_VARS[$key]"
+
+ if [[ "$(config "$key")" == "" ]]
+ then
+ missing_vars=$(( missing_vars + 1))
+ fi
+ done
+
+ if (( 0 < missing_vars ))
+ then
+ emerg "bashtard/add" "One or more required variables are unset"
+ return 3
+ fi
+
+ # Run the playbook
if ! playbook_add
then
crit "bashtard/add" "$BASHTARD_PLAYBOOK reported an error"
diff --git a/lib/subcommands/sync.bash b/lib/subcommands/sync.bash
index 1ae2aae..8f5e2cd 100644
--- a/lib/subcommands/sync.bash
+++ b/lib/subcommands/sync.bash
@@ -29,6 +29,7 @@ subcommand()
sync_playbook()
{
local playbook_base="$BASHTARD_ETCDIR/playbooks.d/$BASHTARD_PLAYBOOK"
+ local missing_vars=0
notice "bashtard/sync" "Running sync for $BASHTARD_PLAYBOOK"
@@ -47,5 +48,28 @@ sync_playbook()
# shellcheck disable=SC1090,SC1091
. "$playbook_base/playbook.bash"
+ debug "bashtard/sync" "Checking for \$BASHTARD_PLAYBOOK_VARS"
+
+ # Ensure all required vars are non-empty
+ for key in "${!BASHTARD_PLAYBOOK_VARS[@]}"
+ do
+ # shellcheck disable=SC2086
+ in_args "required" ${BASHTARD_PLAYBOOK_VARS[$key]} || continue
+
+ debug "bashtard/sync" "Checking \$BASHTARD_PLAYBOOK_VARS[$key]"
+
+ if [[ "$(config "$key")" == "" ]]
+ then
+ missing_vars=$(( missing_vars + 1))
+ fi
+ done
+
+ if (( 0 < missing_vars ))
+ then
+ emerg "bashtard/sync" "One or more required variables are unset"
+ return 3
+ fi
+
+ # Run the playbook
playbook_sync
}