mirror of
https://github.com/tests-always-included/mo.git
synced 2024-12-18 16:27:52 +00:00
Updates for how to source "mo" and then use it
When you source mo, it adds the "mo" function to the environment.
This commit is contained in:
parent
e623b16bb8
commit
b16d73b5a7
@ -47,14 +47,15 @@ Using arrays adds a slight level of complexity. *You must source `mo`.* Look a
|
||||
#!/bin/bash
|
||||
cd "$(dirname "$0")" # Go to the script's directory
|
||||
export ARRAY=( one two "three three three" four five )
|
||||
cat << EOF | . ../mo
|
||||
. ../mo # This loads the "mo" function
|
||||
cat << EOF | mo
|
||||
Here are the items in the array:
|
||||
{{#ARRAY}}
|
||||
* {{.}}
|
||||
{{/ARRAY}}
|
||||
EOF
|
||||
|
||||
The result? You get a list of the five elements in the array. Take a look at the line that executes `mo`. You'll see that we went from `../mo` to using `. ../mo`. That is very important when you want arrays to work, since you can not execute a command and have arrays passed to that command's environment. Instead, we source the file. `. ../mo` is identical to `source ../mo` and you can find more about that in bash's man page.
|
||||
The result? You get a list of the five elements in the array. It is vital that you source `mo` and run the function when you want arrays to work because you can not execute a command and have arrays passed to that command's environment. Instead, we first source the file to load the function and then run the function directly.
|
||||
|
||||
There are more scripts available in the [demos directory](demo/) that could help illustrate how you would use this program.
|
||||
|
||||
|
@ -4,8 +4,9 @@ cd "$(dirname "$0")" # Go to the script's directory
|
||||
|
||||
declare -A DATA
|
||||
DATA=([one]=111 [two]=222)
|
||||
. ../mo
|
||||
|
||||
cat <<EOF | . ../mo
|
||||
cat <<EOF | mo
|
||||
Accessing data directly:
|
||||
DATA: {{DATA}}
|
||||
One: {{DATA.one}}
|
||||
|
@ -11,7 +11,8 @@ export OPTIONS=(
|
||||
"Call a service representative at 1-800-000-0000 to discuss payment options"
|
||||
"Return the vehicle immediately and pay a fine of 1 million dollars"
|
||||
)
|
||||
sed '0,/^# END/ d' "$(basename "$0")" | . ../mo
|
||||
. ../mo
|
||||
sed '0,/^# END/ d' "$(basename "$0")" | mo
|
||||
exit
|
||||
|
||||
# END
|
||||
|
@ -28,8 +28,8 @@ declare -A resque hub rip
|
||||
resque=([name]=Resque [url]=http://example.com/resque)
|
||||
hub=([name]=Hub [url]=http://example.com/hub)
|
||||
rip=([name]=Rip [url]=http://example.com/rip)
|
||||
|
||||
cat <<EOF | . ../mo
|
||||
. ../mo
|
||||
cat <<EOF | mo
|
||||
{{#EVERY_REPO}}
|
||||
The repo is __REPO__
|
||||
Name: {{__REPO__.name}}
|
||||
|
@ -8,7 +8,8 @@ wrapper() { echo -n "*** $1 ***"; }
|
||||
export IP=127.0.0.1
|
||||
export ALLOWED_HOSTS=( 192.168.0.1 192.168.0.2 192.168.0.3 )
|
||||
|
||||
cat <<EOF | . mo
|
||||
. mo # Keep in mind this script is executing in the parent directory
|
||||
cat <<EOF | mo
|
||||
# {{#wrapper}}OH SO IMPORTANT{{/wrapper}}
|
||||
# This file automatically generated at {{date-string}}
|
||||
home_ip={{IP}}
|
||||
|
@ -1,7 +1,8 @@
|
||||
#!/bin/bash
|
||||
cd "$(dirname "$0")" # Go to the script's directory
|
||||
export ARRAY=( one two "three three three" four five )
|
||||
cat << EOF | . ../mo
|
||||
. ../mo
|
||||
cat << EOF | mo
|
||||
Here are the items in the array:
|
||||
{{#ARRAY}}
|
||||
* {{.}}
|
||||
|
@ -1,4 +1,7 @@
|
||||
#!/bin/bash
|
||||
#
|
||||
# This example does not source `mo` and is intentionally restricted to
|
||||
# variables that are not arrays.
|
||||
cd "$(dirname "$0")" # Go to the script's directory
|
||||
export TEST="This is a test"
|
||||
echo "Your message: {{TEST}}" | ../mo
|
||||
|
@ -2,6 +2,7 @@
|
||||
|
||||
cd "$(dirname $0)"
|
||||
|
||||
. ./mo
|
||||
PASS=0
|
||||
FAIL=0
|
||||
|
||||
@ -11,7 +12,7 @@ for TEST in tests/*.expected; do
|
||||
echo -n "$BASE ... "
|
||||
echo "Do not read this input" | (
|
||||
. "${BASE}.env"
|
||||
. ./mo "${BASE}.template"
|
||||
mo "${BASE}.template"
|
||||
) | diff -wU5 - "${TEST}" > "${BASE}.diff"
|
||||
|
||||
if [[ $? -ne 0 ]]; then
|
||||
|
Loading…
Reference in New Issue
Block a user