]> src.twobees.de Git - dotfiles.git/blob - stow/oh-my-zsh/.oh-my-zsh/plugins/dotenv/dotenv.plugin.zsh
46cd4b10ab400f5697c3a101aa52d686812ecab5
[dotfiles.git] / stow / oh-my-zsh / .oh-my-zsh / plugins / dotenv / dotenv.plugin.zsh
1 ## Settings
2
3 # Filename of the dotenv file to look for
4 : ${ZSH_DOTENV_FILE:=.env}
5
6 # Path to the file containing allowed paths
7 : ${ZSH_DOTENV_ALLOWED_LIST:="${ZSH_CACHE_DIR:-$ZSH/cache}/dotenv-allowed.list"}
8 : ${ZSH_DOTENV_DISALLOWED_LIST:="${ZSH_CACHE_DIR:-$ZSH/cache}/dotenv-disallowed.list"}
9
10
11 ## Functions
12
13 source_env() {
14   if [[ ! -f "$ZSH_DOTENV_FILE" ]]; then
15     return
16   fi
17
18   if [[ "$ZSH_DOTENV_PROMPT" != false ]]; then
19     local confirmation dirpath="${PWD:A}"
20
21     # make sure there is an (dis-)allowed file
22     touch "$ZSH_DOTENV_ALLOWED_LIST"
23     touch "$ZSH_DOTENV_DISALLOWED_LIST"
24
25     # early return if disallowed
26     if command grep -Fx -q "$dirpath" "$ZSH_DOTENV_DISALLOWED_LIST" &>/dev/null; then
27       return
28     fi
29
30     # check if current directory's .env file is allowed or ask for confirmation
31     if ! command grep -Fx -q "$dirpath" "$ZSH_DOTENV_ALLOWED_LIST" &>/dev/null; then
32       # get cursor column and print new line before prompt if not at line beginning
33       local column
34       echo -ne "\e[6n" > /dev/tty
35       read -t 1 -s -d R column < /dev/tty
36       column="${column##*\[*;}"
37       [[ $column -eq 1 ]] || echo
38
39       # print same-line prompt and output newline character if necessary
40       echo -n "dotenv: found '$ZSH_DOTENV_FILE' file. Source it? ([Y]es/[n]o/[a]lways/n[e]ver) "
41       read -k 1 confirmation
42       [[ "$confirmation" = $'\n' ]] || echo
43
44       # check input
45       case "$confirmation" in
46         [nN]) return ;;
47         [aA]) echo "$dirpath" >> "$ZSH_DOTENV_ALLOWED_LIST" ;;
48         [eE]) echo "$dirpath" >> "$ZSH_DOTENV_DISALLOWED_LIST"; return ;;
49         *) ;; # interpret anything else as a yes
50       esac
51     fi
52   fi
53
54   # test .env syntax
55   zsh -fn $ZSH_DOTENV_FILE || {
56     echo "dotenv: error when sourcing '$ZSH_DOTENV_FILE' file" >&2
57     return 1
58   }
59
60   setopt localoptions allexport
61   source $ZSH_DOTENV_FILE
62 }
63
64 autoload -U add-zsh-hook
65 add-zsh-hook chpwd source_env
66
67 source_env