aboutsummaryrefslogtreecommitdiff
path: root/lib/util.bash
diff options
context:
space:
mode:
Diffstat (limited to 'lib/util.bash')
-rw-r--r--lib/util.bash48
1 files changed, 41 insertions, 7 deletions
diff --git a/lib/util.bash b/lib/util.bash
index 54ec562..ad84247 100644
--- a/lib/util.bash
+++ b/lib/util.bash
@@ -18,6 +18,11 @@ chgdir() {
cd -- "$1" || die "Failed to change directory to $1"
}
+# Removes whitespace surrounding a given text.
+chomp() {
+ awk '{$1=$1};1' <<< "$@"
+}
+
# Create a datetime stamp. This is a wrapper around the date utility, ensuring
# that the date being formatted is always in UTC and respect SOURCE_DATE_EPOCH,
# if it is set.
@@ -61,9 +66,19 @@ die() {
# in a directory. It can be used to check whether contents changed after
# templating files in a given directory.
dir_hash() {
- find "$1" -type f -exec sha1sum {} \; \
- | sha1sum \
- | awk '{ print $1 }'
+ local path
+
+ path="$1" ; shift
+
+ for entry in "$path"/*
+ do
+ if [[ -d "$entry" ]]
+ then
+ dir_hash "$entry"
+ fi
+
+ file_hash "$entry"
+ done | file_hash -
}
# Fetch a file from an URL. Using this function introduces a dependency on curl.
@@ -106,6 +121,25 @@ fetch_http_wget() {
wget --quiet --output-document "$2" "$1"
}
+# Hash a given file. This is a convenience function to work around different
+# systems calling their file hashing programs differently, and generating
+# different output. This function only expects 1 file as argument, and only
+# outputs the hash of this particular file.
+file_hash() {
+ file_hash_md5 "$@"
+}
+
+file_hash_md5() {
+ local file
+
+ file="$1" ; shift
+
+ case "${BASHTARD_PLATFORM[key]}" in
+ freebsd) md5 "$file" | awk '{ print $NF }' ;;
+ linux-*) md5sum "$file" | awk '{ print $1 }' ;;
+ esac
+}
+
# 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
@@ -128,18 +162,18 @@ file_template()
do
debug "bashtard/template" "Adding $kv to sedfile at $sedfile"
- key="$(awk -F= '{ print $1 }' <<< "$kv")"
+ key="$(cut -d'=' -f -1 <<< "$kv")"
if [[ -z "$key" ]]
then
- crit "bashtard/template" "Empty key in '$kv' while rendering $file?"
+ crit "bashtard/template" "Empty key in '$kv' while rendering $file"
fi
- value="$(awk -F= '{ print $NF }' <<< "$kv")"
+ value="$(cut -d'=' -f 2- <<< "$kv")"
if [[ -z "$value" ]]
then
- crit "bashtard/template" "Empty key in '$kv' while rendering $file?"
+ crit "bashtard/template" "Empty key in '$kv' while rendering $file"
fi
# shellcheck disable=SC2016