mirror of
https://github.com/tests-always-included/mo.git
synced 2024-12-18 16:27:52 +00:00
Merge pull request #9 from Alexandre-Silva/master
mo can now source a script before parsing templates
This commit is contained in:
commit
ade7c1e77b
13
demo/sourcing
Executable file
13
demo/sourcing
Executable file
@ -0,0 +1,13 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
#
|
||||||
|
# This sources a simple script with the env. variables needed for the template.
|
||||||
|
|
||||||
|
cd "$(dirname "$0")" # Go to the script's directory
|
||||||
|
|
||||||
|
cat <<EOF | ../mo --source=sourcing.vars
|
||||||
|
Hello, my name is {{NAME}}.
|
||||||
|
And this is ARRAY's conntents:
|
||||||
|
{{#ARRAY}}
|
||||||
|
* {{.}}
|
||||||
|
{{/ARRAY}}
|
||||||
|
EOF
|
2
demo/sourcing.vars
Normal file
2
demo/sourcing.vars
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
export NAME="Alex"
|
||||||
|
export ARRAY=( AAA BBB CCC )
|
40
mo
40
mo
@ -21,26 +21,46 @@
|
|||||||
#
|
#
|
||||||
# $0 - Name of the mo file, used for getting the help message.
|
# $0 - Name of the mo file, used for getting the help message.
|
||||||
# $* - Filenames to parse. Can use -h or --help as the only option
|
# $* - Filenames to parse. Can use -h or --help as the only option
|
||||||
# in order to show a help message.
|
# in order to show a help message. Additionaly, --source=file
|
||||||
|
# can be passed so that mo sources the file before parsing
|
||||||
|
# the filenames.
|
||||||
#
|
#
|
||||||
# Returns nothing.
|
# Returns nothing.
|
||||||
mo() (
|
mo() (
|
||||||
# This function executes in a subshell so IFS is reset.
|
# This function executes in a subshell so IFS is reset.
|
||||||
# Namespace this variable so we don't conflict with desired values.
|
# Namespace this variable so we don't conflict with desired values.
|
||||||
local moContent
|
local moContent files=()
|
||||||
|
|
||||||
IFS=$' \n\t'
|
IFS=$' \n\t'
|
||||||
|
|
||||||
if [[ $# -gt 0 ]]; then
|
if [[ $# -gt 0 ]]; then
|
||||||
case "$1" in
|
for arg in "$@"; do
|
||||||
-h|--h|--he|--hel|--help)
|
case "$arg" in
|
||||||
moUsage "$0"
|
-h|--h|--he|--hel|--help)
|
||||||
exit 0
|
moUsage "$0"
|
||||||
;;
|
exit 0
|
||||||
esac
|
;;
|
||||||
|
|
||||||
|
--source=*)
|
||||||
|
local f2source="${1#--source=}"
|
||||||
|
if [[ -f "$f2source" ]]; then
|
||||||
|
. "$f2source"
|
||||||
|
continue
|
||||||
|
else
|
||||||
|
printf "%s: %s: %s\n"\
|
||||||
|
"$0" "$f2source" "No such file" >&2
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
;;
|
||||||
|
|
||||||
|
*) # Every arg that is not a flag or a option should be a file
|
||||||
|
files+=("$arg")
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
done
|
||||||
fi
|
fi
|
||||||
|
|
||||||
moGetContent moContent "$@"
|
moGetContent moContent "${files[@]}"
|
||||||
moParse "$moContent" "" true
|
moParse "$moContent" "" true
|
||||||
)
|
)
|
||||||
|
|
||||||
|
5
tests/source.expected
Normal file
5
tests/source.expected
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
value
|
||||||
|
* 1
|
||||||
|
* 2
|
||||||
|
* 3
|
||||||
|
AAA BBB
|
17
tests/source.sh
Executable file
17
tests/source.sh
Executable file
@ -0,0 +1,17 @@
|
|||||||
|
#!/usr/bin/env bash
|
||||||
|
|
||||||
|
cd "${0%/*}"
|
||||||
|
cat <<EOF | ../mo --source=source.vars
|
||||||
|
{{VAR}}
|
||||||
|
{{#ARR}}
|
||||||
|
* {{.}}
|
||||||
|
{{/ARR}}
|
||||||
|
{{ASSOC_ARR.a}} {{ASSOC_ARR.b}}
|
||||||
|
EOF
|
||||||
|
|
||||||
|
|
||||||
|
# Prints the string should mo NOT fail. Meaning that the output will not match
|
||||||
|
# tests/source.expected and therefore the test will fail.
|
||||||
|
|
||||||
|
../mo --source=a/non/existent/file files >/dev/null 2>&1
|
||||||
|
[[ "$?" -ne 1 ]] && echo "mo accepted a non existent file"
|
4
tests/source.vars
Normal file
4
tests/source.vars
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
export VAR=value
|
||||||
|
export ARR=(1 2 3)
|
||||||
|
declare -A ASSOC_ARR
|
||||||
|
export ASSOC_ARR=([a]=AAA [b]=BBB)
|
Loading…
Reference in New Issue
Block a user