dump-env takes an .env.template file and some optional environmental variables to create a new .env file from these two sources. No external dependencies are used.
Why?
Why do we need such a tool? Well, this tool is very helpful when your CI is building docker (or other) images.
Previously we had some complex logic of encrypting and decrypting files, importing secret keys and so on.
Now we can just create secret variables for our CI, add some prefix to it, and use dump-env to make our life easier.
Installation
$ pip install dump-env
Quickstart
This quick demo will demonstrate the main and the only purpose of dump-env:
This command will do pretty much the same thing as with one prefix. But, it will replace multiple prefixes.
Further prefixes always replace previous ones if they are the same.
For example:
Any number of --strict flags can be provided.
No more forgotten template overrides or missing env vars!
Source templates
You can use an env template as a source template by using the -s or --source argument. This will restrict any non-prefixed variables found in the environment to only those already defined in your template.
Values are expanded once, in definition order. You need to define variables before referencing them.
A reference to an unknown variable is kept as a literal ${VAR}, and $$ produces a literal $.
$VAR and ${VAR} are equivalent - they both expand the variable VAR.
Braces are required when the text directly after the reference would otherwise be read as part
of the variable name:
${HOST}_SUFFIX expands HOST and appends _SUFFIX,
while $HOST_SUFFIX is a reference to a single variable named HOST_SUFFIX.
Templates need no quoting: ${VAR} is read from the file as-is.
For shell exports, the quoting picks who expands the reference:
$ export DB_URL="postgresql://${DB_HOST}:5432/mydb" # the shell expands it at export time
$ export DB_URL='postgresql://${DB_HOST}:5432/mydb' # dump-env expands it when it runs
Add --strict-interpolate to fail on undefined or malformed references: