-
Notifications
You must be signed in to change notification settings - Fork 721
/
lib.sh
138 lines (111 loc) · 2.07 KB
/
lib.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
to_jsonlist() {
for x in "$@"
do echo "\"$x\""
done | jq --slurp '.'
}
jq_tolist() {
local exp=$1; shift
jq "$exp | join (\" \")" --raw-output "$@"
}
jq_fmutate() {
local f=$1; shift
test -f "$f" || { echo '{}' > "$f"; }
jq "$@" "$f" | sponge "$f"
}
jq_check_json() {
jq '.' "$1" >/dev/null
}
__usage() {
local op=$1 desc=$2
cat >&2 <<EOF
USAGE: $(basename "$0") OPTIONS.. $op SUBOP SUBOP-ARGS..
$desc:
$(cat)
EOF
}
usage() {
__usage "$@"
exit 1
}
declare -A colormap
colormap=(
[black]=30
[red]=31
[green]=32
[yellow]=33
[blue]=34
[magenta]=35
[cyan]=36
[white]=37
[reset]=0
)
color() {
echo -ne "\033[${colormap[$1]}m"
}
with_color() {
local color=$1; shift
color $color
echo -ne "$*"
color reset
}
colorise_colors=(
red green yellow blue cyan white
)
colorise() {
for ((i=0; $#!=0; i++))
do echo -n "$(with_color ${colorise_colors[$((i % 6))]} $1) "
shift
done
}
msg() {
echo "workbench: $*" >&2
}
msg_ne() {
echo -ne "workbench: $*" >&2
}
green() {
with_color green $*
}
blue() {
with_color blue $*
}
white() {
with_color white $*
}
yellow() {
with_color yellow $*
}
red() {
with_color red $*
}
progress() {
local subsys=$1; shift
msg "$(with_color green $subsys): $(with_color blue $*)"
}
progress_ne() {
local subsys=$1; shift
msg_ne "$(with_color green $subsys): $(with_color blue $*)"
}
warn() {
local subsys=$1; shift
msg "$(with_color green $subsys): $(with_color yellow $*)"
}
fail() {
msg "$(with_color red $*)"
exit 1
}
fatal() {
fail "FATAL: $*"
}
jqtest() {
jq --exit-status "$@" > /dev/null
}
git_repo_commit_description() {
local repo=$1
local commit=$(git -C "$repo" rev-parse 'HEAD' 2>/dev/null || true)
test -n "$commit" && {
git -C "$repo" describe --match '[0-9].*' --tags $commit 2>/dev/null |
cut -d'-' -f1,2 | tr -d '\n'
git -C "$repo" diff --exit-code --quiet || echo '-modified'
} || echo "unknown-not-a-git-repo"
}