mirror of
https://github.com/kvz/bash3boilerplate.git
synced 2025-01-30 15:13:49 +00:00
Add tests for templater
and follow Library export best practices
This commit is contained in:
parent
707583a93c
commit
7fbf9ea8e5
2
.gitignore
vendored
2
.gitignore
vendored
@ -1,2 +1,4 @@
|
||||
npm-debug.log
|
||||
node_modules
|
||||
|
||||
test/scenario/templater/break.cfg
|
||||
|
@ -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
|
||||
|
||||
|
@ -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
|
||||
|
1
test/fixture/templater.exitcode
Normal file
1
test/fixture/templater.exitcode
Normal file
@ -0,0 +1 @@
|
||||
1
|
14
test/fixture/templater.stdio
Normal file
14
test/fixture/templater.stdio
Normal 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
|
2
test/scenario/templater/app.template.cfg
Normal file
2
test/scenario/templater/app.template.cfg
Normal file
@ -0,0 +1,2 @@
|
||||
[connection]
|
||||
host = ${TARGET_HOST}
|
3
test/scenario/templater/break.template.cfg
Normal file
3
test/scenario/templater/break.template.cfg
Normal file
@ -0,0 +1,3 @@
|
||||
[connection]
|
||||
host = ${TARGET_HOST}
|
||||
port = ${I_DONT_EXIST}
|
33
test/scenario/templater/run.sh
Normal file
33
test/scenario/templater/run.sh
Normal 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
|
Loading…
x
Reference in New Issue
Block a user