forked from hyperupcall/autoenv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
activate.sh
executable file
·171 lines (156 loc) · 3.99 KB
/
activate.sh
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
AUTOENV_AUTH_FILE="${AUTOENV_AUTH_FILE-$HOME/.autoenv_authorized}"
AUTOENV_ENV_FILENAME="${AUTOENV_ENV_FILENAME-.env}"
autoenv_init() {
local _mountpoint _files _orderedfiles
if [ "${OSTYPE#darwin*}" != "${OSTYPE}" ]; then
_mountpoint="`df "${PWD}" | awk 'END{print $NF}'`"
else
_mountpoint="`stat -c '%m' \"${PWD}\"`"
fi
# Discover all files we need to source
# We do this in a subshell so we can cd/chdir
_files="`
_hadone=''
while [ "${PWD}" != "${_mountpoint}" ]; do
_file="${PWD}/${AUTOENV_ENV_FILENAME}"
if [ -f "${_file}" ]; then
if [ -z "${_hadone}" ]; then
echo -n ${_file}
_hadone='1'
else
echo -n "
${_file}"
fi
fi
command -v chdir >/dev/null 2>&1 && chdir .. || builtin cd ..
done
`"
# ZSH: Use traditional for loop
zsh_shwordsplit="$( setopt > /dev/null 2>&1 | grep -q shwordsplit && echo 1 )"
if [ -z "${zsh_shwordsplit}" ]; then
setopt shwordsplit >/dev/null 2>&1
fi
# Custom IFS
origIFS="${IFS}"
IFS='
'
# Turn around the env files order if needed
_orderedfiles=''
if [ -z "${AUTOENV_LOWER_FIRST}" ]; then
for _file in ${_files}; do
_orderedfiles="${_file}
${_orderedfiles}"
done
else
_orderedfiles="${_files}"
fi
# Execute the env files
for _file in ${_orderedfiles}; do
autoenv_check_authz_and_run "${_file}"
done
IFS="${origIFS}"
# ZSH: Unset shwordsplit
if [ -z "${zsh_shwordsplit}" ]; then
unsetopt shwordsplit >/dev/null 2>&1
fi
}
autoenv_hashline() {
local _envfile _hash
_envfile="${1}"
_hash=$(autoenv_shasum "${_envfile}" | cut -d' ' -f 1)
echo "${_envfile}:${_hash}"
}
autoenv_check_authz() {
local _envfile _hash
_envfile="${1}"
_hash=$(autoenv_hashline "${_envfile}")
touch "${AUTOENV_AUTH_FILE}"
\grep -Gq "${_hash}" "${AUTOENV_AUTH_FILE}"
}
autoenv_check_authz_and_run() {
local _envfile
_envfile="${1}"
if autoenv_check_authz "${_envfile}"; then
autoenv_source "${_envfile}"
return 0
fi
if [ -z "${MC_SID}" ]; then # Make sure mc is not running
echo "autoenv:"
echo "autoenv: WARNING:"
echo "autoenv: This is the first time you are about to source ${_envfile}":
echo "autoenv:"
echo "autoenv: --- (begin contents) ---------------------------------------"
cat -e "${_envfile}" | sed 's/.*/autoenv: &/'
echo "autoenv:"
echo "autoenv: --- (end contents) -----------------------------------------"
echo "autoenv:"
printf "%s" "autoenv: Are you sure you want to allow this? (y/N) "
read answer
if [ "${answer}" = "y" ] || [ "${answer}" = "Y" ]; then
autoenv_authorize_env "${_envfile}"
autoenv_source "${_envfile}"
fi
fi
}
autoenv_deauthorize_env() {
local _envfile _noclobber
_envfile="${1}"
\cp "${AUTOENV_AUTH_FILE}" "${AUTOENV_AUTH_FILE}.tmp"
_noclobber="$(set +o | \grep noclobber)"
set +C
\grep -Gv "${_envfile}:" "${AUTOENV_AUTH_FILE}.tmp" > "${AUTOENV_AUTH_FILE}"
eval "${_noclobber}"
rm "${AUTOENV_AUTH_FILE}.tmp" 2>/dev/null || :
}
autoenv_authorize_env() {
local _envfile
_envfile="${1}"
autoenv_deauthorize_env "${_envfile}"
autoenv_hashline "${_envfile}" >> "${AUTOENV_AUTH_FILE}"
}
autoenv_source() {
local _allexport
_allexport="$(set +o | \grep allexport)"
set -a
AUTOENV_CUR_FILE="${1}"
AUTOENV_CUR_DIR="$(dirname \"${1}\")"
. "${1}"
[ "${ZSH_VERSION#*5.1}" != "${ZSH_VERSION}" ] && set +a
eval "${_allexport}"
unset AUTOENV_CUR_FILE AUTOENV_CUR_DIR
}
autoenv_cd() {
command -v chdir >/dev/null 2>&1 && chdir "${@}" || builtin cd "${@}"
if [ "${?}" -eq 0 ]; then
autoenv_init
return 0
else
return "${?}"
fi
}
# Override the cd alias
enable_autoenv() {
cd() {
autoenv_cd "${@}"
}
cd "${PWD}"
}
# Probe to see if we have access to a shasum command, otherwise disable autoenv
if which gsha1sum 2>/dev/null >&2 ; then
autoenv_shasum() {
gsha1sum "${@}"
}
enable_autoenv
elif which sha1sum 2>/dev/null >&2; then
autoenv_shasum() {
sha1sum "${@}"
}
enable_autoenv
elif which shasum 2>/dev/null >&2; then
autoenv_shasum() {
shasum "${@}"
}
enable_autoenv
else
echo "autoenv: can not locate a compatible shasum binary; not enabling"
fi