mirror of
https://github.com/tests-always-included/mo.git
synced 2024-12-18 16:27:52 +00:00
Merge pull request #15 from maslennikov/master
Adding an option to fail upon unset env variables
This commit is contained in:
commit
e6787e71d5
1
.gitignore
vendored
1
.gitignore
vendored
@ -4,3 +4,4 @@ diagnostic.test
|
|||||||
tests/*.diff
|
tests/*.diff
|
||||||
spec/
|
spec/
|
||||||
spec-runner/
|
spec-runner/
|
||||||
|
node_modules/
|
||||||
|
65
mo
65
mo
@ -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}
|
||||||
@ -686,7 +696,7 @@ moParse() {
|
|||||||
# Returns nothing.
|
# Returns nothing.
|
||||||
moPartial() {
|
moPartial() {
|
||||||
# Namespace variables here to prevent conflicts.
|
# Namespace variables here to prevent conflicts.
|
||||||
local moContent moFilename moIndent moPartial moStandalone
|
local moContent moFilename moIndent moPartial moStandalone moUnindented
|
||||||
|
|
||||||
if moIsStandalone moStandalone "$2" "$4" "$5"; then
|
if moIsStandalone moStandalone "$2" "$4" "$5"; then
|
||||||
moStandalone=( $moStandalone )
|
moStandalone=( $moStandalone )
|
||||||
@ -705,16 +715,17 @@ moPartial() {
|
|||||||
(
|
(
|
||||||
# TODO: Remove dirname and use a function instead
|
# TODO: Remove dirname and use a function instead
|
||||||
cd "$(dirname -- "$moFilename")" || exit 1
|
cd "$(dirname -- "$moFilename")" || exit 1
|
||||||
moIndentLines moPartial "$moIndent" "$(
|
moUnindented="$(
|
||||||
moLoadFile moPartial "${moFilename##*/}"
|
moLoadFile moPartial "${moFilename##*/}"
|
||||||
moParse "${moPartial}" "$6" true
|
moParse "${moPartial}" "$6" true
|
||||||
|
|
||||||
# Fix bash handling of subshells and keep trailing whitespace.
|
# Fix bash handling of subshells and keep trailing whitespace.
|
||||||
# This is removed in moIndentLines.
|
# This is removed in moIndentLines.
|
||||||
echo -n "."
|
echo -n "."
|
||||||
)"
|
)" || exit 1
|
||||||
|
moIndentLines moPartial "$moIndent" "$moUnindented"
|
||||||
echo -n "$moPartial"
|
echo -n "$moPartial"
|
||||||
)
|
) || exit 1
|
||||||
|
|
||||||
local "$1" && moIndirect "$1" "$moContent"
|
local "$1" && moIndirect "$1" "$moContent"
|
||||||
}
|
}
|
||||||
@ -746,7 +757,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" ]] || moTestVarSet "$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
|
||||||
@ -863,6 +880,16 @@ moTest() {
|
|||||||
return 1
|
return 1
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# Internal: Determine if a variable is assigned, even if it is assigned an empty
|
||||||
|
# value.
|
||||||
|
#
|
||||||
|
# $1 - Variable name to check.
|
||||||
|
#
|
||||||
|
# Returns true (0) if the variable is set, 1 if the variable is unset.
|
||||||
|
moTestVarSet() {
|
||||||
|
[[ "${!1-a}" == "${!1-b}" ]]
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
# Internal: Trim the leading whitespace only.
|
# Internal: Trim the leading whitespace only.
|
||||||
#
|
#
|
||||||
|
1
tests/fail-not-set-file.expected
Normal file
1
tests/fail-not-set-file.expected
Normal file
@ -0,0 +1 @@
|
|||||||
|
Env variable not set: __NO_SUCH_VAR
|
9
tests/fail-not-set-file.sh
Executable file
9
tests/fail-not-set-file.sh
Executable file
@ -0,0 +1,9 @@
|
|||||||
|
#!/usr/bin/env bash
|
||||||
|
|
||||||
|
cd "${0%/*}"
|
||||||
|
unset __NO_SUCH_VAR
|
||||||
|
POPULATED="words" EMPTY="" ../mo --fail-not-set ./fail-not-set-file.template 2>&1
|
||||||
|
|
||||||
|
if [[ $? -ne 1 ]]; then
|
||||||
|
echo "Did not return 1"
|
||||||
|
fi
|
3
tests/fail-not-set-file.template
Normal file
3
tests/fail-not-set-file.template
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
Populated: {{POPULATED}};
|
||||||
|
Empty: {{EMPTY}};
|
||||||
|
Unset: {{__NO_SUCH_VAR}};
|
3
tests/fail-not-set.expected
Normal file
3
tests/fail-not-set.expected
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
Populated: words;
|
||||||
|
Empty: ;
|
||||||
|
Unset: Env variable not set: __NO_SUCH_VAR
|
13
tests/fail-not-set.sh
Executable file
13
tests/fail-not-set.sh
Executable file
@ -0,0 +1,13 @@
|
|||||||
|
#!/usr/bin/env bash
|
||||||
|
|
||||||
|
cd "${0%/*}"
|
||||||
|
unset __NO_SUCH_VAR
|
||||||
|
POPULATED="words" EMPTY="" ../mo --fail-not-set 2>&1 <<EOF
|
||||||
|
Populated: {{POPULATED}};
|
||||||
|
Empty: {{EMPTY}};
|
||||||
|
Unset: {{__NO_SUCH_VAR}};
|
||||||
|
EOF
|
||||||
|
|
||||||
|
if [[ $? -ne 1 ]]; then
|
||||||
|
echo "Did not return 1"
|
||||||
|
fi
|
@ -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.
|
||||||
|
Loading…
Reference in New Issue
Block a user