-
-
Notifications
You must be signed in to change notification settings - Fork 261
/
Copy pathactivate.sh
executable file
·371 lines (332 loc) · 9.45 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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
# shellcheck shell=sh
# shellcheck disable=SC2216,SC3043
if [ -n "${AUTOENV_AUTH_FILE:-}" ]; then
:
elif [ -f "$HOME/.autoenv_authorized" ]; then
AUTOENV_AUTH_FILE="$HOME/.autoenv_authorized"
else
_autoenv_state_dir="$XDG_STATE_HOME"
case $_autoenv_state_dir in
/*) AUTOENV_AUTH_FILE="$_autoenv_state_dir/autoenv/authorized_list" ;;
*) AUTOENV_AUTH_FILE="$HOME/.local/state/autoenv/authorized_list" ;;
esac
unset -v _autoenv_state_dir
fi
if [ -n "${AUTOENV_NOTAUTH_FILE:-}" ]; then
:
elif [ -f "$HOME/.autoenv_authorized" ]; then
# If `.autoenv_authorized` is in home, don't suprise the user by using XDG Base Dir.
AUTOENV_NOTAUTH_FILE="$HOME/.autoenv_not_authorized"
elif [ -f "$HOME/.autoenv_not_authorized" ]; then
# Ensure file in ~/ is used, even if the authorized file has been moved or deleted.
AUTOENV_NOTAUTH_FILE="$HOME/.autoenv_not_authorized"
else
_autoenv_state_dir="$XDG_STATE_HOME"
case $_autoenv_state_dir in
/*) AUTOENV_NOTAUTH_FILE="$_autoenv_state_dir/autoenv/not_authorized_list" ;;
*) AUTOENV_NOTAUTH_FILE="$HOME/.local/state/autoenv/not_authorized_list" ;;
esac
unset -v _autoenv_state_dir
fi
AUTOENV_ENV_FILENAME="${AUTOENV_ENV_FILENAME:-.env}"
AUTOENV_ENV_LEAVE_FILENAME="${AUTOENV_ENV_LEAVE_FILENAME:-.env.leave}"
: ${AUTOENV_VIEWER:=}
# AUTOENV_ENABLE_LEAVE
__autoenv_use_color() {
if [ ${NO_COLOR+x} ]; then
return 1
fi
case ${FORCE_COLOR:-} in
1|2|3) return 0 ;;
0) return 1 ;;
esac
if [ "$TERM" = 'dumb' ]; then
return 1
fi
if [ -t 1 ]; then
return 0
fi
return 1
}
# @description Prints a user message to standard output
# @internal
_autoenv_print() {
local title="${1}" color="${2}" text="${3}"
# shellcheck disable=SC2059
if __autoenv_use_color; then
\printf "\033[${color}m[${title}]\033[0m ${text}"
else
\printf "[${title}] ${text}"
fi
}
# @description Prints a horizontal line
# @args $1: title text to print near the beginning of the line
# @internal
_autoenv_draw_line() {
local text="${1:-}" char="-" width=${COLUMNS:-80} margin=3 line
if [ -n "${text}" ]; then
text="--- ${text} "
fi
width=$((width - ${#text} - margin))
line=$(\printf '%*s\n' ${width} | \command tr " " "${char}")
if __autoenv_use_color; then
\printf "\033[1m%s%s\033[0m\n" "${text}" "${line}"
else
\printf "%s%s\n" "${text}" "${line}"
fi
}
# @description Main initialization function
# @internal
autoenv_init() {
if [ -n "${AUTOENV_ENABLE_LEAVE:-}" ]; then
autoenv_leave "$@"
fi
local _mountpoint _pwd
_mountpoint="$(\command df -P "${PWD}" | \command tail -n 1 | \command awk '$0=$NF')"
_pwd=$(\echo "${PWD}" | \command sed -E 's:/+:/:g') # Removes double slashes. (see #125)
# Discover all files that we need to source.
local _files
_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
if [ -n "${ZSH_VERSION:-}" ]; then
\setopt shwordsplit >/dev/null 2>&1
fi
# Custom IFS
origIFS="${IFS}"
IFS='
'
\set -f
# Turn around the env files order if needed
local _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
\unset -v _orderedfiles
IFS="${origIFS}"
\set +f
# ZSH: Unset shwordsplit
if [ -n "${ZSH_VERSION:-}" ]; then
\unsetopt shwordsplit >/dev/null 2>&1
fi
}
# @description Checks the expected hash entry of the file
# @internal
autoenv_hashline() {
local _envfile="${1}" _hash
_hash=$(autoenv_shasum "${_envfile}" | \command cut -d' ' -f 1)
\printf '%s\n' "${_envfile}:${_hash}"
}
# @description Source an env file if is able to do so
# @internal
_autoenv_check_authz_and_run() {
local _envfile="${1}" _hash
_hash=$(autoenv_hashline "${_envfile}")
\command mkdir -p -- "$(\command dirname "${AUTOENV_AUTH_FILE}")" "$(\command dirname "${AUTOENV_NOTAUTH_FILE}")"
\command touch -- "${AUTOENV_AUTH_FILE}" "${AUTOENV_NOTAUTH_FILE}"
if \command grep -q "${_hash}" -- "${AUTOENV_AUTH_FILE}"; then
autoenv_source "${_envfile}"
\return 0
elif \command grep -q "${_hash}" -- "${AUTOENV_NOTAUTH_FILE}"; then
\return 0
fi
# Don't ask for permission if "assume yes" is switched on
if [ -n "${AUTOENV_ASSUME_YES:-}" ]; then
autoenv_authorize_env "${_envfile}"
autoenv_source "${_envfile}"
\return 0
fi
if [ -n "${MC_SID:-}" ]; then # Make sure mc is not running
\return 0
fi
if [ -z "$AUTOENV_VIEWER" ]; then
\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 \command sed 's/.*/autoenv: &/'
\echo "autoenv:"
\echo "autoenv: --- (end contents) -----------------------------------------"
\echo "autoenv:"
\printf "%s" "autoenv: Are you sure you want to allow this? (y/N/D) " # Keep (y/N/D) for old UI consistency.
else
_autoenv_print 'autoenv' 36 'New or modified env file detected\n'
_autoenv_draw_line "Contents of \"${_envfile##*/}\""
local ofs="${IFS}"
IFS=" "
$AUTOENV_VIEWER "${_envfile}"
IFS="${ofs}"
_autoenv_draw_line
_autoenv_print 'autoenv' 36 "Authorize this file? (y/n/d) "
fi
\read -r answer
if [ "${answer}" = "y" ] || [ "${answer}" = "Y" ]; then
autoenv_authorize_env "${_envfile}"
autoenv_source "${_envfile}"
elif [ "${answer}" = "d" ] || [ "${answer}" = "D" ]; then
autoenv_unauthorize_env "${_envfile}"
fi
}
# @description Mark an env file as able to be sourced
# @internal
autoenv_deauthorize_env() {
local _envfile="${1}"
\command cp -- "${AUTOENV_AUTH_FILE}" "${AUTOENV_AUTH_FILE}.tmp"
\command grep -Gv "${_envfile}:" -- "${AUTOENV_AUTH_FILE}.tmp" >| "${AUTOENV_AUTH_FILE}"
\command rm -- "${AUTOENV_AUTH_FILE}.tmp" 2>/dev/null || :
}
# @description Mark an env file as not able to be sourced
# @internal
autoenv_unauthorize_env() {
local _envfile="$1"
autoenv_deauthorize_env "$_envfile"
autoenv_hashline "$_envfile" >> "$AUTOENV_NOTAUTH_FILE"
}
# @description Mark an env file as able to be sourced
# @internal
autoenv_authorize_env() {
local _envfile="${1}"
autoenv_deauthorize_env "${_envfile}"
autoenv_hashline "${_envfile}" >> "${AUTOENV_AUTH_FILE}"
}
# @description Actually source a file
# @internal
autoenv_source() {
AUTOENV_CUR_DIR="$(\command dirname "${1}")"
export AUTOENV_CUR_FILE="${1}" AUTOENV_CUR_DIR
case $- in
*a*) ;;
*) \set -a; local __autoenv_set_allexport=yes ;;
esac
# shellcheck disable=SC1090
. "${1}"
if [ "${__autoenv_set_allexport:-}" = 'yes' ]; then
\set +a
fi
\unset -v AUTOENV_CUR_FILE AUTOENV_CUR_DIR
}
# @description Function to override the 'cd' builtin
autoenv_cd() {
local _pwd="${PWD}"
if \command -v chdir >/dev/null 2>&1 && \chdir "${@}" || \builtin cd "${@}"; then
autoenv_init "${_pwd}"
\return 0
else
\return "${?}"
fi
}
# @description Cleanup autoenv
autoenv_leave() {
local from_dir="${*}" to_dir
to_dir=$(\echo "${PWD}" | \command sed -E 's:/+:/:g')
# Discover all files that we need to source.
local _files
_files=$(
\command -v chdir >/dev/null 2>&1 && \chdir "${from_dir}" || \builtin cd "${from_dir}"
_hadone=''
while [ "$PWD" != "" ] && [ "$PWD" != "/" ]; do
case $to_dir/ in
$PWD/*)
\break
;;
*)
_file="$PWD/${AUTOENV_ENV_LEAVE_FILENAME}"
if [ -f "${_file}" ]; then
if [ -z "${_hadone}" ]; then
\printf %s "${_file}"
_hadone='1'
else
\printf %s "
${_file}"
fi
fi
\command -v chdir >/dev/null 2>&1 && \chdir "$(\pwd)/.." || \builtin cd "$PWD/.."
;;
esac
done
)
# ZSH: Use traditional for loop
if [ -n "${ZSH_VERSION:-}" ]; then
\setopt shwordsplit >/dev/null 2>&1
fi
# Custom IFS
origIFS="${IFS}"
IFS='
'
# Execute the env files
\set -f
for _file in ${_files}; do
_autoenv_check_authz_and_run "${_file}"
done
IFS="${origIFS}"
\set +f
# ZSH: Unset shwordsplit
if [ -n "${ZSH_VERSION:-}" ]; then
\unsetopt shwordsplit >/dev/null 2>&1
fi
}
# Set Zsh option to prevent errors when "cd" is already an alias.
# shellcheck disable=SC3010
if [ -n "${ZSH_VERSION:-}" ] && [[ ! -o aliasfuncdef ]]; then
__autoenv_unset_aliasfuncdef=yes
\setopt ALIAS_FUNC_DEF 2>/dev/null
fi
# @description Run to automatically replace the cd builtin with our improved one
enable_autoenv() {
if [ -z "${AUTOENV_PRESERVE_CD:-}" ]; then
cd() {
autoenv_cd "${@}"
}
fi
cd "${PWD}"
}
if [ "${__autoenv_unset_aliasfuncdef:-}" = 'yes' ]; then
\unsetopt ALIAS_FUNC_DEF 2>/dev/null
\unset -v __autoenv_unset_aliasfuncdef
fi
# If some shasum exists, specifically use it. Otherwise, do not enable autoenv.
if \command -v gsha1sum >/dev/null 2>&1; then
autoenv_shasum() {
gsha1sum "${@}"
}
enable_autoenv "$@"
elif \command -v sha1sum >/dev/null 2>&1; then
autoenv_shasum() {
sha1sum "${@}"
}
enable_autoenv "$@"
elif \command -v shasum >/dev/null 2>&1; then
autoenv_shasum() {
shasum "${@}"
}
enable_autoenv "$@"
else
_autoenv_print 'autoenv error' 31 "can not locate a compatible shasum binary; not enabling\n" >&2
fi