malud / temgo Goto Github PK
View Code? Open in Web Editor NEWEnvironment variable based template engine like Jinja2 for your container config files. Lightweight, fast, go cli
License: MIT License
Environment variable based template engine like Jinja2 for your container config files. Lightweight, fast, go cli
License: MIT License
tg
is a nice replacement for sed
when moving ENV vars to config files in docker entrypoint scripts!
however, the tg < template > file
semantics forces me to use a separate template file. to keep things easy to read, i would prefer an inline editing mode like sed -i
:
tg -i my.conf
the command should be idempotent to avoid changes to the file when the docker container is restarted. ideally, subsequent runs would not write the file at all (because no placeholder was found).
When the input stream has no placeholders, tg
generates no output:
Expected:
$ echo foo bar | ./tg
foo bar
Actual:
$ echo foo bar | ./tg
With correct placeholders, it works as expected:
$ echo 'foo {{ x }}bar' | ./tg
foo bar
However, incomplete placeholders show the erroneous behaviour, too:
$ echo 'foo {{ }}bar' | ./tg
$ echo 'foo {{x}}bar' | ./tg
$ echo 'foo {{x }}bar' | ./tg
I want to propose a strict mode that makes tg
fail if a placeholder cannot be replaced.
$ echo '{{ NOT_SET }}' | tg -s
Error: placeholder NOT_SET cannot be replaced
$ echo $?
1
My use case is as usual a docker container's entrypoint. I use tg
to fill env vars into configuration files. The configuration file may be broken if a placeholder is not replaced. In the entrypoint I have to check that every env variable used in placeholders is set. This is error prone, because I have at least two places to get it wrong.
With tg -s
I would be able to let my set -e
entrypoint fail without repeating the variable list.
The stdout variant tg -s > file
will empty the file because bash opens the redirection before the command is executed.
But in inplace mode tg -s -i file
the output file should not be written at all, if a variable is missing.
I would like to define default values for replacements:
KEY={{ VALUE : default }}
especially for docker setups, where tg
is used from the entrypoint, I imagine this to be more robust than defining all necessary env vars in docker-compose.yml
. this would also eliminate the need to check for all variables from the tg
called (entrypoint).
The other nice effect is, that default values can be defined in the target file.
Jinja defaults are quite cumbersome: {{ first_name|default("walter", true) }
. therefore my proposal is more bash-oriented, where a default can be written as ${NAME:-walter}
. the colon could have arbitrary whitespaces (see above) or we could define {{ VALUE:default }}
. it doesn't really matter.
However, it would be handy to allow whitespaces in the default value without the need for quoting:
TITLE={{ PAGE_TITLE : Good news! }}
For processing I would suggest to read everything behind the colon and trim whitespaces.
@malud, what do you think?
This feature would be quite useful for saving a lot of code (depends on the number of variables). Currently I do something like this:
set -o errexit
set -o unset
[[ -z ${HTTP_SERVER} ]] && :
[[ -z ${HTTPS_SERVER} ]] && :
[[ -z ${DB_HOSTNAME} ]] && :
[[ -z ${DB_USERNAME} ]] && :
[[ -z ${DB_PASSWORD} ]] && :
[[ -z ${DB_DATABASE} ]] && :
[[ -z ${DB_PORT} ]] && :
[[ -z ${DB_PREFIX} ]] && :
[[ -z ${ADMIN_HTTP_SERVER} ]] && :
[[ -z ${ADMIN_HTTP_CATALOG} ]] && :
[[ -z ${ADMIN_HTTPS_SERVER} ]] && :
[[ -z ${ADMIN_HTTPS_CATALOG} ]] && :
tg -i /app/config.php
If temgo
failed when a variable is not set, let's say with the -f
switch (force/fail), the above would look like this:
set -o errexit
tg -f -i /app/config.php
The savings on nerves and typing would be enormous ;)
Cheers
The os envs are parsed naively with a strings.Split("=")
which leads to wrong value replacements for e.g. a URI with query params.
The Env value https://example.com/?query=param
gets mapped to [key]=https://example.com/?query"
.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.