dotenv.plugin.zsh 728 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
10

    if [ "$ZSH_DOTENV_PROMPT" != "false" ]; then
      # confirm before sourcing 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
11
12
    fi

13
    # test .env syntax
14
    zsh -fn $ZSH_DOTENV_FILE || echo "dotenv: error when sourcing '$ZSH_DOTENV_FILE' file" >&2
15

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

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

29
30
31
32
if [[ -z $ZSH_DOTENV_FILE ]]; then
    ZSH_DOTENV_FILE=.env
fi

33
source_env