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
|
npm-debug.log
|
||||||
node_modules
|
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)
|
### 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 `ini_val` and follow Library export best practices
|
||||||
- Add tests for `parse_url` 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
|
#!/usr/bin/env bash
|
||||||
# Copyright (c) 2014, Transloadit Ltd.
|
# BASH3 Boilerplate: templater
|
||||||
#
|
#
|
||||||
# This file:
|
# This file:
|
||||||
# - takes a source (template) & destination (config) filepath argument
|
# - 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
|
# ALLOW_REMAINDERS=1 templater.sh input.cfg output.cfg
|
||||||
#
|
#
|
||||||
# Authors:
|
# Licensed under MIT
|
||||||
# - Kevin van Zonneveld <kevin@transloadit.com>
|
# Copyright (c) 2016 Kevin van Zonneveld (http://kvz.io)
|
||||||
|
|
||||||
set -o pipefail
|
function templater() {
|
||||||
set -o errexit
|
sed=""
|
||||||
# set -o xtrace
|
[ -n "$(which sed)" ] && sed="$(which sed)"
|
||||||
# set -o nounset
|
[ -n "$(which gsed)" ] && sed="$(which gsed)"
|
||||||
|
|
||||||
sed=""
|
ALLOW_REMAINDERS="${ALLOW_REMAINDERS:-0}"
|
||||||
[ -n "$(which sed)" ] && sed="$(which sed)"
|
|
||||||
[ -n "$(which gsed)" ] && sed="$(which gsed)"
|
|
||||||
|
|
||||||
ALLOW_REMAINDERS="${ALLOW_REMAINDERS:-0}"
|
templateSrc="${1:-}"
|
||||||
|
templateDst="${2:-}"
|
||||||
|
|
||||||
templateSrc="${1}"
|
if [ ! -f "${templateSrc}" ]; then
|
||||||
templateDst="${2}"
|
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
|
cp -f "${templateSrc}" "${templateDst}"
|
||||||
echo "ERROR: Template source '${templateSrc}' needs to exist"
|
for var in $(env |awk -F= '{print $1}' |egrep '^[A-Z0-9_]+$'); do
|
||||||
exit 1
|
${sed} -i.bak -e "s#\${${var}}#${!var}#g" "${templateDst}"
|
||||||
fi
|
# this .bak dance is done for BSD/GNU portability: http://stackoverflow.com/a/22084103/151666
|
||||||
if [ ! -n "${templateDst}" ]; then
|
rm -f "${templateDst}.bak"
|
||||||
echo "ERROR: Template destination '${templateDst}' needs to exist"
|
done
|
||||||
exit 1
|
|
||||||
fi
|
# cat "${templateDst}"
|
||||||
|
|
||||||
cp -f "${templateSrc}" "${templateDst}"
|
if grep '${' ${templateDst} && [ "${ALLOW_REMAINDERS}" = "0" ]; then
|
||||||
for var in $(env |awk -F= '{print $1}' |egrep '^[A-Z0-9_]+$'); do
|
echo "ERROR: Unable to replace the above template vars"
|
||||||
${sed} "s#\${${var}}#${!var}#g" -i "${templateDst}"
|
exit 1
|
||||||
done
|
fi
|
||||||
|
}
|
||||||
# cat "${templateDst}"
|
|
||||||
|
if [ "${BASH_SOURCE[0]}" != ${0} ]; then
|
||||||
if grep '${' ${templateDst} && [ "${ALLOW_REMAINDERS}" == "0" ]; then
|
export -f templater
|
||||||
echo "ERROR: Unable to replace the above template vars"
|
else
|
||||||
exit 1
|
templater "${@}"
|
||||||
|
exit ${?}
|
||||||
fi
|
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