config/functions: add die() function

die is meant to be a more flexible exit, printing a message to go with
the exit call. It works like:

die -- same as "exit 1"
die "It just broke." -- echo statement, exit value 1
die "Tried to do the thing." "2" -- echo statement, exit value 2

Exit codes other than 1 require a message to go with it.

it would replace existing code that looks like:

echo "ERROR: Everything went wrong. Sorry!"
exit 1

or be more helpful in picking up the pieces when something unexpected
happens then:

do_something || exit 1

Signed-off-by: Ian Leonard <antonlacon@gmail.com>
This commit is contained in:
Ian Leonard 2018-10-19 17:20:54 +00:00
parent f0c1537041
commit aa00529d3a

View File

@ -1,4 +1,12 @@
### FUNCTION HELPERS ###
# die (message, code) abort with optional message and code
die() {
if [ -n "$1" ]; then
echo -e "$1" >&2
fi
exit "${2:-1}"
}
# return 0 if $2 in space-separated list $1, otherwise return 1
listcontains() {
if [ -n "$1" -a -n "$2" ]; then