From cad506a3d8097d42d116400a26dbe4897d598b81 Mon Sep 17 00:00:00 2001 From: Patrick Spek Date: Fri, 10 Mar 2023 10:32:18 +0100 Subject: Move the file_template function and add a comment --- lib/util.bash | 72 +++++++++++++++++++++++++++++++---------------------------- 1 file changed, 38 insertions(+), 34 deletions(-) (limited to 'lib/util.bash') diff --git a/lib/util.bash b/lib/util.bash index 1e6854a..e5dbc3f 100644 --- a/lib/util.bash +++ b/lib/util.bash @@ -106,40 +106,10 @@ fetch_http_wget() { wget --quiet --output-document "$2" "$1" } -# Check if the first argument given appears in the list of all following -# arguments. -in_args() { - local needle="$1" - shift - - for arg in "$@" - do - [[ $needle == "$arg" ]] && return 0 - done - - return 1 -} - -# Join a list of arguments into a single string. By default, this will join -# using a ",", but you can set a different character using -c. Note that this -# only joins with a single character, not a string of characters. -join_args() { - local OPTIND - local IFS="," - - while getopts ":c:" opt - do - case "$opt" in - c) IFS="$OPTARG" ;; - *) warn "bashtard/join_args" "Unused opt specified: $opt" ;; - esac - done - - shift $(( OPTIND - 1)) - - printf "%s" "$*" -} - +# A very simple means of templating a file, using sed and awk. The template +# file is assumed to exist within the share directory of the current playbook. +# Variables are passed as key=value pairs to this function. Inside the +# template, they are expected to be written as ${key}. file_template() { local file="$BASHTARD_ETCDIR/playbooks.d/$BASHTARD_PLAYBOOK/share/$1" ; shift @@ -178,6 +148,40 @@ file_template() sed -f "$sedfile" "$file" } +# Check if the first argument given appears in the list of all following +# arguments. +in_args() { + local needle="$1" + shift + + for arg in "$@" + do + [[ $needle == "$arg" ]] && return 0 + done + + return 1 +} + +# Join a list of arguments into a single string. By default, this will join +# using a ",", but you can set a different character using -c. Note that this +# only joins with a single character, not a string of characters. +join_args() { + local OPTIND + local IFS="," + + while getopts ":c:" opt + do + case "$opt" in + c) IFS="$OPTARG" ;; + *) warn "bashtard/join_args" "Unused opt specified: $opt" ;; + esac + done + + shift $(( OPTIND - 1)) + + printf "%s" "$*" +} + # Create a temporary directory. Similar to tempfile, but you'll get a directory # instead. tmpdir() { -- cgit v1.1