• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env bash
2# shellcheck disable=SC2048
3# shellcheck disable=SC2086 # we want word splitting
4# shellcheck disable=SC2155 # mktemp usually not failing
5
6function x_off {
7    if [[ "$-" == *"x"* ]]; then
8      state_x=1
9      set +x
10    else
11      state_x=0
12    fi
13}
14
15# TODO: implement x_on !
16
17function error {
18    x_off 2>/dev/null
19    RED="\e[0;31m"
20    ENDCOLOR="\e[0m"
21    # we force the following to be not in a section
22    section_end $CURRENT_SECTION
23
24    DATE_S=$(date -u +"%s")
25    JOB_START_S=$(date -u +"%s" -d "${CI_JOB_STARTED_AT:?}")
26    CURR_TIME=$((DATE_S-JOB_START_S))
27    CURR_MINSEC="$(printf "%02d" $((CURR_TIME/60))):$(printf "%02d" $((CURR_TIME%60)))"
28    echo -e "\n${RED}[${CURR_MINSEC}] ERROR: $*${ENDCOLOR}\n"
29    [ "$state_x" -eq 0 ] || set -x
30}
31
32function trap_err {
33    error ${CURRENT_SECTION:-'unknown-section'}: ret code: $*
34}
35
36function build_section_start {
37    local section_params=$1
38    shift
39    local section_name=$1
40    CURRENT_SECTION=$section_name
41    shift
42    CYAN="\e[0;36m"
43    ENDCOLOR="\e[0m"
44
45    DATE_S=$(date -u +"%s")
46    JOB_START_S=$(date -u +"%s" -d "${CI_JOB_STARTED_AT:?}")
47    CURR_TIME=$((DATE_S-JOB_START_S))
48    CURR_MINSEC="$(printf "%02d" $((CURR_TIME/60))):$(printf "%02d" $((CURR_TIME%60)))"
49    echo -e "\n\e[0Ksection_start:$(date +%s):$section_name$section_params\r\e[0K${CYAN}[${CURR_MINSEC}] $*${ENDCOLOR}\n"
50}
51
52function section_start {
53    x_off 2>/dev/null
54    build_section_start "[collapsed=true]" $*
55    [ "$state_x" -eq 0 ] || set -x
56}
57
58function build_section_end {
59    echo -e "\e[0Ksection_end:$(date +%s):$1\r\e[0K"
60    CURRENT_SECTION=""
61}
62
63function section_end {
64    x_off >/dev/null
65    build_section_end $*
66    [ "$state_x" -eq 0 ] || set -x
67}
68
69function section_switch {
70    x_off 2>/dev/null
71    if [ -n "$CURRENT_SECTION" ]
72    then
73	build_section_end $CURRENT_SECTION
74    fi
75    build_section_start "[collapsed=true]" $*
76    [ "$state_x" -eq 0 ] || set -x
77}
78
79function uncollapsed_section_switch {
80    x_off 2>/dev/null
81    if [ -n "$CURRENT_SECTION" ]
82    then
83	build_section_end $CURRENT_SECTION
84    fi
85    build_section_start "" $*
86    [ "$state_x" -eq 0 ] || set -x
87}
88
89export -f x_off
90export -f error
91export -f trap_err
92export -f build_section_start
93export -f section_start
94export -f build_section_end
95export -f section_end
96export -f section_switch
97export -f uncollapsed_section_switch
98
99# Freedesktop requirement (needed for Wayland)
100[ -n "${XDG_RUNTIME_DIR}" ] || export XDG_RUNTIME_DIR="$(mktemp -p "$PWD" -d xdg-runtime-XXXXXX)"
101
102set -E
103trap 'trap_err $?' ERR
104