Adding an option to fail upon unset env variables

This commit is contained in:
Alexey Maslennikov 2017-06-15 20:47:51 +02:00
parent 4ccfaf9d79
commit 50aa18e2a6
5 changed files with 46 additions and 18 deletions

1
.gitignore vendored
View File

@ -4,3 +4,4 @@ diagnostic.test
tests/*.diff tests/*.diff
spec/ spec/
spec-runner/ spec-runner/
node_modules/

46
mo
View File

@ -13,9 +13,10 @@
#/ #/
#/ mo [--false] [--help] [--source=FILE] filenames... #/ mo [--false] [--help] [--source=FILE] filenames...
#/ #/
#/ --false - Treat the string "false" as empty for conditionals. #/ --fail-not-set - Fail upon expansion of an unset variable.
#/ --help - This message. #/ --false - Treat the string "false" as empty for conditionals.
#/ --source=FILE - Load FILE into the environment before processing templates. #/ --help - This message.
#/ --source=FILE - Load FILE into the environment before processing templates.
# #
# Mo is under a MIT style licence with an additional non-advertising clause. # Mo is under a MIT style licence with an additional non-advertising clause.
# See LICENSE.md for the full text. # See LICENSE.md for the full text.
@ -27,19 +28,23 @@
# Public: Template parser function. Writes templates to stdout. # Public: Template parser function. Writes templates to stdout.
# #
# $0 - Name of the mo file, used for getting the help message. # $0 - Name of the mo file, used for getting the help message.
# --false - Treat "false" as an empty value. You may set the # --fail-not-set - Fail upon expansion of an unset variable. Default behavior
# MO_FALSE_IS_EMPTY environment variable instead to a non-empty # is to silently ignore and expand into empty string.
# value to enable this behavior. # --false - Treat "false" as an empty value. You may set the
# --help - Display a help message. # MO_FALSE_IS_EMPTY environment variable instead to a non-empty
# --source=FILE - Source a file into the environment before processint # value to enable this behavior.
# template files. # --help - Display a help message.
# -- - Used to indicate the end of options. You may optionally # --source=FILE - Source a file into the environment before processint
# use this when filenames may start with two hyphens. # template files.
# $@ - Filenames to parse. # -- - Used to indicate the end of options. You may optionally
# use this when filenames may start with two hyphens.
# $@ - Filenames to parse.
# #
# Mo uses the following environment variables: # Mo uses the following environment variables:
# #
# MO_FAIL_ON_UNSET - When set to a non-empty value, expansion of an unset
# env variable will be aborted with an error.
# MO_FALSE_IS_EMPTY - When set to a non-empty value, the string "false" # MO_FALSE_IS_EMPTY - When set to a non-empty value, the string "false"
# will be treated as an empty value for the purposes # will be treated as an empty value for the purposes
# of conditionals. # of conditionals.
@ -69,6 +74,11 @@ mo() (
exit 0 exit 0
;; ;;
--fail-not-set)
# shellcheck disable=SC2030
MO_FAIL_ON_UNSET=true
;;
--false) --false)
# shellcheck disable=SC2030 # shellcheck disable=SC2030
MO_FALSE_IS_EMPTY=true MO_FALSE_IS_EMPTY=true
@ -255,7 +265,7 @@ moIndentLines() {
local content fragment len posN posR result trimmed local content fragment len posN posR result trimmed
result="" result=""
#: Remove the period from the end of the string. #: Remove the period from the end of the string.
len=$((${#3} - 1)) len=$((${#3} - 1))
content=${3:0:$len} content=${3:0:$len}
@ -746,7 +756,13 @@ moShow() {
eval moJoin moJoined "," "\${$1[@]}" eval moJoin moJoined "," "\${$1[@]}"
echo -n "$moJoined" echo -n "$moJoined"
else else
echo -n "${!1}" # shellcheck disable=SC2031
if [[ -z "$MO_FAIL_ON_UNSET" ]] || moTest "$1"; then
echo -n "${!1}"
else
echo "Env variable not set: $1" >&2
exit 1
fi
fi fi
else else
# Further subindexes are disallowed # Further subindexes are disallowed

View File

@ -0,0 +1 @@
This will fail: Env variable not set: __NO_SUCH_VAR

9
tests/fail-not-set.sh Executable file
View File

@ -0,0 +1,9 @@
#!/usr/bin/env bash
cd "${0%/*}"
unset __NO_SUCH_VAR
echo "This will fail: {{__NO_SUCH_VAR}}" | ../mo --fail-not-set 2>&1
if [[ $? -ne 1 ]]; then
echo "Did not return 1"
fi

View File

@ -11,6 +11,7 @@ Simple usage:
mo [--false] [--help] [--source=FILE] filenames... mo [--false] [--help] [--source=FILE] filenames...
--false - Treat the string "false" as empty for conditionals. --fail-not-set - Fail upon expansion of an unset variable.
--help - This message. --false - Treat the string "false" as empty for conditionals.
--source=FILE - Load FILE into the environment before processing templates. --help - This message.
--source=FILE - Load FILE into the environment before processing templates.