dotenv.plugin.zsh 898 B

1234567891011121314151617181920212223242526272829303132333435
  1. source_env() {
  2. if [[ -f $ZSH_DOTENV_FILE ]]; then
  3. if [ "$ZSH_DOTENV_PROMPT" != "false" ]; then
  4. # confirm before sourcing file
  5. local confirmation
  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) "
  8. read -k 1 confirmation; [[ "$confirmation" != $'\n' ]] && echo
  9. # only bail out if confirmation character is n
  10. if [[ "$confirmation" = [nN] ]]; then
  11. return
  12. fi
  13. fi
  14. # test .env syntax
  15. zsh -fn $ZSH_DOTENV_FILE || echo "dotenv: error when sourcing '$ZSH_DOTENV_FILE' file" >&2
  16. if [[ -o a ]]; then
  17. source $ZSH_DOTENV_FILE
  18. else
  19. set -a
  20. source $ZSH_DOTENV_FILE
  21. set +a
  22. fi
  23. fi
  24. }
  25. autoload -U add-zsh-hook
  26. add-zsh-hook chpwd source_env
  27. if [[ -z $ZSH_DOTENV_FILE ]]; then
  28. ZSH_DOTENV_FILE=.env
  29. fi
  30. source_env