Skip to content

Instantly share code, notes, and snippets.

@netoht
Last active August 29, 2016 01:18
Show Gist options
  • Save netoht/5d4db9af7c8d558e4eaab64fe45e68c6 to your computer and use it in GitHub Desktop.
Save netoht/5d4db9af7c8d558e4eaab64fe45e68c6 to your computer and use it in GitHub Desktop.
Shell as a template engine
#!/bin/sh
render=true
debug=${debug:-false}
BASENAME=`basename "$0"`
usage() {
if [ ! "$1" = "usage" ] ; then
return
fi
echo "Usage: Render template shell
Variables default:
TEMPLATE - Template to render (required)
OUTPUT - Directory for output template redering (required)
debug - Active logs for debug
** All variables in template are required
Example:
$ TEMPLATE=your_template.tpl; cat \$TEMPLATE
User: \$VAR_NAME
Age: \${VAR_AGE}
City: \$VAR_CITY
Address: \\\$Street 1
ZIPCODE: \\\${50001}
$ TEMPLATE=your_template.tpl OUTPUT=output.txt \\
VAR_NAME=JONH \\
VAR_AGE=30 \\
VAR_CITY=\"SAO PAULO\" ./$BASENAME
$ OUTPUT=output.txt; cat \$OUTPUT
User: JONH
Age: 30
City: SAO PAULO
Address: \$Street 1
ZIPCODE: \${50001}
"
exit 0
}
log_message() {
local timestamp=""
if [ $debug = true ] ; then
timestamp=$(date +"[%Y-%m-%d %H:%M:%S]")
fi
echo "$timestamp ${1-}" >&2
shift
for message; do
echo " $message" >&2
done
}
validate_variable_template() {
local var_name=TEMPLATE
local var_value=$TEMPLATE
if [ -z $var_value ] ; then
log_message "!!! ERROR: Variable '${var_name}' is required to render template"
render=false
validate_template_render
fi
if [ ! -f $var_value ] ; then
log_message "!!! ERROR: Template '${var_value}' not exists"
render=false
validate_template_render
fi
}
validate_variable_output() {
local var_name=OUTPUT
local var_value=$OUTPUT
if [ -z $var_value ] ; then
log_message "!!! ERROR: Variable '${var_name}' is required to render template"
render=false
validate_template_render
fi
}
validate_template_variables() {
validate_template_render
local tpl=$TEMPLATE
local tpl_vars=$(grep -oE "[^\\](\\$|\\$\\{)\\w+}?" "${tpl}" | sed -E 's/([[:space:]]|\$|\{|\})//g')
for tpl_var in $tpl_vars ; do
var_value=$(eval echo "\$$tpl_var")
if [ "$var_value" = "" ] ; then
log_message "!!! ERROR: Template variable ${tpl_var}=NULL"
render=false
else
if [ $debug = true ] ; then
log_message "Template variable $tpl_var=$var_value"
fi
fi
done
}
validate_template_render() {
if [ $render = false ] ; then
log_message "!!! Error rendering template, all variables should be set correctly."
log_message "Help? ./$BASENAME usage"
exit 1
fi
}
prepare_output() {
local var_name=OUTPUT
local var_value=$OUTPUT
parentdir="$(dirname "$var_value")"
mkdir -p $parentdir 2> /dev/null
if [ $? = 1 ] ; then
log_message "!!! Error trying to create output directory: ${parentdir}"
render=false
fi
}
render_template() {
validate_template_render
local template=$TEMPLATE
local output=$OUTPUT
if [ $debug = true ] ; then
log_message "Processing template: ${template}"
fi
eval "echo \"$(cat $template)\"" > $output
if [ $debug = true ] ; then
log_message "Output template: ${output}"
fi
exit 0
}
usage $@
validate_variable_template
validate_variable_output
validate_template_variables
prepare_output
render_template
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment