From 28c55aa1a8c82def2ffdb3e000812a5523371ac5 Mon Sep 17 00:00:00 2001 From: Patrick Spek Date: Fri, 10 Mar 2023 10:44:48 +0100 Subject: Add playbook_path() --- CHANGELOG.md | 2 ++ lib/subcommands/add.bash | 2 +- lib/subcommands/del.bash | 2 +- lib/subcommands/sync.bash | 2 +- lib/util.bash | 20 +++++++++++++++++++- lib/util/config.bash | 4 ++-- 6 files changed, 26 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f0fe9b6..78cf85f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -47,6 +47,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 between playbook runs. This new directory is intended to create a clearer seperation between a playbook and a user's specific data used by the playbook, which in turn should make re-using playbooks easier. +- A convenience function has been introduced, `playbook_path()`, which can give + you the absolute path to the playbook's base or data directory. ### Changed diff --git a/lib/subcommands/add.bash b/lib/subcommands/add.bash index 8df2e49..644c04f 100644 --- a/lib/subcommands/add.bash +++ b/lib/subcommands/add.bash @@ -17,7 +17,7 @@ subcommand() return 2 fi - local playbook_base="$BASHTARD_ETCDIR/playbooks.d/$BASHTARD_PLAYBOOK" + local playbook_base="$(playbook_path "base")" local playbook_registry="$BASHTARD_ETCDIR/registry.d/${BASHTARD_PLATFORM[fqdn]}" # Make sure we only run add if the playbook is not in the registry yet diff --git a/lib/subcommands/del.bash b/lib/subcommands/del.bash index c09c501..38d675c 100644 --- a/lib/subcommands/del.bash +++ b/lib/subcommands/del.bash @@ -16,7 +16,7 @@ subcommand() return fi - local playbook_base="$BASHTARD_ETCDIR/playbooks.d/$BASHTARD_PLAYBOOK" + local playbook_base="$(playbook_path "base")" local playbook_registry="$BASHTARD_ETCDIR/registry.d/${BASHTARD_PLATFORM[fqdn]}" # Make sure we only run add if the playbook is not in the registry yet diff --git a/lib/subcommands/sync.bash b/lib/subcommands/sync.bash index 8f5e2cd..6bda162 100644 --- a/lib/subcommands/sync.bash +++ b/lib/subcommands/sync.bash @@ -28,7 +28,7 @@ subcommand() sync_playbook() { - local playbook_base="$BASHTARD_ETCDIR/playbooks.d/$BASHTARD_PLAYBOOK" + local playbook_base="$(playbook_path "base")" local missing_vars=0 notice "bashtard/sync" "Running sync for $BASHTARD_PLAYBOOK" diff --git a/lib/util.bash b/lib/util.bash index e5dbc3f..489ccf1 100644 --- a/lib/util.bash +++ b/lib/util.bash @@ -112,7 +112,7 @@ fetch_http_wget() { # template, they are expected to be written as ${key}. file_template() { - local file="$BASHTARD_ETCDIR/playbooks.d/$BASHTARD_PLAYBOOK/share/$1" ; shift + local file="$(playbook_path "base")/share/$1" ; shift local sedfile sedfile="$(tmpfile)" @@ -182,6 +182,24 @@ join_args() { printf "%s" "$*" } +# Convenience function to easily get paths used by the playbook, or to use in +# your playbook. +playbook_path() { + if [[ -z "$BASHTARD_PLAYBOOK" ]] + then + crit "bashtard/playbook_path" "Called outside of a playbook" + return 1 + fi + + case "$1" in + base) printf "$BASHTARD_ETCDIR/playbooks.d/$BASHTARD_PLAYBOOK" ;; + data) printf "$BASHTARD_ETCDIR/data.d/$BASHTARD_PLAYBOOK" ;; + *) + crit "bashtard/playbook_path" "Invalid path '$1'" + return 1 + esac +} + # Create a temporary directory. Similar to tempfile, but you'll get a directory # instead. tmpdir() { diff --git a/lib/util/config.bash b/lib/util/config.bash index ff8b66a..c114742 100644 --- a/lib/util/config.bash +++ b/lib/util/config.bash @@ -34,8 +34,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 -- cgit v1.1