-
Notifications
You must be signed in to change notification settings - Fork 0
/
events.sh
86 lines (76 loc) · 2.43 KB
/
events.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
# shellcheck shell=bash
# Constants
# shellcheck disable=SC2034
{
# Event levels
readonly level_err=0
readonly level_sec=1
readonly level_warn=2
readonly level_notice=3
readonly level_info=4
# Symbols
readonly symbol_err='✗'
readonly symbol_sec='☠'
readonly symbol_warn='⚠'
readonly symbol_notice='🛈'
readonly symbol_ok='✓'
readonly symbol_tip='💡'
readonly symbol_todo='☐'
readonly symbol_doing='…'
readonly symbol_done='🗹'
readonly symbol_failed='☒'
# Symbols to event levels mapping
readonly symbol_to_level=("$symbol_err" "$symbol_sec" "$symbol_warn" "$symbol_notice" "$symbol_ok")
} 2>/dev/null
# Functions
# Send event to stderr
# $1 ... event message
# [$2] ... event level, one of level_* constants, default "$level_info"
# [$3] ... event symbol, one of symbol_* constants, default "${symbol_to_level[$level]}"
# [$4] ... command, default "$0"
# Stderr: command symbol event message
function event() {
local level="${2:-$level_info}"
local symbol="${3:-${symbol_to_level[$level]}}"
echo "${4:-${TEST_ARGV[0]:-$0}} $symbol $1" >&2
}
# Raise an error
# $1 ... event message
# [$2] ... event symbol, one of symbol_* constants, default "$symbol_err"
# [$3] ... command, default "$0"
# Stderr: command symbol event message
function err() {
event "$1" "$level_err" "${2:-}" "${3:-}"
}
# Raise a security warning
# $1 ... event message
# [$2] ... event symbol, one of symbol_* constants, default "$symbol_security"
# [$3] ... command, default "$0"
# Stderr: command symbol event message
function sec() {
event "$1" "$level_sec" "${2:-}" "${3:-}"
}
# Raise a warning
# $1 ... event message
# [$2] ... event symbol, one of symbol_* constants, default "$symbol_warn"
# [$3] ... command, default "$0"
# Stderr: command symbol event message
function warn() {
event "$1" "$level_warn" "${2:-}" "${3:-}"
}
# Raise a notice
# $1 ... event message
# [$2] ... event symbol, one of symbol_* constants, default "$symbol_notice"
# [$3] ... command, default "$0"
# Stderr: command symbol event message
function notice() {
event "$1" "$level_notice" "${2:-}" "${3:-}"
}
# Raise an information
# $1 ... event message
# [$2] ... event symbol, one of symbol_* constants, default "$symbol_ok"
# [$3] ... command, default "$0"
# Stderr: command symbol event message
function info() {
event "$1" "$level_info" "${2:-}" "${3:-}"
}