-
-
Notifications
You must be signed in to change notification settings - Fork 262
/
activate.sh
executable file
·386 lines (345 loc) · 9.57 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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
# shellcheck shell=sh
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_VIEWER:-cat}"
# AUTOENV_ENABLE_LEAVE
# @description print a user message to stdout
# @args
# -b[NUM]: number of lines to print before message
# -a[NUM]: number of lines to print after message (default=1)
# -n: do not print trailing newline (same as -a0)
# message: space seperated text of message
# @example _autoenv_info -n -b1 'my message'
# @internal
_autoenv_info() {
local after=1 before=0
while : ; do
case "$1" in
-n) after=0 ;;
-b*) before=${1#-b} ; : "${before:=1}" ;;
-a*) after=${1#-a} ; : "${after:=1}" ;;
*) break ;;
esac
shift
done
[ $before -gt 0 ] && printf '%*s' ${before} | tr " " "\n"
if [ -n "$NO_COLOR" ]; then
printf "[autoenv] %s" "${*}"
else
printf "\033[33m[autoenv]\033[0m %s" "${*}"
fi
[ $after -gt 0 ] && printf '%*s' ${after} | tr " " "\n"
}
# @description print a message to stderr
# @args
# message: space seperated text of message
# @example _autoenv_err 'there was an error'
# @internal
_autoenv_err() {
if [ -n "$NO_COLOR" ]; then
printf "[autoenv] Error %s" "${*}" >&2
else
printf "\033[33m[autoenv]\033[0m \033[31mError\033[0m %s\n" "${*}" >&2
fi
return 1
}
# @description print a horizontal line
# @args
# text: title to print near the beginning of the line
# @example _autoenv_draw_line 'text'
# @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} | tr " " "${char}")
if [ -n "$NO_COLOR" ]; then
printf "%s%s\n\n" "${text}" "$line"
else
printf "\033[1m%s%s\033[0m\n\n" "${text}" "$line"
fi
}
# @description display the contents of a `.env` or `.env.leave` file using the `$AUTOENV_VIEWER`` command
# @example _autoenv_show_file './.env_file'
# @internal
_autoenv_show_file() {
local file="$1" ofs="$IFS"
_autoenv_info -b "New or modified env file detected:"
_autoenv_draw_line "${file##*/} contents"
IFS=" "
$AUTOENV_VIEWER "${file}"
IFS="$ofs"
_autoenv_draw_line
}
# @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 hash
# @internal
autoenv_hashline() {
local _envfile _hash
_envfile="${1}"
_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}"
local _hash
_hash=$(autoenv_hashline "${_envfile}")
command mkdir -p -- "$(dirname "${AUTOENV_AUTH_FILE}")" "$(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
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
_autoenv_show_file "${_envfile}"
_autoenv_info -n "Authorize this file? (y/N/D) "
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
fi
}
# @description Mark an env file as able to be sourced
# @internal
autoenv_deauthorize_env() {
local _envfile _noclobber
_envfile="${1}"
command cp -- "${AUTOENV_AUTH_FILE}" "${AUTOENV_AUTH_FILE}.tmp"
_noclobber="$(set +o | command grep noclobber)"
set +C
command grep -Gv "${_envfile}:" -- "${AUTOENV_AUTH_FILE}.tmp" > "${AUTOENV_AUTH_FILE}"
eval "${_noclobber}"
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
_envfile="${1}"
autoenv_deauthorize_env "${_envfile}"
autoenv_hashline "${_envfile}" >> "${AUTOENV_AUTH_FILE}"
}
# @description Actually source a file
# @internal
autoenv_source() {
local _allexport
_allexport="$(set +o | command 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
}
# @description Function to override the 'cd' builtin
autoenv_cd() {
local _pwd
_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() {
# execute file when leaving a directory
local from_dir to_dir
from_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
}
# Override the cd alias
if command -v setopt >/dev/null 2>&1; then
if setopt 2> /dev/null | command grep -q aliasfuncdef; then
has_alias_func_def_enabled=true
else
setopt ALIAS_FUNC_DEF 2>/dev/null
fi
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 ! $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 >/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_err "can not locate a compatible shasum binary; not enabling"
fi