-
-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
Showing
10 changed files
with
537 additions
and
154 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,52 +1,85 @@ | ||
#!/usr/bin/env python | ||
# -*- coding: utf-8 -*- | ||
|
||
import argparse | ||
import sys | ||
from typing import NoReturn | ||
|
||
from dump_env.dumper import dump | ||
from dump_env.exceptions import StrictEnvException | ||
|
||
|
||
def _create_parser() -> argparse.ArgumentParser: | ||
parser = argparse.ArgumentParser() | ||
parser.add_argument( | ||
'-p', | ||
'--prefix', | ||
type=str, | ||
action='append', | ||
help='Adds prefix', | ||
) | ||
parser.add_argument( | ||
'-t', | ||
'--template', | ||
default='', | ||
type=str, | ||
help='Adds template path', | ||
) | ||
parser.add_argument('-p', '--prefix', action='append', help='Adds prefix') | ||
parser.add_argument( | ||
'--strict', | ||
type=str, | ||
action='append', | ||
help='Strict variables should exists in os envs', | ||
) | ||
return parser | ||
|
||
|
||
def main() -> None: | ||
def main() -> NoReturn: | ||
""" | ||
Runs dump-env script. | ||
Examples: | ||
This example will dump all environ variables:: | ||
Example: | ||
This example will dump all environ variables: | ||
.. code:: bash | ||
$ dump-env | ||
This example will dump all environ variables starting with `PIP_`:: | ||
This example will dump all environ variables starting with ``PIP_``: | ||
.. code:: bash | ||
$ dump-env -p 'PIP_' | ||
This example will dump all environ variables starting with `PIP_` | ||
and update them with variables starting with `SECRET_`:: | ||
This example will dump all environ variables starting with ``PIP_`` | ||
and update them with variables starting with ``SECRET_``: | ||
.. code:: bash | ||
$ dump-env -p 'PIP_' -p 'SECRET_' | ||
This example will dump everything from `.env.template` file | ||
and all env variables with `SECRET_` prefix into a `.env` file:: | ||
This example will dump everything from ``.env.template`` file | ||
and all env variables with ``SECRET_`` prefix into a ``.env`` file: | ||
.. code:: bash | ||
$ dump-env -p 'SECRET_' -t .env.template > .env | ||
""" | ||
parser = _create_parser() | ||
args = parser.parse_args() | ||
variables = dump(args.template, args.prefix) | ||
This example will fail if ``REQUIRED`` does not exist in environ: | ||
for env_name, env_value in variables.items(): | ||
sys.stdout.write('{0}={1}'.format(env_name, env_value) + '\n') | ||
.. code:: bash | ||
$ dump-env --strict=REQUIRED | ||
""" | ||
args = _create_parser().parse_args() | ||
strict_vars = set(args.strict) if args.strict else None | ||
|
||
try: | ||
variables = dump(args.template, args.prefix, strict_vars) | ||
except StrictEnvException as exc: | ||
sys.stdout.write('{0}\n'.format(str(exc))) | ||
sys.exit(1) | ||
else: | ||
for env_name, env_value in variables.items(): | ||
sys.stdout.write('{0}={1}\n'.format(env_name, env_value)) | ||
sys.exit(0) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
|
||
class StrictEnvException(Exception): | ||
"""We use this exception when some env vars are missing from environ.""" |
Oops, something went wrong.