Add tests for templater and follow Library export best practices

This commit is contained in:
Kevin van Zonneveld 2016-02-17 13:19:03 +01:00
parent 707583a93c
commit 7fbf9ea8e5
8 changed files with 110 additions and 34 deletions

2
.gitignore vendored
View File

@ -1,2 +1,4 @@
npm-debug.log
node_modules
test/scenario/templater/break.cfg

View File

@ -51,6 +51,7 @@ Although *3* introduces a node.js dependency, this does allow for easy version p
### master (Unreleased)
- Add tests for `templater` and follow Library export best practices
- Add tests for `ini_val` and follow Library export best practices
- Add tests for `parse_url` and follow Library export best practices

View File

@ -1,48 +1,68 @@
#!/usr/bin/env bash
# Copyright (c) 2014, Transloadit Ltd.
# BASH3 Boilerplate: templater
#
# This file:
# - takes a source (template) & destination (config) filepath argument
# - and then replaces placeholders with variables found in the environment
# - then replaces placeholders with variables found in the environment
#
# Run as:
# More info:
# - https://github.com/kvz/bash3boilerplate
# - http://kvz.io/blog/2013/02/26/introducing-bash3boilerplate/
#
# Version: 1.2.1
#
# Authors:
# - Kevin van Zonneveld (http://kvz.io)
#
# Usage as a function:
#
# source templater.sh
# NAME=kevin templater input.cfg output.cfg
#
# Usage as a command:
#
# ALLOW_REMAINDERS=1 templater.sh input.cfg output.cfg
#
# Authors:
# - Kevin van Zonneveld <kevin@transloadit.com>
# Licensed under MIT
# Copyright (c) 2016 Kevin van Zonneveld (http://kvz.io)
set -o pipefail
set -o errexit
# set -o xtrace
# set -o nounset
function templater() {
sed=""
[ -n "$(which sed)" ] && sed="$(which sed)"
[ -n "$(which gsed)" ] && sed="$(which gsed)"
sed=""
[ -n "$(which sed)" ] && sed="$(which sed)"
[ -n "$(which gsed)" ] && sed="$(which gsed)"
ALLOW_REMAINDERS="${ALLOW_REMAINDERS:-0}"
ALLOW_REMAINDERS="${ALLOW_REMAINDERS:-0}"
templateSrc="${1:-}"
templateDst="${2:-}"
templateSrc="${1}"
templateDst="${2}"
if [ ! -f "${templateSrc}" ]; then
echo "ERROR: Template source '${templateSrc}' needs to exist"
exit 1
fi
if [ ! -n "${templateDst}" ]; then
echo "ERROR: Template destination '${templateDst}' needs to be specified"
exit 1
fi
if [ ! -f "${templateSrc}" ]; then
echo "ERROR: Template source '${templateSrc}' needs to exist"
exit 1
fi
if [ ! -n "${templateDst}" ]; then
echo "ERROR: Template destination '${templateDst}' needs to exist"
exit 1
fi
cp -f "${templateSrc}" "${templateDst}"
for var in $(env |awk -F= '{print $1}' |egrep '^[A-Z0-9_]+$'); do
${sed} "s#\${${var}}#${!var}#g" -i "${templateDst}"
done
# cat "${templateDst}"
if grep '${' ${templateDst} && [ "${ALLOW_REMAINDERS}" == "0" ]; then
echo "ERROR: Unable to replace the above template vars"
exit 1
cp -f "${templateSrc}" "${templateDst}"
for var in $(env |awk -F= '{print $1}' |egrep '^[A-Z0-9_]+$'); do
${sed} -i.bak -e "s#\${${var}}#${!var}#g" "${templateDst}"
# this .bak dance is done for BSD/GNU portability: http://stackoverflow.com/a/22084103/151666
rm -f "${templateDst}.bak"
done
# cat "${templateDst}"
if grep '${' ${templateDst} && [ "${ALLOW_REMAINDERS}" = "0" ]; then
echo "ERROR: Unable to replace the above template vars"
exit 1
fi
}
if [ "${BASH_SOURCE[0]}" != ${0} ]; then
export -f templater
else
templater "${@}"
exit ${?}
fi

View File

@ -0,0 +1 @@
1

View File

@ -0,0 +1,14 @@
--
[connection]
host = 127.0.0.1
--
[connection]
host = 127.0.0.1
--
port = ${I_DONT_EXIST}
[connection]
host = 127.0.0.1
port = ${I_DONT_EXIST}
--
port = ${I_DONT_EXIST}
ERROR: Unable to replace the above template vars

View File

@ -0,0 +1,2 @@
[connection]
host = ${TARGET_HOST}

View File

@ -0,0 +1,3 @@
[connection]
host = ${TARGET_HOST}
port = ${I_DONT_EXIST}

View File

@ -0,0 +1,33 @@
#!/usr/bin/env bash
set -o pipefail
set -o errexit
set -o nounset
# set -o xtrace
# Set magic variables for current FILE & DIR
__dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
__file="${__dir}/$(basename "${BASH_SOURCE[0]}")"
__base="$(basename ${__file} .sh)"
__root="$(cd "$(dirname $(dirname $(dirname "${__dir}")))" && pwd)"
echo "--"
env TARGET_HOST="127.0.0.1" bash "${__root}/src/templater.sh" ./app.template.cfg ./app.cfg
cat app.cfg
rm -f app.cfg
echo "--"
export TARGET_HOST="127.0.0.1"
source ${__root}/src/templater.sh
templater ./app.template.cfg ./app.cfg
cat app.cfg
rm -f app.cfg
echo "--"
env ALLOW_REMAINDERS="1" TARGET_HOST="127.0.0.1" bash "${__root}/src/templater.sh" ./break.template.cfg ./break.cfg
cat break.cfg
rm -f break.cfg
echo "--"
env TARGET_HOST="127.0.0.1" bash "${__root}/src/templater.sh" ./break.template.cfg ./break.cfg
cat break.cfg
rm -f break.cfg