From fcedd32155d3b9630c969023a08c545b4aac052d Mon Sep 17 00:00:00 2001 From: Andrea Bonomi Date: Wed, 7 Aug 2019 14:59:00 +0200 Subject: [PATCH] Pass arguments to function as environment variable MO_ARGS --- mo | 10 ++++++---- tests/function-args-read.env | 3 +++ tests/function-args-read.expected | 4 ++++ tests/function-args-read.template | 4 ++++ 4 files changed, 17 insertions(+), 4 deletions(-) create mode 100644 tests/function-args-read.env create mode 100644 tests/function-args-read.expected create mode 100644 tests/function-args-read.template diff --git a/mo b/mo index 5fb7ca2..271627c 100755 --- a/mo +++ b/mo @@ -62,6 +62,8 @@ # options and arguments. This puts the content from the # template directly into an eval statement. Use with # extreme care. +# MO_ARGS - Arguments passed to the function, +# only if MO_ALLOW_FUNCTION_ARGUMENTS is unset # 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" @@ -156,14 +158,14 @@ mo() ( moCallFunction() { local moArgs - moArgs=() - # shellcheck disable=SC2031 if [[ -n "${MO_ALLOW_FUNCTION_ARGUMENTS-}" ]]; then moArgs=$3 + echo -n "$2" | eval "$1" "$moArgs" + else + moTrimWhitespace moArgs "$3" + echo -n "$2" | MO_ARGS="$moArgs" eval "$1" fi - - echo -n "$2" | eval "$1" "$moArgs" } diff --git a/tests/function-args-read.env b/tests/function-args-read.env new file mode 100644 index 0000000..2ff7a01 --- /dev/null +++ b/tests/function-args-read.env @@ -0,0 +1,3 @@ +testArgs() { + echo "$MO_ARGS" +} diff --git a/tests/function-args-read.expected b/tests/function-args-read.expected new file mode 100644 index 0000000..ee15f37 --- /dev/null +++ b/tests/function-args-read.expected @@ -0,0 +1,4 @@ +No args: [] - done +One arg: [one] - done +Multiple arguments: [aa bb cc 'x' " ! {[_.|] - done +Evil: [bla; cat /etc/issue] - done diff --git a/tests/function-args-read.template b/tests/function-args-read.template new file mode 100644 index 0000000..de02070 --- /dev/null +++ b/tests/function-args-read.template @@ -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