From fcedd32155d3b9630c969023a08c545b4aac052d Mon Sep 17 00:00:00 2001 From: Andrea Bonomi Date: Wed, 7 Aug 2019 14:59:00 +0200 Subject: [PATCH 1/2] 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 From 5b8cf240687816a81d948bcb9e32f6077f74f10d Mon Sep 17 00:00:00 2001 From: Andrea Bonomi Date: Thu, 8 Aug 2019 10:36:44 +0200 Subject: [PATCH 2/2] Pass arguments to function as environment variable MO_ALLOW_FUNCTION_ARGUMENTS --- demo/function-args | 29 +++++++++++++++++++++++++++++ demo/function-args-part1 | 1 + demo/function-args-part2 | 3 +++ mo | 14 +++++++------- tests/function-args-read.env | 2 +- 5 files changed, 41 insertions(+), 8 deletions(-) create mode 100755 demo/function-args create mode 100644 demo/function-args-part1 create mode 100644 demo/function-args-part2 diff --git a/demo/function-args b/demo/function-args new file mode 100755 index 0000000..83fa354 --- /dev/null +++ b/demo/function-args @@ -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 <