dotenv.plugin.zsh 898 Bytes
Newer Older
Arthur's avatar
Arthur committed
1
source_env() {
2
  if [[ -f $ZSH_DOTENV_FILE ]]; then
3
4
5
    if [ "$ZSH_DOTENV_PROMPT" != "false" ]; then
      # confirm before sourcing file
      local confirmation
Marc Cornellà's avatar
Marc Cornellà committed
6
      # print same-line prompt and output newline character if necessary
7
      echo -n "dotenv: source '$ZSH_DOTENV_FILE' file in the directory? (Y/n) "
Marc Cornellà's avatar
Marc Cornellà committed
8
9
10
      read -k 1 confirmation; [[ "$confirmation" != $'\n' ]] && echo
      # only bail out if confirmation character is n
      if [[ "$confirmation" = [nN] ]]; then
11
12
        return
      fi
13
14
    fi

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

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

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

31
32
33
34
if [[ -z $ZSH_DOTENV_FILE ]]; then
    ZSH_DOTENV_FILE=.env
fi

35
source_env