-
Notifications
You must be signed in to change notification settings - Fork 7
/
ci-echo
executable file
·386 lines (348 loc) · 12.6 KB
/
ci-echo
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
#!/usr/bin/env bash
set -euo pipefail
YP_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." >/dev/null && pwd)"
source ${YP_DIR}/sh/common.inc.sh
#- ci-echo 1.0
## Usage: ci-echo [OPTION] -- STEP TEXT...
## Echo message with a STEP indicator for stdout or for TeamCity/Travis consumption.
##
## --benchmark Benchmark file to use for benchmarking groups (DO-DONE durations).
## --external Path to external logger.
## Defaults to ci-echo-external-honeycomb if
## both CI_ECHO_EXTERNAL_HONEYCOMB_API_KEY and
## and CI_ECHO_EXTERNAL_HONEYCOMB_DATASET are set.
##
## -h, --help Display this help and exit
## -v, --version Output version information and exit
BENCHMARK=/dev/null
EXTERNAL=
[[ "${CI:-}" != "true" ]] || \
[[ -z "${YP_CI_ECHO_EXTERNAL_HONEYCOMB_API_KEY:-}" ]] || \
[[ -z "${YP_CI_ECHO_EXTERNAL_HONEYCOMB_DATASET:-}" ]] || \
EXTERNAL=${YP_DIR}/bin/ci-echo-external-honeycomb
if { getopt --test >/dev/null 2>&1 && false; } || [[ "$?" = "4" ]] || false; then
ARGS=$(getopt -o hv -l help,version,benchmark:,external: -n $(basename ${BASH_SOURCE[0]}) -- "$@") || sh_script_usage
eval set -- "${ARGS}"
fi
function timestamp_nano() {
local TIMESTAMP_NANO=$(date +"%s%N")
echo ${TIMESTAMP_NANO}
}
# if not GNU date, then do best effort
[[ $(date +"%N") != "N" ]] || {
# shellcheck disable=SC1091
YP_NANOSECONDS_VSN=$(source ${YP_DIR}/CONST.inc && echo "${YP_NANOSECONDS_VSN}")
function timestamp_nano() {
local TIMESTAMP_NANO
# bypass the proxy executable for speed reasons
# local NANOSECONDS_BINARY=${YP_DIR}/bin/nanoseconds
local NANOSECONDS_BINARY=nanoseconds-${YP_NANOSECONDS_VSN}-${OS_SHORT}-${ARCH_NORMALIZED}
NANOSECONDS_BINARY=${YP_DIR}/bin/nanoseconds-${YP_NANOSECONDS_VSN}/${NANOSECONDS_BINARY}
if [[ -e "${NANOSECONDS_BINARY}" ]]; then
TIMESTAMP_NANO=$(${NANOSECONDS_BINARY})
else
# seconds to nanoseconds
TIMESTAMP_NANO=$(date +"%s")000000000
fi
echo ${TIMESTAMP_NANO}
}
}
while [[ $# -gt 0 ]]; do
case "$1" in
--benchmark)
BENCHMARK=$2
shift 2
[[ -e "${BENCHMARK}" ]] || {
touch ${BENCHMARK}
chmod a+rw ${BENCHMARK}
echo "$(timestamp_nano) 0 benchmark" >> ${BENCHMARK}
}
;;
--external)
EXTERNAL=$2
shift 2
;;
-h|--help)
sh_script_usage
;;
-v|--version)
sh_script_version
;;
--)
shift
break
;;
-*)
sh_script_usage
;;
*)
break
;;
esac
done
# [[ $# -eq 0 ]] || sh_script_usage
[[ -z "${EXTERNAL}" ]] || {
[[ -e "${EXTERNAL}" ]] || {
>&2 echo "$(timestamp)" "[ERR ] External logger '${EXTERNAL}' doesn't exist."
exit 1
}
[[ -x "${EXTERNAL}" ]] || {
>&2 echo "$(timestamp)" "[ERR ] External logger '${EXTERNAL}' isn't executable."
exit 1
}
}
STOPWATCH_TIMESTAMP_NANO=$(cat ${BENCHMARK} | head -n1 | cut -d" " -f1 || true)
DATE_REF="--date=@"
# assume bsd/macos
date --date=@0 >/dev/null 2>&1 || DATE_REF="-r "
function timestamp() {
local TIMESTAMP_NANO=$(timestamp_nano)
local TIMESTAMP_MS="$(( ${TIMESTAMP_NANO} / 1000000000 ))"
local TIMESTAMP=$(date "${DATE_REF}${TIMESTAMP_MS}" +%H:%M:%S)
if [[ -z "${STOPWATCH_TIMESTAMP_NANO}" ]]; then
echo "${TIMESTAMP}"
else
local STOPWATCH_TIMESTAMP_MS="$(( (${TIMESTAMP_NANO} - ${STOPWATCH_TIMESTAMP_NANO}) / 1000000000 ))"
local STOPWATCH=$(date -u "${DATE_REF}${STOPWATCH_TIMESTAMP_MS}" +%H:%M:%S)
echo "${TIMESTAMP} ${STOPWATCH}"
fi
}
function fallback_echo() {
case "$1" in
"[DO ]"|"[DONE]")
local COLOR_DIM="\e[2m"
local COLOR_RESET="\e[0m"
[[ "${CI}" != "true" ]] || {
COLOR_DIM=
COLOR_RESET=
}
;;
*)
;;
esac
case $1 in
"[DO ]")
>&2 echo
>&2 echo -e "$(timestamp) $1 ${GROUP}"
>&2 echo -e " ${COLOR_DIM}> ${PWD_GROUP}${COLOR_RESET}"
>&2 echo
;;
"[DONE]")
>&2 echo
>&2 echo -e " ${COLOR_DIM}< ${PWD_GROUP}${COLOR_RESET}"
>&2 echo -e "$(timestamp) $1 ${GROUP}"
>&2 echo -e " in $(( ${GROUP_DURATION} / 1000000000 )) seconds"
>&2 echo
;;
""|" ")
[[ -z "${GROUP}" ]] || >&2 echo "$(timestamp | sed "s/./ /g") $1 ${GROUP}"
;;
*)
>&2 echo "$(timestamp) $1 ${GROUP}"
;;
esac
}
function basic_travis_echo() {
# local ANSI_RED="\033[31;1m"
# local ANSI_GREEN="\033[32;1m"
# local ANSI_RESET="\033[0m"
local ANSI_CLEAR_LINE="\033[0K"
local XTRACE_STATE="$(set +o | grep xtrace)"
set +x
echo -en "$*\\r${ANSI_CLEAR_LINE}"
eval "${XTRACE_STATE}"
}
function travis_echo() {
case $1 in
"[DO ]")
>&2 echo
basic_travis_echo "travis_fold:start:${PWD_GROUP}"
basic_travis_echo "travis_time:start:${PWD_GROUP}"
>&2 echo "$(timestamp) $1 ${GROUP}"
>&2 echo -e " > ${PWD_GROUP}"
>&2 echo
;;
"[DONE]")
>&2 echo
>&2 echo -e " < ${PWD_GROUP}"
basic_travis_echo "travis_time:end:${PWD_GROUP}:start=${GROUP_START}000000000,finish=${GROUP_FINISH},duration=${GROUP_DURATION}"
basic_travis_echo "travis_fold:end:${PWD_GROUP}"
>&2 echo "$(timestamp) $1 ${GROUP}"
>&2 echo "$(timestamp) in $(( ${GROUP_DURATION} / 1000000000 )) seconds"
>&2 echo
;;
""|" ")
[[ -z "${GROUP}" ]] || >&2 echo "$(timestamp | sed "s/./ /g") $1 ${GROUP}"
;;
*)
>&2 echo "$(timestamp) $1 ${GROUP}"
;;
esac
}
function github_actions_echo() {
case $1 in
"[DO ]")
>&2 echo
>&2 echo -e "$(timestamp) $1 ${GROUP}"
# NOTE nesting doesn't work properly
# see https://github.com/actions/runner/issues/802
GITHUB_GROUP_MARKER=/tmp/ci-echo-github-group
[[ -f "${GITHUB_GROUP_MARKER}" ]] || {
echo -n "${PWD_GROUP}" > ${GITHUB_GROUP_MARKER}
>&2 echo "::group::${PWD_GROUP}"
}
>&2 echo -e " > ${PWD_GROUP}"
>&2 echo
;;
"[DONE]")
>&2 echo
>&2 echo -e " < ${PWD_GROUP}"
# NOTE nesting doesn't work properly
# see https://github.com/actions/runner/issues/802
GITHUB_GROUP_MARKER=/tmp/ci-echo-github-group
[[ ! -f "${GITHUB_GROUP_MARKER}" ]] || {
PWD_GROUP2="$(cat "${GITHUB_GROUP_MARKER}")"
[[ "${PWD_GROUP2}" != "${PWD_GROUP}" ]] || {
rm -f "${GITHUB_GROUP_MARKER}"
>&2 echo "::endgroup::"
}
}
>&2 echo -e "$(timestamp) $1 ${GROUP}"
>&2 echo -e " in $(( ${GROUP_DURATION} / 1000000000 )) seconds"
>&2 echo
;;
# "[WARN]")
# >&2 echo "$(timestamp) $1 ${GROUP}"
# echo "::warning::${GROUP}"
# ;;
# "[ERR ]")
# >&2 echo "$(timestamp) $1 ${GROUP}"
# echo "::error::${GROUP}"
# ;;
""|" ")
[[ -z "${GROUP}" ]] || >&2 echo "$(timestamp | sed "s/./ /g") $1 ${GROUP}"
;;
*)
>&2 echo "$(timestamp) $1 ${GROUP}"
;;
esac
}
function basic_teamcity_echo() {
local XTRACE_STATE="$(set +o | grep xtrace)"
set +x
echo "##teamcity[$*]"
eval "${XTRACE_STATE}"
}
function teamcity_echo() {
local MESSAGE="$(echo "${GROUP}" | sed "s/'/|'/")"
local STATUS=NORMAL
case "$1" in
"[DO ]"|"[DONE]")
local PWD_MESSAGE="${REL_PWD} ${MESSAGE}"
;;
*)
;;
esac
case $1 in
"[NEXT]")
echo "##teamcity[progressMessage '${MESSAGE}']"
return 0
;;
"[DO ]")
TEAMCITY_BLOCK_MARKER=/tmp/ci_echo_teamcity_block_${RANDOM}
echo -n "${PWD_MESSAGE}" > ${TEAMCITY_BLOCK_MARKER}
basic_teamcity_echo "blockOpened name='${PWD_MESSAGE}'"
return 0
;;
"[DONE]")
[[ -n "${MESSAGE}" ]] || {
# shellcheck disable=SC2012
TEAMCITY_BLOCK_MARKER=$(ls -t /tmp/ci_echo_teamcity_block_* 2>/dev/null | head -n1 || true)
[[ ! -f "${TEAMCITY_BLOCK_MARKER}" ]] || {
PWD_MESSAGE="$(cat ${TEAMCITY_BLOCK_MARKER})"
rm -f ${TEAMCITY_BLOCK_MARKER}
}
}
basic_teamcity_echo "blockClosed name='${PWD_MESSAGE}'"
echo
return 0
;;
"[SKIP]")
STATUS=WARNING
;;
"[WARN]")
STATUS=WARNING
;;
"[ERR ]")
STATUS=ERROR
;;
"[INFO]"|*)
;;
esac
echo "##teamcity[message text='${MESSAGE}' status='${STATUS}']"
}
GROUP="${*:2}"
case "${1:-}" in
"[DO ]")
GROUP_START=$(timestamp_nano)
REL_PWD=$(${YP_DIR}/bin/relpath "$(pwd)" "${HOME}" 2>/dev/null || pwd)
PWD_GROUP="$(echo "${REL_PWD} ${GROUP}" | tr "[:upper:]" "[:lower:]" | sed "s/[^a-z0-9]\{1,\}/-/g" | sed "s/^-//" | sed "s/-$//")"
# shellcheck disable=SC2012
# PARENT_GROUP_MARKER=$(ls -t /tmp/ci-echo-group-* 2>/dev/null | head -n1 || true)
# PARENT_GROUP_MARKER_PWD_GROUP=
# [[ -z "${PARENT_GROUP_MARKER}" ]] || \
# PARENT_GROUP_MARKER_PWD_GROUP="$(cat "${PARENT_GROUP_MARKER}" | sed -n "3,3 p")"
# GROUP_MARKER=/tmp/ci-echo-group-${PWD_GROUP}-${GROUP_START}_${RANDOM}
GROUP_MARKER=/tmp/ci-echo-group-${GROUP_START}-${RANDOM}
echo -e "${GROUP_START}\n${GROUP}\n${PWD_GROUP}" > ${GROUP_MARKER}
# [[ -z "${EXTERNAL}" ]] || ${EXTERNAL} \
# --group-parent "${PARENT_GROUP_MARKER_PWD_GROUP}" \
# --group "${PWD_GROUP}" \
# --group-start "${GROUP_START}" &
;;
"[DONE]")
REL_PWD=$(${YP_DIR}/bin/relpath "$(pwd)" "${HOME}" 2>/dev/null || pwd)
# PWD_GROUP_PREFIX="$(echo "${REL_PWD}" | tr "A-Z" "a-z" | sed "s/[^a-z0-9]\{1,\}/-/g" | sed "s/^-//" | sed "s/-$//")"
GROUP_FINISH=$(timestamp_nano)
GROUP_START=${GROUP_FINISH}
GROUP_DURATION=0
[[ -n "${GROUP}" ]] || {
# shellcheck disable=SC2012
# GROUP_MARKER=$(ls -t /tmp/ci-echo-group-${PWD_GROUP_PREFIX}-* 2>/dev/null | head -n1 || true)
GROUP_MARKER=$(ls -t /tmp/ci-echo-group-* 2>/dev/null | head -n1 || true)
[[ ! -f "${GROUP_MARKER}" ]] || {
GROUP_START=$(cat ${GROUP_MARKER} | sed -n "1,1 p")
GROUP=$(cat ${GROUP_MARKER} | sed -n "2,2 p")
PWD_GROUP=$(cat ${GROUP_MARKER} | sed -n "3,3 p")
GROUP_DURATION=$(( ${GROUP_FINISH} - ${GROUP_START} ))
rm -f ${GROUP_MARKER}
# shellcheck disable=SC2012
PARENT_GROUP_MARKER=$(ls -t /tmp/ci-echo-group-* 2>/dev/null | head -n1 || true)
PARENT_GROUP_MARKER_PWD_GROUP=
[[ -z "${PARENT_GROUP_MARKER}" ]] || \
PARENT_GROUP_MARKER_PWD_GROUP="$(cat "${PARENT_GROUP_MARKER}" | sed -n "3,3 p")"
echo "${GROUP_START} ${GROUP_DURATION} ${PWD_GROUP}" >> ${BENCHMARK}
[[ -z "${EXTERNAL}" ]] || ${EXTERNAL} \
--group-parent "${PARENT_GROUP_MARKER_PWD_GROUP}" \
--group "${PWD_GROUP}" \
--group-start "${GROUP_START}" \
--group-duration "${GROUP_DURATION}" &
}
}
[[ -n "${PWD_GROUP:-}" ]] || {
>&2 echo "$(timestamp)" "[ERR ] Looks like you've called 'ci-echo [DONE]' without calling 'ci-echo [DO ]' first."
exit 1
}
;;
*)
;;
esac
if [[ "${TRAVIS:-}" = "true" ]]; then
travis_echo "${1:-}" "${GROUP}"
elif [[ -n "${GITHUB_ACTIONS:-}" ]]; then
github_actions_echo "${1:-}" "${GROUP}"
elif [[ -n "${TEAMCITY_VERSION:-}" ]]; then
teamcity_echo "${1:-}" "${GROUP}"
else
fallback_echo "${1:-}" "${GROUP}"
fi