-
Notifications
You must be signed in to change notification settings - Fork 22
/
autoenv.plugin.zsh
executable file
·104 lines (90 loc) · 2.6 KB
/
autoenv.plugin.zsh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#!/usr/bin/env zsh
if [[ -z $AUTOENV_AUTH_FILE ]]; then
AUTOENV_AUTH_FILE=~/.autoenv_authorized
fi
if [[ -z $COLORS ]]; then
COLORS=true
fi
if [[ -z $AUTOENV_IN_FILE ]]; then
AUTOENV_IN_FILE=".env"
fi
if [[ -z $AUTOENV_OUT_FILE ]]; then
AUTOENV_OUT_FILE=".out"
fi
check_and_run(){
if [[ $COLORS == true ]]; then
echo -e "$fg_no_bold[green]> $fg_no_bold[red]WARNING$reset_color"
echo -e "$fg_no_bold[green]> $fg_no_bold[blue]This is the first time you are about to source $fg_no_bold[yellow]\"$fg_bold[red]$1$fg_no_bold[yellow]\"$reset_color"
echo
echo -e "$fg_no_bold[green]----------------$reset_color"
if hash pygmentize 2>/dev/null; then
echo
`whence pygmentize` -f 256 -l shell -g "$1"
else
echo -e "$fg_no_bold[green]"
cat $1
fi
echo
echo -e "$fg_no_bold[green]----------------$reset_color"
echo
echo -ne "$fg_no_bold[blue]Are you sure you want to allow this? $fg[cyan]($fg_no_bold[green]y$fg[cyan]/$fg_no_bold[red]N$fg[cyan]) $reset_color"
else
echo "> WARNING"
echo "> This is the first time you are about to source \"$1\""
echo
echo "----------------"
echo
cat $1
echo
echo "----------------"
echo
echo -n "Are you sure you want to allow this? (y/N)"
fi
read answer
if [[ "$answer" == "y" ]] || [[ "$answer" == "Y" ]]; then
echo "$1:$2" >> $AUTOENV_AUTH_FILE
envfile=$1
shift
source $envfile
fi
}
check_and_exec(){
if which shasum &> /dev/null; then
hash=$(shasum "$1" | cut -d' ' -f 1)
else
hash=$(sha1sum "$1" | cut -d' ' -f 1)
fi
if grep --quiet "$1:$hash" "$AUTOENV_AUTH_FILE"; then
envfile=$1
shift
source $envfile
else
check_and_run $1 $hash
fi
}
autoenv_chdir(){
_AUTOENV_OLDPATH="$OLDPWD/"
_AUTOENV_NEWPATH="$(pwd)/"
while [[ ! "$_AUTOENV_NEWPATH" == "$_AUTOENV_OLDPATH"* ]]; do
if [[ -f "$_AUTOENV_OLDPATH/$AUTOENV_OUT_FILE" ]]; then
check_and_exec "$_AUTOENV_OLDPATH/$AUTOENV_OUT_FILE"
fi
_AUTOENV_OLDPATH="$(dirname $_AUTOENV_OLDPATH)"
done
if [[ $_AUTOENV_OLDPATH == '/' ]]; then
_AUTOENV_OLDPATH=''
fi
while [[ ! "$_AUTOENV_OLDPATH" == "$_AUTOENV_NEWPATH" ]]; do
_AUTOENV_OLDPATH="$_AUTOENV_OLDPATH$(echo -n '/'; echo ${_AUTOENV_NEWPATH#${_AUTOENV_OLDPATH}} | tr \/ "\n" | sed -n '2p' )"
if [[ -f "$_AUTOENV_OLDPATH/$AUTOENV_IN_FILE" ]]; then
check_and_exec "$_AUTOENV_OLDPATH/$AUTOENV_IN_FILE"
fi
done
}
_autoenv_first_run(){
local OLDPWD=''
autoenv_chdir
precmd_functions=(${precmd_functions#_autoenv_first_run})
}
precmd_functions+=(_autoenv_first_run)
chpwd_functions+=(autoenv_chdir)