-
Notifications
You must be signed in to change notification settings - Fork 7
/
exe.inc.sh
232 lines (194 loc) · 6.25 KB
/
exe.inc.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
#!/usr/bin/env bash
set -euo pipefail
# ECHO -------------------------------------------------------------------------
YP_CI_ECHO_BENCHMARK=${YP_CI_ECHO_BENCHMARK:-/dev/null}
export YP_CI_ECHO="${YP_DIR}/bin/ci-echo --benchmark ${YP_CI_ECHO_BENCHMARK}"
function echo_do() {
${YP_CI_ECHO} -- "[DO ]" "$@"
}
# shellcheck disable=SC2120
function echo_done() {
${YP_CI_ECHO} -- "[DONE]" "$@"
}
function echo_indent() {
${YP_CI_ECHO} -- " " "$@"
}
function echo_next() {
${YP_CI_ECHO} -- "[NEXT]" "$@"
}
function echo_q() {
${YP_CI_ECHO} -- "[Q ]" "$@"
}
function echo_skip() {
${YP_CI_ECHO} -- "[SKIP]" "$@"
}
# ECHO -------------------------------------------------------------------------
function echo_err() {
${YP_CI_ECHO} -- "[ERR ]" "$@"
}
function echo_info() {
${YP_CI_ECHO} -- "[INFO]" "$@"
}
function echo_warn() {
${YP_CI_ECHO} -- "[WARN]" "$@"
}
# SHELL ------------------------------------------------------------------------
function sh_script_usage() {
grep "^##" "${0}" | cut -c 4- | if command -v envsubst >/dev/null 2>&1; then envsubst; else cat; fi
# return 1
exit 1
}
function sh_script_version() {
grep "^#-" "${0}" | cut -c 4- | if command -v envsubst >/dev/null 2>&1; then envsubst; else cat; fi
# return 1
exit 1
}
function sh_shellopts() {
# usage: OLDOPTS="$(sh_shellopts)"; ...; sh_shellopts_restore "${OLDOPTS}"
# see https://unix.stackexchange.com/a/383581/61053
# see https://unix.stackexchange.com/a/476710/61053
local OLDOPTS="$(set +o)"
# bash resets errexit inside sub-shells like $(set +o) above
[[ ! -o errexit ]] || OLDOPTS="${OLDOPTS}; set -e"
if [[ -n "${BASH_VERSION:-}" ]]; then
# bash-specific options
OLDOPTS="${OLDOPTS}; $(shopt -p)"
elif [[ -n "${ZSH_VERSION:-}" ]]; then
# zsh-specific options
OLDOPTS="${OLDOPTS}; setopt $(setopt); unsetopt $(unsetopt)"
fi
return "${OLDOPTS}"
}
function sh_shellopts_restore() {
set +vx
eval "$1"
}
# EXE --------------------------------------------------------------------------
function exe() {
local PS_MARKER="\$"
[[ "${EUID}" != "0" ]] || PS_MARKER="#"
>&2 echo "$(pwd)${PS_MARKER} $*"
"$@"
}
function exe_and_grep_q() {
local CMD="$1"
shift
local EXPECTED_STDOUT="$1"
shift
local EXECUTABLE="$(echo "${CMD}" | cut -d" " -f1)"
local EXECUTABLE_TYPE="$(type -t ${EXECUTABLE} || echo "undefined")"
local CMD_STDOUT="command not found: ${EXECUTABLE}"
local CMD_STDERR="$(mktemp -t yplatform.XXXXXXXXXX)"
if [[ "${EXECUTABLE_TYPE}" != "undefined" ]]; then
CMD_STDOUT="$(eval "${CMD}" 2>${CMD_STDERR} || true)"
fi
if echo "${CMD_STDOUT}" | grep -q "${EXPECTED_STDOUT}"; then
echo_info "Command '${CMD}' with stdout '${CMD_STDOUT}' matches '${EXPECTED_STDOUT}'."
else
echo_err "Command '${CMD}' with stdout '${CMD_STDOUT}' failed to match '${EXPECTED_STDOUT}'."
echo_info "Command stderr: $(cat ${CMD_STDERR})"
echo_info "Command's executable info:"
>&2 debug_exe "${EXECUTABLE}"
rm -f ${CMD_STDERR}
return 1
fi
rm -f ${CMD_STDERR}
}
function if_exe_and_grep_q() {
local CMD="$1"
shift
local EXPECTED_STDOUT="$1"
shift
local EXECUTABLE=$(echo "$1" | cut -d" " -f1)
if exe_and_grep_q "${CMD}" "${EXPECTED_STDOUT}"; then
"$@"
hash -r
>&2 debug_exe "${EXECUTABLE}"
if exe_and_grep_q "${CMD}" "${EXPECTED_STDOUT}"; then
return 1
fi
else
>&2 echo_skip "$@"
fi
}
function unless_exe_and_grep_q() {
local CMD="$1"
shift
local EXPECTED_STDOUT="$1"
shift
local EXECUTABLE=$(echo "$1" | cut -d" " -f1)
if exe_and_grep_q "${CMD}" "${EXPECTED_STDOUT}"; then
>&2 echo_skip "$@"
else
"$@"
hash -r
>&2 debug_exe "${EXECUTABLE}"
exe_and_grep_q "${CMD}" "${EXPECTED_STDOUT}"
fi
}
function debug_exe() {
local EXECUTABLE="$1"
local EXECUTABLE_TYPE="$(type -t ${EXECUTABLE} || echo "undefined")"
if [[ "${EXECUTABLE_TYPE}" = "file" ]]; then
type -a ${EXECUTABLE}
type -a -p ${EXECUTABLE} | while read -r EXECUTABLE_PATH; do \
ls -l "${EXECUTABLE_PATH}"
done
type ${EXECUTABLE} || true
else
echo "PATH=${PATH}"
echo "${EXECUTABLE} is ${EXECUTABLE_TYPE}."
fi
}
# PRINTENV ---------------------------------------------------------------------
# list all shell variables' names, not just exported variables
function printenv_all_names() {
compgen -A variable
}
# list shell variables, not just exported variables
function printenv_all() {
local VARS="$*"
[[ $# -ne 0 ]] || VARS="$(printenv_all_names | grep -v "^VARS$")"
# not sure why, but printenv_all_names catches "undefined" vars e.g. BASH_ALIASES
local NOUNSET_STATE="$(set +o | grep nounset)"
set +u
for VAR in ${VARS}; do
echo "${VAR}=${!VAR}"
done
eval "${NOUNSET_STATE}"
unset NOUNSET_STATE
}
# MISC -------------------------------------------------------------------------
# - use with: cmd1 | cmd2 || exit_allow_sigpipe
# - use with: cmd1 | cmd2 || exit_allow_sigpipe "$?"
# - use with: cmd1 | cmd2 || exit_allow_sigpipe "${PIPESTATUS[@]}"
# - use with: cmd1 | cmd2 || exit_allow_sigpipe "$?" "${PIPESTATUS[@]}"
function exit_allow_sigpipe() {
local EXIT_STATUS=$?
[[ $# -ne 0 ]] || set -- "${EXIT_STATUS}"
for EXIT_STATUS in "$@"; do
[[ "${EXIT_STATUS}" = "0" ]] || \
[[ "${EXIT_STATUS}" = "141" ]] || \
return ${EXIT_STATUS}
done
return 0
}
function prompt_q_to_continue() {
local Q="${1:-Are you sure you want to continue?}"
local CANCEL_KEY="${2:-Ctrl-C}"
echo_q "${Q}"
echo_indent "Press ENTER to Continue."
echo_indent "Press ${CANCEL_KEY} to Cancel."
if [[ "${CI:-}" = "true" ]]; then
echo_info "CI pressed ENTER."
return 0
fi
read -r -p "" -n1
echo
[[ "${REPLY}" != "${CANCEL_KEY}" ]]
}
ANY_PYTHON=
ANY_PYTHON=${ANY_PYTHON:-$(command -v -p python3 2>/dev/null || true)}
ANY_PYTHON=${ANY_PYTHON:-$(command -v -p python2 2>/dev/null || true)}
ANY_PYTHON=${ANY_PYTHON:-$(command -v -p python 2>/dev/null || true)}
ANY_PYTHON=${ANY_PYTHON:-ANY_PYTHON_NOT_FOUND}