Changing how functions are called

Old way:

    functionName CONTENT argument1 argument2

New way:

    echo -n "$CONTENT" | functionName argument1 argument2

This follow the Unix style more closely.
This commit is contained in:
Tyler Akins 2017-11-13 10:13:03 -06:00
parent e78b15d95b
commit ad98577c42
No known key found for this signature in database
GPG Key ID: 8F3B8C432F4393BD
7 changed files with 25 additions and 22 deletions

View File

@ -3,6 +3,9 @@
cd "$(dirname "$0")" # Go to the script's directory cd "$(dirname "$0")" # Go to the script's directory
EVERY_REPO() { EVERY_REPO() {
# The block contents come in through standard input. Capture it here.
content=$(cat)
echo "# Starting EVERY_REPO" echo "# Starting EVERY_REPO"
# Get list of repos # Get list of repos
@ -14,7 +17,7 @@ EVERY_REPO() {
# It rewrites {{__REPO__.name}} into {{resque.name}}, for instance. # It rewrites {{__REPO__.name}} into {{resque.name}}, for instance.
# You can prefix your environment variables and do other things as well. # You can prefix your environment variables and do other things as well.
echo -n "$1" | sed "s/__REPO__/${REPO}/" echo "$content" | sed "s/__REPO__/${REPO}/"
echo "## Looped one time for repo: $REPO" echo "## Looped one time for repo: $REPO"
done done

View File

@ -2,13 +2,17 @@
cd "$(dirname "$0")"/.. cd "$(dirname "$0")"/..
date-string() { date; } date-string() {
wrapper() { echo -n "*** $1 ***"; } date
}
wrapper() {
echo -n "*** $(cat) ***"
}
export IP=127.0.0.1 export IP=127.0.0.1
export ALLOWED_HOSTS=( 192.168.0.1 192.168.0.2 192.168.0.3 ) export ALLOWED_HOSTS=( 192.168.0.1 192.168.0.2 192.168.0.3 )
. mo # Keep in mind this script is executing in the parent directory . ./mo # Keep in mind this script is executing in the parent directory
cat <<EOF | mo cat <<EOF | mo
# {{#wrapper}}OH SO IMPORTANT{{/wrapper}} # {{#wrapper}}OH SO IMPORTANT{{/wrapper}}
# This file automatically generated at {{date-string}} # This file automatically generated at {{date-string}}

11
mo
View File

@ -141,15 +141,16 @@ mo() (
# #
# Returns nothing. # Returns nothing.
moCallFunction() { moCallFunction() {
local moCommand local moArgs
moArgs=()
# shellcheck disable=SC2031 # shellcheck disable=SC2031
if [[ -n "$MO_ALLOW_FUNCTION_ARGUMENTS" ]]; then if [[ -n "$MO_ALLOW_FUNCTION_ARGUMENTS" ]]; then
printf -v moCommand "%q %q %s" "$1" "$2" "$3" moArgs=$3
eval "$moCommand"
else
"$1" "$2"
fi fi
echo -n "$2" | eval "$1" "$moArgs"
} }

View File

@ -2,16 +2,12 @@ name=Willy
MO_ALLOW_FUNCTION_ARGUMENTS=true MO_ALLOW_FUNCTION_ARGUMENTS=true
pipeTo() { pipeTo() {
echo -n "$1" | "$2" cat | "$1"
} }
testArgs() { testArgs() {
printf "%d" "$#" printf "%d" "$#"
# Remove content. Note that when zero arguments are passed, this
# line does nothing and $1 will still be the content.
shift
# Display all arguments # Display all arguments
printf " %q" "$@" printf " %q" ${@+"$@"}
} }

View File

@ -1,4 +1,4 @@
No args: 1 '' - done No args: 0 '' - done
One arg: 2 one - done One arg: 1 one - done
Getting name in a string: 2 The\ name\ is\ Willy - done Getting name in a string: 1 The\ name\ is\ Willy - done
Reverse this: edcba Reverse this: edcba

View File

@ -1,5 +1,5 @@
name=Willy name=Willy
wrapped() { wrapped() {
# The final newline is eaten by mo # This eats the newline in the content
echo "<b>$1</b>" echo "<b>$(cat)</b>"
} }

View File

@ -1,2 +1 @@
<b> Willy is awesome. <b> Willy is awesome.</b>... this is the last line.
</b>... this is the last line.