summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAaron Ball <nullspoon@oper.io>2018-03-05 08:24:07 -0700
committerAaron Ball <nullspoon@oper.io>2018-03-05 08:24:07 -0700
commitb676abb0f0faf699f83146e423cb49ec87146196 (patch)
tree0a51af8067e01cc161d6a7a0b478534ce9e60167
parente2d16d96663de4b22a61da8ad3e70ee540d4fc57 (diff)
downloadpkgself-b676abb0f0faf699f83146e423cb49ec87146196.tar.gz
pkgself-b676abb0f0faf699f83146e423cb49ec87146196.tar.xz
Added libensure
This provides the ensure::set function as well as the ERRNO_ENSURE variable. The function checks if a variable is set, and returns code 1 if unset, or code 0 if set. It also distinguishes between unset and empty (empty is considered set). The ERRNO_ENSURE variable is set to 1 when an unset variable is passed to ensure::set. This variable is useful for checking a list of variables without the need to catch every return code (just check ERRNO_ENSURE for code 1 at the end of the variable sequence).
-rw-r--r--libinstall/ensure.sh24
1 files changed, 24 insertions, 0 deletions
diff --git a/libinstall/ensure.sh b/libinstall/ensure.sh
new file mode 100644
index 0000000..f703087
--- /dev/null
+++ b/libinstall/ensure.sh
@@ -0,0 +1,24 @@
+#!/usr/bin/env bash
+export ERRNO_ENSURE=0
+
+# ensures::set:
+# @ref Reference to the variable to be checked
+# @msg Message to print if variable is unset
+#
+# Ensures that the specified variable is set. If varible is unset, the
+# specified variable description is printed out.
+#
+# Note: An empty variable will return as 'set'.
+#
+# Returns 0 when set and 1 when unset
+ensure::set() {
+ local _ref="${1:-}"
+ local _msg="${2:-}"
+ if [ -z "${!_ref+x}" ]; then
+ printf -- "Error: Required variable '%s' unset.\n" "${_ref}" >&2
+ printf -- " %s: %s\n" "${_ref}" "${_msg}" >&2
+ ERRNO_ENSURE=1
+ return 1
+ fi
+ return 0
+}

Generated by cgit