Skip to content

Commit

Permalink
dotenv: add support for custom env file names (#7861)
Browse files Browse the repository at this point in the history
  • Loading branch information
arshadkazmi42 authored and mcornella committed May 19, 2019
1 parent 5f7666e commit f960e2b
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
11 changes: 11 additions & 0 deletions plugins/dotenv/README.md
Expand Up @@ -32,6 +32,17 @@ PORT=3001
```
You can even mix both formats, although it's probably a bad idea.

### ZSH_DOTENV_FILE

You can also modify the name of the file to be loaded with the variable `ZSH_DOTENV_FILE`.
If the variable isn't set, the plugin will default to use `.env`.
For example, this will make the plugin look for files named `.dotenv` and load them:

```
# in ~/.zshrc, before Oh My Zsh is sourced:
ZSH_DOTENV_FILE=.dotenv
```

## Version Control

**It's strongly recommended to add `.env` file to `.gitignore`**, because usually it contains sensitive information such as your credentials, secret keys, passwords etc. You don't want to commit this file, it's supposed to be local only.
Expand Down
12 changes: 8 additions & 4 deletions plugins/dotenv/dotenv.plugin.zsh
@@ -1,13 +1,13 @@
source_env() {
if [[ -f .env ]]; then
if [[ -f $ZSH_DOTENV_FILE ]]; then
# test .env syntax
zsh -fn .env || echo 'dotenv: error when sourcing `.env` file' >&2
zsh -fn $ZSH_DOTENV_FILE || echo "dotenv: error when sourcing '$ZSH_DOTENV_FILE' file" >&2

if [[ -o a ]]; then
source .env
source $ZSH_DOTENV_FILE
else
set -a
source .env
source $ZSH_DOTENV_FILE
set +a
fi
fi
Expand All @@ -16,4 +16,8 @@ source_env() {
autoload -U add-zsh-hook
add-zsh-hook chpwd source_env

if [[ -z $ZSH_DOTENV_FILE ]]; then
ZSH_DOTENV_FILE=.env
fi

source_env

0 comments on commit f960e2b

Please sign in to comment.