Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

easily allow sourcing .env-file in bash #45

Open
woutervh opened this issue Feb 19, 2020 · 1 comment
Open

easily allow sourcing .env-file in bash #45

woutervh opened this issue Feb 19, 2020 · 1 comment

Comments

@woutervh
Copy link

django-dotenv is a python-library, and used in python-code.
Currently .env-files can only contain:

  • key/value-pairs
  • blank lines
  • comments

But sometimes, env-vars are also used by executables outside your python-code.
E.g In a django-project with a postgresql-backend, I have these env-vars:

# postgres - used in django-settings + by postgres-executables (bin/pg_* )
PGHOSTADDR="127.0.0.1"
PGPORT="5432"
PGDATABASE="my-db"
PGUSER="my-dbadmin"
PGPASSWORD="SUPERSECRET"
PGOPTIONS=""

# used only by postgres-executables, 
# cfr. https://www.postgresql.org/docs/12.1/libpq-envars.html
PGDATA="..."
PGHOST="..."

I use following snippet to load all these key/value-pairs in my current bash-session
to use the postgresql-commands:
`

set -a && source .env && set +a
`

You can find many oneliners to source a .env-file in bash, many using grep/sed/xargs/...

If we would allow setting bash-options in the .env-file:

set -o allexport
PGHOSTADDR="127.0.0.1"
...
set -o allexport

these option-lines can just be skipped in the python-parsing of the dotenv

this would simplify to:

 > source .env

just source the .env!

@woutervh
Copy link
Author

this gives a syntax-warning right now:

elif not re.search(r'^\s*(?:#.*)?$', line):  # not comment or blank
            warnings.warn(
                "Line {0} doesn't match format".format(repr(line)),
                SyntaxWarning

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant