Skip to main content

A utility tool to create .env files

Project description

A utility tool to create .env files

wemake.services test codecov Python Version Docs wemake-python-styleguide

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:

$ dump-env --template=.env.template --prefix='SECRET_ENV_' > .env

This command will:

  1. take .env.template
  2. parse its keys and values
  3. read all the variables from the environment starting with SECRET_ENV_
  4. remove this prefix
  5. mix it all together, environment vars may override ones from the template
  6. sort keys in alphabetic order
  7. dump all the keys and values into the .env file

Advanced Usage

Multiple prefixes

$ dump-env -t .env.template -p 'SECRET_ENV_' -p 'ANOTHER_SECRET_ENV_' > .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:

$ export SECRET_TOKEN='very secret string'
$ export SECRET_ANSWER='13'
$ export ANOTHER_SECRET_ENV_ANSWER='42'
$ export ANOTHER_SECRET_ENV_VALUE='0'
$ dump-env -p SECRET_ -p ANOTHER_SECRET_ENV_
ANSWER=42
TOKEN=very secret string
VALUE=0

Strict env variables

In case you want to be sure that YOUR_VAR exists in your environment when dumping, you can use --strict flag:

$ dump-env --strict YOUR_VAR -p YOUR_
Missing env vars: YOUR_VAR

Oups! We forgot to create it! Now this will work:

$ export YOUR_VAR='abc'
$ dump-env --strict YOUR_VAR -p YOUR_
VAR=abc

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.

$ cat template.env
ANSWER=13
TOKEN=very secret string
VALUE=0
$ export ANSWER='42'
$ dump-env --source=template.env
ANSWER=42
TOKEN=very secret string
VALUE=0

You can still also use prefixes to add extra variables from the environment

$ export EXTRA_VAR='foo'
$ dump-env -s template.env -p EXTRA_
ANSWER=13
TOKEN=very secret string
VALUE=0
VAR=foo

Strict Source

Using the --strict-source flag has the same effect as defining a --strict flag for every variable defined in the source template.

$ export ANSWER='42'
$ dump-env -s template.env --strict-source
Missing env vars: TOKEN, VALUE

Creating secret variables in some CIs

Real-world usages

Projects that use this tool in production:

Related

You might also be interested in:

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

dump_env-1.6.0.tar.gz (7.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

dump_env-1.6.0-py3-none-any.whl (7.6 kB view details)

Uploaded Python 3

File details

Details for the file dump_env-1.6.0.tar.gz.

File metadata

  • Download URL: dump_env-1.6.0.tar.gz
  • Upload date:
  • Size: 7.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.2 CPython/3.11.9 Darwin/23.5.0

File hashes

Hashes for dump_env-1.6.0.tar.gz
Algorithm Hash digest
SHA256 c67f357ce1cad8c42ff61ce9899459eb5dd23230bd791584dc5d1d94c1e1f262
MD5 f9d07906a864e67a86c2e9be2dfa8548
BLAKE2b-256 9506354fac7778266c28b397b26f8cd92b562256ee0b2b07960e8356f248da3d

See more details on using hashes here.

File details

Details for the file dump_env-1.6.0-py3-none-any.whl.

File metadata

  • Download URL: dump_env-1.6.0-py3-none-any.whl
  • Upload date:
  • Size: 7.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.2 CPython/3.11.9 Darwin/23.5.0

File hashes

Hashes for dump_env-1.6.0-py3-none-any.whl
Algorithm Hash digest
SHA256 5d3297bf263e298381e3a1b0d79eb7e4e15e8b617c830e62fd120a5ee9db8083
MD5 c1e75bc649096ceb00d2e27d24ab88e6
BLAKE2b-256 9740aa834b18df669bb178e3584ebd1955b4c1594481ccc6e6529366c3b3ab43

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page