mirror of
https://github.com/tests-always-included/mo.git
synced 2024-12-18 16:27:52 +00:00
Merge pull request #32 from andreax79/master
Pass arguments to function as environment variable MO_ALLOW_FUNCTION_ARGUMENTS
This commit is contained in:
commit
929ffc5b88
29
demo/function-args
Executable file
29
demo/function-args
Executable file
@ -0,0 +1,29 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
#
|
||||||
|
# This sources a simple script with the env. variables needed for the template.
|
||||||
|
|
||||||
|
cd "$(dirname "$0")" # Go to the script's directory
|
||||||
|
source ../mo
|
||||||
|
|
||||||
|
export NAME="Alex"
|
||||||
|
export ARRAY=( AAA BBB CCC )
|
||||||
|
|
||||||
|
# Include an external template
|
||||||
|
INCLUDE() {
|
||||||
|
cat "$MO_FUNCTION_ARGS"
|
||||||
|
}
|
||||||
|
|
||||||
|
# Print section title
|
||||||
|
TITLE() {
|
||||||
|
echo "+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+"
|
||||||
|
echo "$MO_FUNCTION_ARGS"
|
||||||
|
echo "+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+"
|
||||||
|
}
|
||||||
|
|
||||||
|
cat <<EOF | mo -u
|
||||||
|
{{TITLE Part 1}}
|
||||||
|
{{INCLUDE function-args-part1}}
|
||||||
|
|
||||||
|
{{TITLE Part 2}}
|
||||||
|
{{INCLUDE function-args-part2}}
|
||||||
|
EOF
|
1
demo/function-args-part1
Normal file
1
demo/function-args-part1
Normal file
@ -0,0 +1 @@
|
|||||||
|
Hello, my name is {{NAME}}.
|
3
demo/function-args-part2
Normal file
3
demo/function-args-part2
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
{{#ARRAY}}
|
||||||
|
* {{.}}
|
||||||
|
{{/ARRAY}}
|
6
mo
6
mo
@ -62,6 +62,7 @@
|
|||||||
# options and arguments. This puts the content from the
|
# options and arguments. This puts the content from the
|
||||||
# template directly into an eval statement. Use with
|
# template directly into an eval statement. Use with
|
||||||
# extreme care.
|
# extreme care.
|
||||||
|
# MO_FUNCTION_ARGS - Arguments passed to the function
|
||||||
# MO_FAIL_ON_UNSET - When set to a non-empty value, expansion of an unset
|
# MO_FAIL_ON_UNSET - When set to a non-empty value, expansion of an unset
|
||||||
# env variable will be aborted with an error.
|
# 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"
|
||||||
@ -154,16 +155,17 @@ mo() (
|
|||||||
#
|
#
|
||||||
# Returns nothing.
|
# Returns nothing.
|
||||||
moCallFunction() {
|
moCallFunction() {
|
||||||
local moArgs
|
local moArgs moFunctionArgs
|
||||||
|
|
||||||
moArgs=()
|
moArgs=()
|
||||||
|
moTrimWhitespace moFunctionArgs "$3"
|
||||||
|
|
||||||
# shellcheck disable=SC2031
|
# shellcheck disable=SC2031
|
||||||
if [[ -n "${MO_ALLOW_FUNCTION_ARGUMENTS-}" ]]; then
|
if [[ -n "${MO_ALLOW_FUNCTION_ARGUMENTS-}" ]]; then
|
||||||
moArgs=$3
|
moArgs=$3
|
||||||
fi
|
fi
|
||||||
|
|
||||||
echo -n "$2" | eval "$1" "$moArgs"
|
echo -n "$2" | MO_FUNCTION_ARGS="$moFunctionArgs" eval "$1" "$moArgs"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
3
tests/function-args-read.env
Normal file
3
tests/function-args-read.env
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
testArgs() {
|
||||||
|
echo "$MO_FUNCTION_ARGS"
|
||||||
|
}
|
4
tests/function-args-read.expected
Normal file
4
tests/function-args-read.expected
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
No args: [] - done
|
||||||
|
One arg: [one] - done
|
||||||
|
Multiple arguments: [aa bb cc 'x' " ! {[_.|] - done
|
||||||
|
Evil: [bla; cat /etc/issue] - done
|
4
tests/function-args-read.template
Normal file
4
tests/function-args-read.template
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
No args: [{{testArgs}}] - done
|
||||||
|
One arg: [{{testArgs one}}] - done
|
||||||
|
Multiple arguments: [{{testArgs aa bb cc 'x' " ! {[_.| }}] - done
|
||||||
|
Evil: [{{testArgs bla; cat /etc/issue}}] - done
|
Loading…
Reference in New Issue
Block a user