dotenv.plugin.zsh 664 B

123456789101112131415161718192021222324252627282930
  1. source_env() {
  2. if [[ -f $ZSH_DOTENV_FILE ]]; then
  3. # confirm before sourcing .env file
  4. local confirmation
  5. echo -n "dotenv: source '$ZSH_DOTENV_FILE' file in the directory? (Y/n) "
  6. if read -k 1 confirmation && [[ $confirmation = [nN] ]]; then
  7. return
  8. fi
  9. # test .env syntax
  10. zsh -fn $ZSH_DOTENV_FILE || echo "dotenv: error when sourcing '$ZSH_DOTENV_FILE' file" >&2
  11. if [[ -o a ]]; then
  12. source $ZSH_DOTENV_FILE
  13. else
  14. set -a
  15. source $ZSH_DOTENV_FILE
  16. set +a
  17. fi
  18. fi
  19. }
  20. autoload -U add-zsh-hook
  21. add-zsh-hook chpwd source_env
  22. if [[ -z $ZSH_DOTENV_FILE ]]; then
  23. ZSH_DOTENV_FILE=.env
  24. fi
  25. source_env