dump-env


Namedump-env JSON
Version 1.5.0 PyPI version JSON
download
home_pagehttps://dump-env.readthedocs.io
SummaryA utility tool to create .env files
upload_time2024-03-22 11:32:53
maintainerNone
docs_urlNone
authorNikita Sobolev
requires_python<4.0,>=3.9
licenseMIT
keywords dotenv .env tempaltes secrets ci/cd
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # A utility tool to create ``.env`` files

[![wemake.services](https://img.shields.io/badge/%20-wemake.services-green.svg?label=%20&logo=data%3Aimage%2Fpng%3Bbase64%2CiVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAMAAAAoLQ9TAAAABGdBTUEAALGPC%2FxhBQAAAAFzUkdCAK7OHOkAAAAbUExURQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAP%2F%2F%2F5TvxDIAAAAIdFJOUwAjRA8xXANAL%2Bv0SAAAADNJREFUGNNjYCAIOJjRBdBFWMkVQeGzcHAwksJnAPPZGOGAASzPzAEHEGVsLExQwE7YswCb7AFZSF3bbAAAAABJRU5ErkJggg%3D%3D)](https://wemake-services.github.io)
[![test](https://github.com/wemake-services/dump-env/workflows/test/badge.svg?branch=master&event=push)](https://github.com/wemake-services/dump-env/actions?query=workflow%3Atest)
[![codecov](https://codecov.io/gh/wemake-services/dump-env/branch/master/graph/badge.svg)](https://codecov.io/gh/wemake-services/dump-env)
[![Python Version](https://img.shields.io/pypi/pyversions/dump-env.svg)](https://pypi.org/project/dump-env/)
[![Docs](https://readthedocs.org/projects/dump-env/badge/?version=latest)](http://dump-env.readthedocs.io/en/latest/?badge=latest) [![wemake-python-styleguide](https://img.shields.io/badge/style-wemake-000000.svg)](https://github.com/wemake-services/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](https://github.com/wemake-services/wemake-django-template/blob/6a7ab060e8435fd855cd806706c5d1b5a9e76d12/%7B%7Bcookiecutter.project_name%7D%7D/.gitlab-ci.yml#L25) 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

```bash
$ pip install dump-env
```


## Quickstart

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

```bash
$ 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

```bash
$ 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:

```bash
$ 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:

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

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

```bash
$ 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.

```bash
$ cat template.env
ANSWER=13
TOKEN=very secret string
VALUE=0
```

```bash
$ 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

```bash
$ 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.

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

## Creating secret variables in some CIs

- [travis docs](https://docs.travis-ci.com/user/environment-variables/#Defining-encrypted-variables-in-.travis.yml)
- [gitlab-ci docs](https://docs.gitlab.com/ce/ci/variables/README.html#secret-variables)
- [github actions](https://help.github.com/en/articles/virtual-environments-for-github-actions#creating-and-using-secrets-encrypted-variables)


## Real-world usages

Projects that use this tool in production:

- [wemake-django-template](https://github.com/wemake-services/wemake-django-template/blob/master/%7B%7Bcookiecutter.project_name%7D%7D/.gitlab-ci.yml#L24)
- [wemake-vue-template](https://github.com/wemake-services/wemake-vue-template/blob/master/template/.gitlab-ci.yml#L24)


## Related

You might also be interested in:

- <https://github.com/wemake-services/dotenv-linter>


## License

[MIT](https://github.com/wemake-services/dump-env/blob/master/LICENSE)

            

Raw data

            {
    "_id": null,
    "home_page": "https://dump-env.readthedocs.io",
    "name": "dump-env",
    "maintainer": null,
    "docs_url": null,
    "requires_python": "<4.0,>=3.9",
    "maintainer_email": null,
    "keywords": "dotenv, .env, tempaltes, secrets, CI/CD",
    "author": "Nikita Sobolev",
    "author_email": "mail@sobolevn.me",
    "download_url": "https://files.pythonhosted.org/packages/e2/21/4e8ea024b996c09e4de448c55555b5eb3e9f2726a84c338ec4d34233fecb/dump_env-1.5.0.tar.gz",
    "platform": null,
    "description": "# A utility tool to create ``.env`` files\n\n[![wemake.services](https://img.shields.io/badge/%20-wemake.services-green.svg?label=%20&logo=data%3Aimage%2Fpng%3Bbase64%2CiVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAMAAAAoLQ9TAAAABGdBTUEAALGPC%2FxhBQAAAAFzUkdCAK7OHOkAAAAbUExURQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAP%2F%2F%2F5TvxDIAAAAIdFJOUwAjRA8xXANAL%2Bv0SAAAADNJREFUGNNjYCAIOJjRBdBFWMkVQeGzcHAwksJnAPPZGOGAASzPzAEHEGVsLExQwE7YswCb7AFZSF3bbAAAAABJRU5ErkJggg%3D%3D)](https://wemake-services.github.io)\n[![test](https://github.com/wemake-services/dump-env/workflows/test/badge.svg?branch=master&event=push)](https://github.com/wemake-services/dump-env/actions?query=workflow%3Atest)\n[![codecov](https://codecov.io/gh/wemake-services/dump-env/branch/master/graph/badge.svg)](https://codecov.io/gh/wemake-services/dump-env)\n[![Python Version](https://img.shields.io/pypi/pyversions/dump-env.svg)](https://pypi.org/project/dump-env/)\n[![Docs](https://readthedocs.org/projects/dump-env/badge/?version=latest)](http://dump-env.readthedocs.io/en/latest/?badge=latest) [![wemake-python-styleguide](https://img.shields.io/badge/style-wemake-000000.svg)](https://github.com/wemake-services/wemake-python-styleguide)\n\n`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.\n\n\n## Why?\n\nWhy do we need such a tool? Well, this tool is very helpful when your CI is building `docker` (or other) images.\n[Previously](https://github.com/wemake-services/wemake-django-template/blob/6a7ab060e8435fd855cd806706c5d1b5a9e76d12/%7B%7Bcookiecutter.project_name%7D%7D/.gitlab-ci.yml#L25) we had some complex logic of encrypting and decrypting files, importing secret keys and so on.\nNow we can just create secret variables for our CI, add some prefix to it, and use `dump-env` to make our life easier.\n\n\n## Installation\n\n```bash\n$ pip install dump-env\n```\n\n\n## Quickstart\n\nThis quick demo will demonstrate the main and the only purpose of `dump-env`:\n\n```bash\n$ dump-env --template=.env.template --prefix='SECRET_ENV_' > .env\n```\n\nThis command will:\n\n1. take `.env.template`\n2. parse its keys and values\n3. read all the variables from the environment starting with `SECRET_ENV_`\n4. remove this prefix\n5. mix it all together, environment vars may override ones from the template\n6. sort keys in alphabetic order\n7. dump all the keys and values into the `.env` file\n\n\n## Advanced Usage\n\n### Multiple prefixes\n\n```bash\n$ dump-env -t .env.template -p 'SECRET_ENV_' -p 'ANOTHER_SECRET_ENV_' > .env\n```\n\nThis command will do pretty much the same thing as with one prefix. But, it will replace multiple prefixes.\nFurther prefixes always replace previous ones if they are the same.\nFor example:\n\n```bash\n$ export SECRET_TOKEN='very secret string'\n$ export SECRET_ANSWER='13'\n$ export ANOTHER_SECRET_ENV_ANSWER='42'\n$ export ANOTHER_SECRET_ENV_VALUE='0'\n$ dump-env -p SECRET_ -p ANOTHER_SECRET_ENV_\nANSWER=42\nTOKEN=very secret string\nVALUE=0\n```\n\n### Strict env variables\n\nIn case you want to be sure that `YOUR_VAR` exists\nin your environment when dumping, you can use `--strict` flag:\n\n```bash\n$ dump-env --strict YOUR_VAR -p YOUR_\nMissing env vars: YOUR_VAR\n```\n\nOups! We forgot to create it! Now this will work:\n\n```bash\n$ export YOUR_VAR='abc'\n$ dump-env --strict YOUR_VAR -p YOUR_\nVAR=abc\n```\n\nAny number of `--strict` flags can be provided.\nNo more forgotten template overrides or missing env vars!\n\n### Source templates\n\nYou 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.\n\n```bash\n$ cat template.env\nANSWER=13\nTOKEN=very secret string\nVALUE=0\n```\n\n```bash\n$ export ANSWER='42'\n$ dump-env --source=template.env\nANSWER=42\nTOKEN=very secret string\nVALUE=0\n```\n\nYou can still also use prefixes to add extra variables from the environment\n\n```bash\n$ export EXTRA_VAR='foo'\n$ dump-env -s template.env -p EXTRA_\nANSWER=13\nTOKEN=very secret string\nVALUE=0\nVAR=foo\n```\n\n#### Strict Source\n\nUsing the `--strict-source` flag has the same effect as defining a `--strict` flag for every variable defined in the source template.\n\n```bash\n$ export ANSWER='42'\n$ dump-env -s template.env --strict-source\nMissing env vars: TOKEN, VALUE\n```\n\n## Creating secret variables in some CIs\n\n- [travis docs](https://docs.travis-ci.com/user/environment-variables/#Defining-encrypted-variables-in-.travis.yml)\n- [gitlab-ci docs](https://docs.gitlab.com/ce/ci/variables/README.html#secret-variables)\n- [github actions](https://help.github.com/en/articles/virtual-environments-for-github-actions#creating-and-using-secrets-encrypted-variables)\n\n\n## Real-world usages\n\nProjects that use this tool in production:\n\n- [wemake-django-template](https://github.com/wemake-services/wemake-django-template/blob/master/%7B%7Bcookiecutter.project_name%7D%7D/.gitlab-ci.yml#L24)\n- [wemake-vue-template](https://github.com/wemake-services/wemake-vue-template/blob/master/template/.gitlab-ci.yml#L24)\n\n\n## Related\n\nYou might also be interested in:\n\n- <https://github.com/wemake-services/dotenv-linter>\n\n\n## License\n\n[MIT](https://github.com/wemake-services/dump-env/blob/master/LICENSE)\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "A utility tool to create .env files",
    "version": "1.5.0",
    "project_urls": {
        "Homepage": "https://dump-env.readthedocs.io",
        "Repository": "https://github.com/wemake-services/dump-env"
    },
    "split_keywords": [
        "dotenv",
        " .env",
        " tempaltes",
        " secrets",
        " ci/cd"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "5c8f70ba1d21126209681cf151985f2c669a8d7c3fbc13bc94ac8cb554294806",
                "md5": "11470ed39ad78a91ecfb07c6612a2591",
                "sha256": "daf05283bcf5eb2cc7c740907800947ea258278614c19d20d2b170373dde68e6"
            },
            "downloads": -1,
            "filename": "dump_env-1.5.0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "11470ed39ad78a91ecfb07c6612a2591",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": "<4.0,>=3.9",
            "size": 7279,
            "upload_time": "2024-03-22T11:32:51",
            "upload_time_iso_8601": "2024-03-22T11:32:51.903305Z",
            "url": "https://files.pythonhosted.org/packages/5c/8f/70ba1d21126209681cf151985f2c669a8d7c3fbc13bc94ac8cb554294806/dump_env-1.5.0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "e2214e8ea024b996c09e4de448c55555b5eb3e9f2726a84c338ec4d34233fecb",
                "md5": "78a7cce998c8bda075ad445c58726ebd",
                "sha256": "11f05ceec5a76f26e916c51da80f2e07ce4d69fb160710e46169e550c503e9e4"
            },
            "downloads": -1,
            "filename": "dump_env-1.5.0.tar.gz",
            "has_sig": false,
            "md5_digest": "78a7cce998c8bda075ad445c58726ebd",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": "<4.0,>=3.9",
            "size": 6261,
            "upload_time": "2024-03-22T11:32:53",
            "upload_time_iso_8601": "2024-03-22T11:32:53.426688Z",
            "url": "https://files.pythonhosted.org/packages/e2/21/4e8ea024b996c09e4de448c55555b5eb3e9f2726a84c338ec4d34233fecb/dump_env-1.5.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-03-22 11:32:53",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "wemake-services",
    "github_project": "dump-env",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "dump-env"
}
        
Elapsed time: 0.21138s