forked from hyperupcall/autoenv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathactivate.sh
executable file
·210 lines (189 loc) · 5.16 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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
AUTOENV_AUTH_FILE="${AUTOENV_AUTH_FILE:-$HOME/.autoenv_authorized}"
AUTOENV_ENV_FILENAME="${AUTOENV_ENV_FILENAME:-.env}"
AUTOENV_ENV_LEAVE_FILENAME="${AUTOENV_ENV_LEAVE_FILENAME:-.env.leave}"
# AUTOENV_ENABLE_LEAVE
autoenv_init() {
if [ -n "$AUTOENV_ENABLE_LEAVE" ]; then
autoenv_leave "$@"
fi
local _mountpoint _files _orderedfiles _sedregexp _pwd
_sedregexp='-E'
_mountpoint="$(df -P "${PWD}" | tail -n 1 | awk '$0=$NF')"
# Remove double slashes, see #125
_pwd=$(\echo "${PWD}" | \sed "${_sedregexp}" 's:/+:/:g')
# Discover all files we need to source
# We do this in a subshell so we can cd/chdir
_files=$(
\command -v chdir >/dev/null 2>&1 && \chdir "${_pwd}" || builtin cd "${_pwd}"
_hadone=''
while :; do
_file="$(\pwd -P)/${AUTOENV_ENV_FILENAME}"
if [ -f "${_file}" ]; then
if [ -z "${_hadone}" ]; then
\printf %s "${_file}"
_hadone='1'
else
\printf %s "
${_file}"
fi
fi
[ "$(\pwd -P)" = "${_mountpoint}" ] && \break
[ "$(\pwd -P)" = "/" ] && \break
\command -v chdir >/dev/null 2>&1 && \chdir "$(\pwd -P)/.." || builtin cd "$(pwd -P)/.."
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='
'
# Disable file globbing
set -f
# 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}"
# Enable file globbing
set +f
# 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)
\printf '%s\n' "${_envfile}:${_hash}"
}
autoenv_check_authz() {
local _envfile _hash
_envfile="${1}"
_hash=$(autoenv_hashline "${_envfile}")
\touch -- "${AUTOENV_AUTH_FILE}"
\grep -q "${_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 [ -n "${AUTOENV_ASSUME_YES}" ]; then # Don't ask for permission if "assume yes" is switched on
autoenv_authorize_env "${_envfile}"
autoenv_source "${_envfile}"
\return 0
fi
if [ -z "${MC_SID}" ]; then # Make sure mc is not running
\echo "autoenv:"
\echo "autoenv: WARNING:"
\printf '%s\n' "autoenv: This is the first time you are about to source ${_envfile}":
\echo "autoenv:"
\echo "autoenv: --- (begin contents) ---------------------------------------"
\cat -e "${_envfile}" | LC_ALL=C \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() {
local _pwd
_pwd=${PWD}
\command -v chdir >/dev/null 2>&1 && \chdir "${@}" || builtin cd "${@}"
if [ "${?}" -eq 0 ]; then
autoenv_init "${_pwd}"
\return 0
else
\return "${?}"
fi
}
autoenv_leave() {
# execute file when leaving a directory
local target_file dir
dir="${@}"
target_file="${dir}/${AUTOENV_ENV_LEAVE_FILENAME}"
[ -f "${target_file}" ] && autoenv_check_authz_and_run "${target_file}"
}
# Override the cd alias
if setopt 2> /dev/null | grep -q aliasfuncdef; then
has_alias_func_def_enabled=true;
else
setopt ALIAS_FUNC_DEF 2> /dev/null
fi
enable_autoenv() {
cd() {
autoenv_cd "${@}"
}
cd "${PWD}"
}
if ! $has_alias_func_def_enabled; then
unsetopt ALIAS_FUNC_DEF 2> /dev/null
fi
# Probe to see if we have access to a shasum command, otherwise disable autoenv
if command -v gsha1sum 2>/dev/null >&2 ; then
autoenv_shasum() {
gsha1sum "${@}"
}
enable_autoenv
elif command -v sha1sum 2>/dev/null >&2; then
autoenv_shasum() {
sha1sum "${@}"
}
enable_autoenv
elif command -v shasum 2>/dev/null >&2; then
autoenv_shasum() {
shasum "${@}"
}
enable_autoenv
else
\echo "autoenv: can not locate a compatible shasum binary; not enabling"
fi