项目作者: wemake-services

项目描述 :
A utility tool to create .env files
高级语言: Python
项目地址: git://github.com/wemake-services/dump-env.git
创建时间: 2017-12-18T18:03:41Z
项目社区:https://github.com/wemake-services/dump-env

开源协议:MIT License

下载


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

  1. $ pip install dump-env

Quickstart

This quick demo will demonstrate the main and the only purpose of dump-env:

  1. $ 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

  1. $ 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:

  1. $ export SECRET_TOKEN='very secret string'
  2. $ export SECRET_ANSWER='13'
  3. $ export ANOTHER_SECRET_ENV_ANSWER='42'
  4. $ export ANOTHER_SECRET_ENV_VALUE='0'
  5. $ dump-env -p SECRET_ -p ANOTHER_SECRET_ENV_
  6. ANSWER=42
  7. TOKEN=very secret string
  8. 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:

  1. $ dump-env --strict YOUR_VAR -p YOUR_
  2. Missing env vars: YOUR_VAR

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

  1. $ export YOUR_VAR='abc'
  2. $ dump-env --strict YOUR_VAR -p YOUR_
  3. 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.

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

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

  1. $ export EXTRA_VAR='foo'
  2. $ dump-env -s template.env -p EXTRA_
  3. ANSWER=13
  4. TOKEN=very secret string
  5. VALUE=0
  6. 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.

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

Creating secret variables in some CIs

Real-world usages

Projects that use this tool in production:

You might also be interested in:

License

MIT