dotenv.plugin.zsh 664 Bytes
Newer Older
Arthur's avatar
Arthur committed
1
source_env() {
2
  if [[ -f $ZSH_DOTENV_FILE ]]; then
3
4
5
6
7
8
9
    # confirm before sourcing .env file
    local confirmation
    echo -n "dotenv: source '$ZSH_DOTENV_FILE' file in the directory? (Y/n) "
    if read -k 1 confirmation && [[ $confirmation = [nN] ]]; then
      return
    fi

10
    # test .env syntax
11
    zsh -fn $ZSH_DOTENV_FILE || echo "dotenv: error when sourcing '$ZSH_DOTENV_FILE' file" >&2
12

13
    if [[ -o a ]]; then
14
      source $ZSH_DOTENV_FILE
15
16
    else
      set -a
17
      source $ZSH_DOTENV_FILE
18
19
      set +a
    fi
Arthur's avatar
Arthur committed
20
21
22
23
24
  fi
}

autoload -U add-zsh-hook
add-zsh-hook chpwd source_env
25

26
27
28
29
if [[ -z $ZSH_DOTENV_FILE ]]; then
    ZSH_DOTENV_FILE=.env
fi

30
source_env