mirror of
https://github.com/kvz/bash3boilerplate.git
synced 2024-12-18 22:27:51 +00:00
fix templater.sh and associated test
templater.sh now escapes the delimiter it uses for its sed magic within the environment variable content. the associated test uses a properly cleaned up temporary file instead of explicitly rm-ing its own mess.
This commit is contained in:
parent
2463cd6cc9
commit
f57e37eb68
2
.gitignore
vendored
2
.gitignore
vendored
@ -1,8 +1,6 @@
|
||||
npm-debug.log
|
||||
node_modules
|
||||
|
||||
test/scenario/templater/break.cfg
|
||||
|
||||
website/index.md
|
||||
website/_site/
|
||||
website/_vendor/
|
||||
|
@ -41,7 +41,7 @@ function templater() {
|
||||
|
||||
cp -f "${templateSrc}" "${templateDst}"
|
||||
for var in $(env |awk -F= '{print $1}' |grep -E '^(_[A-Z0-9_]+|[A-Z0-9][A-Z0-9_]*)$'); do
|
||||
sed -i.bak -e "s#\${${var}}#${!var}#g" "${templateDst}"
|
||||
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
|
||||
|
@ -5,12 +5,18 @@ set -o nounset
|
||||
# set -o xtrace
|
||||
|
||||
__dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||||
__file="${__dir}/$(basename "${BASH_SOURCE[0]}")"
|
||||
__base="$(basename "${__file}" .sh)"
|
||||
|
||||
__root="$(cd "$(dirname "$(dirname "$(dirname "${__dir}")")")" && pwd)"
|
||||
|
||||
__templaterTmpFile=$(mktemp "${TMPDIR:-/tmp}/${__base}.XXXXXX")
|
||||
function cleanup_before_exit () { rm "${__templaterTmpFile:?}"; }
|
||||
trap cleanup_before_exit EXIT
|
||||
|
||||
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
|
||||
env TARGET_HOST="127.0.0.1" bash "${__root}/src/templater.sh" ./app.template.cfg "${__templaterTmpFile}"
|
||||
cat "${__templaterTmpFile}"
|
||||
|
||||
echo "--"
|
||||
export TARGET_HOST="127.0.0.1"
|
||||
@ -18,16 +24,13 @@ export TARGET_HOST="127.0.0.1"
|
||||
# shellcheck source=src/templater.sh
|
||||
source "${__root}/src/templater.sh"
|
||||
|
||||
templater ./app.template.cfg ./app.cfg
|
||||
cat app.cfg
|
||||
rm -f app.cfg
|
||||
templater ./app.template.cfg "${__templaterTmpFile}"
|
||||
cat "${__templaterTmpFile}"
|
||||
|
||||
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
|
||||
env ALLOW_REMAINDERS="1" TARGET_HOST="127.0.0.1" bash "${__root}/src/templater.sh" ./break.template.cfg "${__templaterTmpFile}"
|
||||
cat "${__templaterTmpFile}"
|
||||
|
||||
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
|
||||
env TARGET_HOST="127.0.0.1" bash "${__root}/src/templater.sh" ./break.template.cfg "${__templaterTmpFile}"
|
||||
cat "${__templaterTmpFile}"
|
||||
|
Loading…
Reference in New Issue
Block a user