darwin-shell 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- darwin_shell/__init__.py +34 -0
- darwin_shell/cli.py +140 -0
- darwin_shell/share/bootstrap.sh +74 -0
- darwin_shell/share/checkpoint.sh +437 -0
- darwin_shell/share/cli.sh +555 -0
- darwin_shell/share/core.sh +214 -0
- darwin_shell/share/datetime.sh +367 -0
- darwin_shell/share/env.sh +322 -0
- darwin_shell/share/file.sh +303 -0
- darwin_shell/share/i18n.sh +181 -0
- darwin_shell/share/json.sh +258 -0
- darwin_shell/share/locale/zh_CN.sh +456 -0
- darwin_shell/share/lock.sh +414 -0
- darwin_shell/share/log.sh +317 -0
- darwin_shell/share/notify.sh +576 -0
- darwin_shell/share/process.sh +394 -0
- darwin_shell/share/sched.sh +360 -0
- darwin_shell/share/stats.sh +540 -0
- darwin_shell/share/terminal.sh +226 -0
- darwin_shell-0.1.0.dist-info/METADATA +208 -0
- darwin_shell-0.1.0.dist-info/RECORD +25 -0
- darwin_shell-0.1.0.dist-info/WHEEL +5 -0
- darwin_shell-0.1.0.dist-info/entry_points.txt +2 -0
- darwin_shell-0.1.0.dist-info/licenses/LICENSE +21 -0
- darwin_shell-0.1.0.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,576 @@
|
|
|
1
|
+
#!/usr/bin/env bash
|
|
2
|
+
# darwin-shell notify: data-driven message rendering and pluggable transports.
|
|
3
|
+
# This module owns neither delivery protocols nor business status semantics.
|
|
4
|
+
|
|
5
|
+
if ! declare -F dsh::i18n::t >/dev/null 2>&1; then
|
|
6
|
+
if declare -F dsh::use >/dev/null 2>&1; then
|
|
7
|
+
dsh::use i18n || return $?
|
|
8
|
+
else
|
|
9
|
+
_dsh_private_notify_self="${BASH_SOURCE[0]}"
|
|
10
|
+
source "${_dsh_private_notify_self%/*}/i18n.sh" || return $?
|
|
11
|
+
unset _dsh_private_notify_self
|
|
12
|
+
fi
|
|
13
|
+
fi
|
|
14
|
+
if ! declare -F dsh::core::fail >/dev/null 2>&1; then
|
|
15
|
+
if declare -F dsh::use >/dev/null 2>&1; then
|
|
16
|
+
dsh::use core || return $?
|
|
17
|
+
else
|
|
18
|
+
_dsh_private_notify_self="${BASH_SOURCE[0]}"
|
|
19
|
+
source "${_dsh_private_notify_self%/*}/core.sh" || return $?
|
|
20
|
+
unset _dsh_private_notify_self
|
|
21
|
+
fi
|
|
22
|
+
fi
|
|
23
|
+
|
|
24
|
+
if [[ -n "${_dsh_private_notify_sourced:-}" ]]; then
|
|
25
|
+
return 0
|
|
26
|
+
fi
|
|
27
|
+
_dsh_private_notify_sourced=1
|
|
28
|
+
readonly _dsh_private_notify_sourced
|
|
29
|
+
|
|
30
|
+
# Registration survives configure; rendering options and labels do not.
|
|
31
|
+
declare -gA _dsh_private_notify_cfg=()
|
|
32
|
+
declare -gA _dsh_private_notify_labels=()
|
|
33
|
+
declare -gA _dsh_private_notify_transports=()
|
|
34
|
+
declare -gA _dsh_private_notify_limits=()
|
|
35
|
+
declare -gA _dsh_private_notify_formatters=()
|
|
36
|
+
|
|
37
|
+
_dsh_private_notify_help() {
|
|
38
|
+
case "${1:-}" in
|
|
39
|
+
configure)
|
|
40
|
+
dsh::i18n::t 'Usage: dsh::notify::configure [--max-chars N] [--head-lines N] [--tail-lines N] [--label KEY=TEXT]...'
|
|
41
|
+
printf '\n'
|
|
42
|
+
dsh::i18n::t 'Set rendering limits and custom labels; 0 means unlimited characters.'
|
|
43
|
+
;;
|
|
44
|
+
transport)
|
|
45
|
+
dsh::i18n::t 'Usage: dsh::notify::transport::register <name> <func> [--limit N]'
|
|
46
|
+
printf '\n'
|
|
47
|
+
dsh::i18n::t 'Register a transport that reads the message from stdin; N=0 means no cap.'
|
|
48
|
+
;;
|
|
49
|
+
formatter)
|
|
50
|
+
dsh::i18n::t 'Usage: dsh::notify::formatter::register <event> <func>'
|
|
51
|
+
printf '\n'
|
|
52
|
+
dsh::i18n::t 'Register func <event> <counters_name> <lists_name> <msgs_name> for an event.'
|
|
53
|
+
;;
|
|
54
|
+
render)
|
|
55
|
+
dsh::i18n::t 'Usage: dsh::notify::render <event> [key=value|list:key=value|msg:level=text]...'
|
|
56
|
+
printf '\n'
|
|
57
|
+
dsh::i18n::t 'Render a message to stdout without sending it.'
|
|
58
|
+
;;
|
|
59
|
+
send)
|
|
60
|
+
dsh::i18n::t 'Usage: dsh::notify::send <event> [--transport NAME] [protocol args...]'
|
|
61
|
+
printf '\n'
|
|
62
|
+
dsh::i18n::t 'Render, apply the transport cap and dispatch through a registered transport.'
|
|
63
|
+
;;
|
|
64
|
+
from_result)
|
|
65
|
+
dsh::i18n::t 'Usage: dsh::notify::from_result <array_name> [--event EVENT] [--transport NAME]'
|
|
66
|
+
printf '\n'
|
|
67
|
+
dsh::i18n::t 'Convert a flat stats::result array into the notify protocol and send it.'
|
|
68
|
+
;;
|
|
69
|
+
*)
|
|
70
|
+
dsh::i18n::t 'dsh::notify: no help for %1$s' "${1:-<empty>}" >&2
|
|
71
|
+
return "$DSH_RC_USAGE"
|
|
72
|
+
;;
|
|
73
|
+
esac
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
_dsh_private_notify_name_ok() {
|
|
77
|
+
[[ "${1:-}" =~ ^[A-Za-z_][A-Za-z0-9_]*$ ]]
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
_dsh_private_notify_uint_ok() {
|
|
81
|
+
[[ "${1:-}" =~ ^[0-9]+$ ]]
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
_dsh_private_notify_cfg_get() {
|
|
85
|
+
case "$1" in
|
|
86
|
+
max_chars) printf '%s' "${_dsh_private_notify_cfg[max_chars]:-0}" ;;
|
|
87
|
+
head_lines) printf '%s' "${_dsh_private_notify_cfg[head_lines]:-10}" ;;
|
|
88
|
+
tail_lines) printf '%s' "${_dsh_private_notify_cfg[tail_lines]:-10}" ;;
|
|
89
|
+
esac
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
_dsh_private_notify_label() {
|
|
93
|
+
local key="$1"
|
|
94
|
+
if [[ -n "${_dsh_private_notify_labels[$key]:-}" ]]; then
|
|
95
|
+
printf '%s' "${_dsh_private_notify_labels[$key]}"
|
|
96
|
+
else
|
|
97
|
+
printf '%s' "$key"
|
|
98
|
+
fi
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
_dsh_private_notify_event_label() {
|
|
102
|
+
case "$1" in
|
|
103
|
+
done) dsh::i18n::t 'dsh::notify: completed' ;;
|
|
104
|
+
interrupt) dsh::i18n::t 'dsh::notify: interrupted' ;;
|
|
105
|
+
error) dsh::i18n::t 'dsh::notify: failed' ;;
|
|
106
|
+
lock_failed) dsh::i18n::t 'dsh::notify: lock failed' ;;
|
|
107
|
+
*) printf '%s' "$1" ;;
|
|
108
|
+
esac
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
_dsh_private_notify_event_mark() {
|
|
112
|
+
case "$1" in
|
|
113
|
+
done) printf '[OK]' ;;
|
|
114
|
+
interrupt) printf '[STOP]' ;;
|
|
115
|
+
error) printf '[FAIL]' ;;
|
|
116
|
+
lock_failed) printf '[LOCK]' ;;
|
|
117
|
+
*) printf '[INFO]' ;;
|
|
118
|
+
esac
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
# Split on newlines into an indexed array, preserving empty lines.
|
|
122
|
+
_dsh_private_notify_split() {
|
|
123
|
+
local rest="$1" line
|
|
124
|
+
local -n _dsh_private_notify_out="$2"
|
|
125
|
+
_dsh_private_notify_out=()
|
|
126
|
+
while [[ -n "$rest" ]]; do
|
|
127
|
+
if [[ "$rest" == *$'\n'* ]]; then
|
|
128
|
+
line="${rest%%$'\n'*}"
|
|
129
|
+
rest="${rest#*$'\n'}"
|
|
130
|
+
else
|
|
131
|
+
line="$rest"
|
|
132
|
+
rest=''
|
|
133
|
+
fi
|
|
134
|
+
_dsh_private_notify_out+=("$line")
|
|
135
|
+
done
|
|
136
|
+
(( ${#_dsh_private_notify_out[@]} )) || _dsh_private_notify_out+=('')
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
# Keep head and tail lines, then enforce a character cap if one still applies.
|
|
140
|
+
_dsh_private_notify_fit() {
|
|
141
|
+
local text="$1" cap="$2" head="$3" tail="$4"
|
|
142
|
+
if (( cap == 0 )) || (( ${#text} <= cap )); then
|
|
143
|
+
printf '%s\n' "$text"
|
|
144
|
+
return 0
|
|
145
|
+
fi
|
|
146
|
+
|
|
147
|
+
local -a lines=() kept=()
|
|
148
|
+
_dsh_private_notify_split "$text" lines
|
|
149
|
+
if (( ${#lines[@]} > head + tail )); then
|
|
150
|
+
local i
|
|
151
|
+
for ((i = 0; i < head; i++)); do
|
|
152
|
+
kept+=("${lines[i]}")
|
|
153
|
+
done
|
|
154
|
+
kept+=("$(dsh::i18n::t 'dsh::notify: omitted %1$s lines' "$(( ${#lines[@]} - head - tail ))")")
|
|
155
|
+
for ((i = ${#lines[@]} - tail; i < ${#lines[@]}; i++)); do
|
|
156
|
+
kept+=("${lines[i]}")
|
|
157
|
+
done
|
|
158
|
+
else
|
|
159
|
+
kept=("${lines[@]}")
|
|
160
|
+
fi
|
|
161
|
+
|
|
162
|
+
local out='' item
|
|
163
|
+
for item in "${kept[@]}"; do
|
|
164
|
+
if [[ -z "$out" ]]; then
|
|
165
|
+
out="$item"
|
|
166
|
+
else
|
|
167
|
+
out+=$'\n'"$item"
|
|
168
|
+
fi
|
|
169
|
+
done
|
|
170
|
+
if (( ${#out} > cap )); then
|
|
171
|
+
local marker
|
|
172
|
+
marker="$(dsh::i18n::t 'dsh::notify: truncated')"
|
|
173
|
+
local room=$((cap - ${#marker}))
|
|
174
|
+
if (( room < 2 )); then
|
|
175
|
+
out="${out:0:cap}"
|
|
176
|
+
else
|
|
177
|
+
local head_chars=$((room * 2 / 3))
|
|
178
|
+
local tail_chars=$((room - head_chars))
|
|
179
|
+
if (( tail_chars > 0 )); then
|
|
180
|
+
out="${out:0:head_chars}${marker}${out: -tail_chars}"
|
|
181
|
+
else
|
|
182
|
+
out="${out:0:head_chars}${marker}"
|
|
183
|
+
fi
|
|
184
|
+
fi
|
|
185
|
+
fi
|
|
186
|
+
printf '%s\n' "$out"
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
_dsh_private_notify_parse() {
|
|
190
|
+
local -n _dsh_private_notify_c_ref="$1"
|
|
191
|
+
local -n _dsh_private_notify_l_ref="$2"
|
|
192
|
+
local -n _dsh_private_notify_m_ref="$3"
|
|
193
|
+
shift 3
|
|
194
|
+
|
|
195
|
+
local kv key value
|
|
196
|
+
for kv in "$@"; do
|
|
197
|
+
case "$kv" in
|
|
198
|
+
list:*)
|
|
199
|
+
key="${kv#list:}"
|
|
200
|
+
key="${key%%=*}"
|
|
201
|
+
value="${kv#list:*=}"
|
|
202
|
+
[[ -n "$key" && "$value" != "$kv" ]] || continue
|
|
203
|
+
_dsh_private_notify_l_ref["$key"]+="$value"$'\n'
|
|
204
|
+
;;
|
|
205
|
+
msg:*)
|
|
206
|
+
key="${kv#msg:}"
|
|
207
|
+
key="${key%%=*}"
|
|
208
|
+
value="${kv#msg:*=}"
|
|
209
|
+
[[ -n "$key" && "$value" != "$kv" ]] || continue
|
|
210
|
+
_dsh_private_notify_m_ref["$key"]+="$value"$'\n'
|
|
211
|
+
;;
|
|
212
|
+
*=*)
|
|
213
|
+
key="${kv%%=*}"
|
|
214
|
+
value="${kv#*=}"
|
|
215
|
+
[[ -n "$key" ]] && _dsh_private_notify_c_ref["$key"]="$value"
|
|
216
|
+
;;
|
|
217
|
+
esac
|
|
218
|
+
done
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
# Public: set rendering options. Repeated calls replace options and labels.
|
|
222
|
+
dsh::notify::configure() {
|
|
223
|
+
if [[ "${1:-}" == --help ]]; then
|
|
224
|
+
_dsh_private_notify_help configure
|
|
225
|
+
return $?
|
|
226
|
+
fi
|
|
227
|
+
|
|
228
|
+
local -A labels=()
|
|
229
|
+
local max_chars=0 head_lines=10 tail_lines=10 key value
|
|
230
|
+
while (( $# )); do
|
|
231
|
+
case "$1" in
|
|
232
|
+
--max-chars|--head-lines|--tail-lines)
|
|
233
|
+
if (( $# < 2 )); then
|
|
234
|
+
dsh::core::fail "$DSH_RC_USAGE" 'dsh::notify: option requires a value: %1$s' "$1"
|
|
235
|
+
return $?
|
|
236
|
+
fi
|
|
237
|
+
_dsh_private_notify_uint_ok "$2" || {
|
|
238
|
+
dsh::core::fail "$DSH_RC_USAGE" 'dsh::notify: expected a non-negative integer: %1$s' "$2"
|
|
239
|
+
return $?
|
|
240
|
+
}
|
|
241
|
+
case "$1" in
|
|
242
|
+
--max-chars) max_chars="$2" ;;
|
|
243
|
+
--head-lines) head_lines="$2" ;;
|
|
244
|
+
--tail-lines) tail_lines="$2" ;;
|
|
245
|
+
esac
|
|
246
|
+
shift 2
|
|
247
|
+
;;
|
|
248
|
+
--label)
|
|
249
|
+
if (( $# < 2 )); then
|
|
250
|
+
dsh::core::fail "$DSH_RC_USAGE" 'dsh::notify: option requires a value: %1$s' "$1"
|
|
251
|
+
return $?
|
|
252
|
+
fi
|
|
253
|
+
key="${2%%=*}"
|
|
254
|
+
value="${2#*=}"
|
|
255
|
+
if [[ "$value" == "$2" ]] || ! _dsh_private_notify_name_ok "$key"; then
|
|
256
|
+
dsh::core::fail "$DSH_RC_USAGE" 'dsh::notify: invalid label: %1$s' "$2"
|
|
257
|
+
return $?
|
|
258
|
+
fi
|
|
259
|
+
labels["$key"]="$value"
|
|
260
|
+
shift 2
|
|
261
|
+
;;
|
|
262
|
+
*)
|
|
263
|
+
dsh::core::fail "$DSH_RC_USAGE" 'dsh::notify: invalid option: %1$s' "$1"
|
|
264
|
+
return $?
|
|
265
|
+
;;
|
|
266
|
+
esac
|
|
267
|
+
done
|
|
268
|
+
|
|
269
|
+
_dsh_private_notify_cfg=([max_chars]="$max_chars" [head_lines]="$head_lines" [tail_lines]="$tail_lines")
|
|
270
|
+
_dsh_private_notify_labels=()
|
|
271
|
+
for key in "${!labels[@]}"; do
|
|
272
|
+
_dsh_private_notify_labels["$key"]="${labels[$key]}"
|
|
273
|
+
done
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
# Public: register a delivery function. It receives <event> <name> and reads
|
|
277
|
+
# the complete message from stdin, so long text never goes through argv.
|
|
278
|
+
dsh::notify::transport::register() {
|
|
279
|
+
if [[ "${1:-}" == --help ]]; then
|
|
280
|
+
_dsh_private_notify_help transport
|
|
281
|
+
return $?
|
|
282
|
+
fi
|
|
283
|
+
if (( $# < 2 )); then
|
|
284
|
+
dsh::core::fail "$DSH_RC_USAGE" 'dsh::notify: expected <name> <func>'
|
|
285
|
+
return $?
|
|
286
|
+
fi
|
|
287
|
+
local name="$1" func="$2" limit=0
|
|
288
|
+
shift 2
|
|
289
|
+
while (( $# )); do
|
|
290
|
+
case "$1" in
|
|
291
|
+
--limit)
|
|
292
|
+
if (( $# < 2 )); then
|
|
293
|
+
dsh::core::fail "$DSH_RC_USAGE" 'dsh::notify: option requires a value: %1$s' "$1"
|
|
294
|
+
return $?
|
|
295
|
+
fi
|
|
296
|
+
_dsh_private_notify_uint_ok "$2" || {
|
|
297
|
+
dsh::core::fail "$DSH_RC_USAGE" 'dsh::notify: expected a non-negative integer: %1$s' "$2"
|
|
298
|
+
return $?
|
|
299
|
+
}
|
|
300
|
+
limit="$2"
|
|
301
|
+
shift 2
|
|
302
|
+
;;
|
|
303
|
+
*)
|
|
304
|
+
dsh::core::fail "$DSH_RC_USAGE" 'dsh::notify: invalid option: %1$s' "$1"
|
|
305
|
+
return $?
|
|
306
|
+
;;
|
|
307
|
+
esac
|
|
308
|
+
done
|
|
309
|
+
if ! _dsh_private_notify_name_ok "$name"; then
|
|
310
|
+
dsh::core::fail "$DSH_RC_USAGE" 'dsh::notify: invalid name: %1$s' "$name"
|
|
311
|
+
return $?
|
|
312
|
+
fi
|
|
313
|
+
if ! declare -F "$func" >/dev/null 2>&1; then
|
|
314
|
+
dsh::core::fail "$DSH_RC_USAGE" 'dsh::notify: transport requires a function: %1$s' "$func"
|
|
315
|
+
return $?
|
|
316
|
+
fi
|
|
317
|
+
_dsh_private_notify_transports["$name"]="$func"
|
|
318
|
+
_dsh_private_notify_limits["$name"]="$limit"
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
# Public: override rendering for an event. The function receives the event and
|
|
322
|
+
# the names of three associative arrays it may read via nameref.
|
|
323
|
+
dsh::notify::formatter::register() {
|
|
324
|
+
if [[ "${1:-}" == --help ]]; then
|
|
325
|
+
_dsh_private_notify_help formatter
|
|
326
|
+
return $?
|
|
327
|
+
fi
|
|
328
|
+
if (( $# < 2 )); then
|
|
329
|
+
dsh::core::fail "$DSH_RC_USAGE" 'dsh::notify: expected <event> <func>'
|
|
330
|
+
return $?
|
|
331
|
+
fi
|
|
332
|
+
local event="$1" func="$2"
|
|
333
|
+
if [[ -z "$event" ]]; then
|
|
334
|
+
dsh::core::fail "$DSH_RC_USAGE" 'dsh::notify: invalid name: %1$s' "$event"
|
|
335
|
+
return $?
|
|
336
|
+
fi
|
|
337
|
+
if ! declare -F "$func" >/dev/null 2>&1; then
|
|
338
|
+
dsh::core::fail "$DSH_RC_USAGE" 'dsh::notify: transport requires a function: %1$s' "$func"
|
|
339
|
+
return $?
|
|
340
|
+
fi
|
|
341
|
+
_dsh_private_notify_formatters["$event"]="$func"
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
_dsh_private_notify_default_render() {
|
|
345
|
+
local event="$1"
|
|
346
|
+
local -n _dsh_private_notify_c="$2"
|
|
347
|
+
local -n _dsh_private_notify_l="$3"
|
|
348
|
+
local -n _dsh_private_notify_m="$4"
|
|
349
|
+
|
|
350
|
+
local headline='' status='' duration='' exit_code='' report='' script=''
|
|
351
|
+
headline="${_dsh_private_notify_c[headline]:-}"
|
|
352
|
+
status="${_dsh_private_notify_c[status]:-}"
|
|
353
|
+
duration="${_dsh_private_notify_c[duration]:-}"
|
|
354
|
+
exit_code="${_dsh_private_notify_c[exit_code]:-}"
|
|
355
|
+
report="${_dsh_private_notify_c[report]:-}"
|
|
356
|
+
script="${_dsh_private_notify_c[script]:-}"
|
|
357
|
+
|
|
358
|
+
local -a counter_keys=() parts=() items=()
|
|
359
|
+
local key
|
|
360
|
+
for key in "${!_dsh_private_notify_c[@]}"; do
|
|
361
|
+
case "$key" in
|
|
362
|
+
headline|status|duration|exit_code|report|script|started_at|finished_at) continue ;;
|
|
363
|
+
esac
|
|
364
|
+
counter_keys+=("$key")
|
|
365
|
+
done
|
|
366
|
+
|
|
367
|
+
local first
|
|
368
|
+
first="$(_dsh_private_notify_event_mark "$event") $(_dsh_private_notify_event_label "$event")"
|
|
369
|
+
[[ -z "$script" ]] || first+=" ${script}"
|
|
370
|
+
|
|
371
|
+
if [[ -n "$headline" ]]; then
|
|
372
|
+
parts+=("$headline")
|
|
373
|
+
else
|
|
374
|
+
for key in "${counter_keys[@]}"; do
|
|
375
|
+
parts+=("$(_dsh_private_notify_label "$key")=${_dsh_private_notify_c[$key]}")
|
|
376
|
+
done
|
|
377
|
+
fi
|
|
378
|
+
for key in "${parts[@]}"; do
|
|
379
|
+
first+=" · $key"
|
|
380
|
+
done
|
|
381
|
+
printf '%s\n' "$first"
|
|
382
|
+
|
|
383
|
+
[[ -z "$status" ]] || dsh::i18n::t 'dsh::notify: Status: %1$s' "$status"
|
|
384
|
+
[[ -z "$duration" ]] || dsh::i18n::t 'dsh::notify: Duration: %1$s' "$duration"
|
|
385
|
+
[[ -z "$exit_code" ]] || dsh::i18n::t 'dsh::notify: Exit code: %1$s' "$exit_code"
|
|
386
|
+
|
|
387
|
+
if [[ -n "$headline" ]] && (( ${#counter_keys[@]} > 0 )); then
|
|
388
|
+
printf '\n%s\n' "$(dsh::i18n::t 'dsh::notify: Counters:')"
|
|
389
|
+
for key in "${counter_keys[@]}"; do
|
|
390
|
+
printf ' %s: %s\n' "$(_dsh_private_notify_label "$key")" "${_dsh_private_notify_c[$key]}"
|
|
391
|
+
done
|
|
392
|
+
fi
|
|
393
|
+
|
|
394
|
+
for key in "${!_dsh_private_notify_l[@]}"; do
|
|
395
|
+
printf '\n%s\n' "$(_dsh_private_notify_label "$key")"
|
|
396
|
+
_dsh_private_notify_split "${_dsh_private_notify_l[$key]}" items
|
|
397
|
+
local line
|
|
398
|
+
for line in "${items[@]}"; do
|
|
399
|
+
printf ' - %s\n' "$line"
|
|
400
|
+
done
|
|
401
|
+
done
|
|
402
|
+
|
|
403
|
+
for key in "${!_dsh_private_notify_m[@]}"; do
|
|
404
|
+
printf '\n%s\n' "$(_dsh_private_notify_label "$key")"
|
|
405
|
+
_dsh_private_notify_split "${_dsh_private_notify_m[$key]}" items
|
|
406
|
+
local message
|
|
407
|
+
for message in "${items[@]}"; do
|
|
408
|
+
printf ' - %s\n' "$message"
|
|
409
|
+
done
|
|
410
|
+
done
|
|
411
|
+
|
|
412
|
+
[[ -z "$report" ]] || printf '\n%s\n' "$(dsh::i18n::t 'dsh::notify: Report: %1$s' "$report")"
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
# Public: render a message without sending it.
|
|
416
|
+
dsh::notify::render() {
|
|
417
|
+
if [[ "${1:-}" == --help ]]; then
|
|
418
|
+
_dsh_private_notify_help render
|
|
419
|
+
return $?
|
|
420
|
+
fi
|
|
421
|
+
if (( $# < 1 )); then
|
|
422
|
+
dsh::core::fail "$DSH_RC_USAGE" 'dsh::notify: expected <event>'
|
|
423
|
+
return $?
|
|
424
|
+
fi
|
|
425
|
+
local event="$1"
|
|
426
|
+
shift
|
|
427
|
+
|
|
428
|
+
local -A counters=() lists=() messages=()
|
|
429
|
+
_dsh_private_notify_parse counters lists messages "$@" || return $?
|
|
430
|
+
|
|
431
|
+
local formatter="${_dsh_private_notify_formatters[$event]:-}"
|
|
432
|
+
if [[ -n "$formatter" ]]; then
|
|
433
|
+
"$formatter" "$event" counters lists messages
|
|
434
|
+
return $?
|
|
435
|
+
fi
|
|
436
|
+
_dsh_private_notify_default_render "$event" counters lists messages
|
|
437
|
+
}
|
|
438
|
+
|
|
439
|
+
# Public: render, cap and dispatch through a registered transport.
|
|
440
|
+
dsh::notify::send() {
|
|
441
|
+
if [[ "${1:-}" == --help ]]; then
|
|
442
|
+
_dsh_private_notify_help send
|
|
443
|
+
return $?
|
|
444
|
+
fi
|
|
445
|
+
if (( $# < 1 )); then
|
|
446
|
+
dsh::core::fail "$DSH_RC_USAGE" 'dsh::notify: expected <event>'
|
|
447
|
+
return $?
|
|
448
|
+
fi
|
|
449
|
+
local event="$1" transport=''
|
|
450
|
+
shift
|
|
451
|
+
|
|
452
|
+
while [[ "${1:-}" == --transport ]]; do
|
|
453
|
+
if (( $# < 2 )); then
|
|
454
|
+
dsh::core::fail "$DSH_RC_USAGE" 'dsh::notify: option requires a value: %1$s' "$1"
|
|
455
|
+
return $?
|
|
456
|
+
fi
|
|
457
|
+
transport="$2"
|
|
458
|
+
shift 2
|
|
459
|
+
done
|
|
460
|
+
|
|
461
|
+
if [[ -z "$transport" ]]; then
|
|
462
|
+
if (( ${#_dsh_private_notify_transports[@]} == 1 )); then
|
|
463
|
+
transport="${!_dsh_private_notify_transports[@]}"
|
|
464
|
+
elif (( ${#_dsh_private_notify_transports[@]} == 0 )); then
|
|
465
|
+
dsh::core::fail "$DSH_RC_USAGE" 'dsh::notify: no transport registered'
|
|
466
|
+
return $?
|
|
467
|
+
else
|
|
468
|
+
dsh::core::fail "$DSH_RC_USAGE" 'dsh::notify: transport required; use --transport NAME'
|
|
469
|
+
return $?
|
|
470
|
+
fi
|
|
471
|
+
fi
|
|
472
|
+
|
|
473
|
+
local func="${_dsh_private_notify_transports[$transport]:-}"
|
|
474
|
+
if [[ -z "$func" ]]; then
|
|
475
|
+
dsh::core::fail "$DSH_RC_USAGE" 'dsh::notify: unknown transport: %1$s' "$transport"
|
|
476
|
+
return $?
|
|
477
|
+
fi
|
|
478
|
+
|
|
479
|
+
local message cap transport_cap
|
|
480
|
+
message="$(dsh::notify::render "$event" "$@")" || return $?
|
|
481
|
+
cap="$(_dsh_private_notify_cfg_get max_chars)"
|
|
482
|
+
transport_cap="${_dsh_private_notify_limits[$transport]:-0}"
|
|
483
|
+
if (( transport_cap > 0 )) && { (( cap == 0 )) || (( transport_cap < cap )); }; then
|
|
484
|
+
cap="$transport_cap"
|
|
485
|
+
fi
|
|
486
|
+
message="$(_dsh_private_notify_fit "$message" "$cap" \
|
|
487
|
+
"$(_dsh_private_notify_cfg_get head_lines)" \
|
|
488
|
+
"$(_dsh_private_notify_cfg_get tail_lines)")"
|
|
489
|
+
|
|
490
|
+
"$func" "$event" "$transport" <<< "$message"
|
|
491
|
+
}
|
|
492
|
+
|
|
493
|
+
# Public: convert a flat stats::result array into the protocol and send it.
|
|
494
|
+
dsh::notify::from_result() {
|
|
495
|
+
if [[ "${1:-}" == --help ]]; then
|
|
496
|
+
_dsh_private_notify_help from_result
|
|
497
|
+
return $?
|
|
498
|
+
fi
|
|
499
|
+
if (( $# < 1 )); then
|
|
500
|
+
dsh::core::fail "$DSH_RC_USAGE" 'dsh::notify: expected <array_name>'
|
|
501
|
+
return $?
|
|
502
|
+
fi
|
|
503
|
+
|
|
504
|
+
local _dsh_private_notify_source_name="$1"
|
|
505
|
+
shift
|
|
506
|
+
if [[ "$_dsh_private_notify_source_name" == _dsh_private_notify_* ]] || ! _dsh_private_notify_name_ok "$_dsh_private_notify_source_name"; then
|
|
507
|
+
dsh::core::fail "$DSH_RC_USAGE" 'dsh::notify: unsafe variable name: %1$s' "$_dsh_private_notify_source_name"
|
|
508
|
+
return $?
|
|
509
|
+
fi
|
|
510
|
+
if ! declare -p "$_dsh_private_notify_source_name" >/dev/null 2>&1; then
|
|
511
|
+
dsh::core::fail "$DSH_RC_USAGE" 'dsh::notify: unsafe variable name: %1$s' "$_dsh_private_notify_source_name"
|
|
512
|
+
return $?
|
|
513
|
+
fi
|
|
514
|
+
|
|
515
|
+
local -n _dsh_private_notify_source="$_dsh_private_notify_source_name"
|
|
516
|
+
local _dsh_private_notify_event='done' _dsh_private_notify_transport=''
|
|
517
|
+
while (( $# )); do
|
|
518
|
+
case "$1" in
|
|
519
|
+
--event|--transport)
|
|
520
|
+
if (( $# < 2 )); then
|
|
521
|
+
dsh::core::fail "$DSH_RC_USAGE" 'dsh::notify: option requires a value: %1$s' "$1"
|
|
522
|
+
return $?
|
|
523
|
+
fi
|
|
524
|
+
if [[ "$1" == --event ]]; then
|
|
525
|
+
_dsh_private_notify_event="$2"
|
|
526
|
+
else
|
|
527
|
+
_dsh_private_notify_transport="$2"
|
|
528
|
+
fi
|
|
529
|
+
shift 2
|
|
530
|
+
;;
|
|
531
|
+
*)
|
|
532
|
+
dsh::core::fail "$DSH_RC_USAGE" 'dsh::notify: invalid option: %1$s' "$1"
|
|
533
|
+
return $?
|
|
534
|
+
;;
|
|
535
|
+
esac
|
|
536
|
+
done
|
|
537
|
+
|
|
538
|
+
local -a _dsh_private_notify_payload=() _dsh_private_notify_list_items=() _dsh_private_notify_message_items=()
|
|
539
|
+
local _dsh_private_notify_key _dsh_private_notify_value _dsh_private_notify_rest
|
|
540
|
+
local _dsh_private_notify_name _dsh_private_notify_index
|
|
541
|
+
for _dsh_private_notify_key in "${!_dsh_private_notify_source[@]}"; do
|
|
542
|
+
_dsh_private_notify_value="${_dsh_private_notify_source[$_dsh_private_notify_key]}"
|
|
543
|
+
case "$_dsh_private_notify_key" in
|
|
544
|
+
counter.*)
|
|
545
|
+
_dsh_private_notify_payload+=("${_dsh_private_notify_key#counter.}=$_dsh_private_notify_value")
|
|
546
|
+
;;
|
|
547
|
+
list.*.*)
|
|
548
|
+
_dsh_private_notify_rest="${_dsh_private_notify_key#list.}"
|
|
549
|
+
_dsh_private_notify_name="${_dsh_private_notify_rest%.*}"
|
|
550
|
+
_dsh_private_notify_index="${_dsh_private_notify_rest##*.}"
|
|
551
|
+
[[ "$_dsh_private_notify_index" =~ ^[0-9]+$ ]] || continue
|
|
552
|
+
_dsh_private_notify_list_items[_dsh_private_notify_index]="list:${_dsh_private_notify_name}=$_dsh_private_notify_value"
|
|
553
|
+
;;
|
|
554
|
+
msg.*.*)
|
|
555
|
+
_dsh_private_notify_rest="${_dsh_private_notify_key#msg.}"
|
|
556
|
+
_dsh_private_notify_name="${_dsh_private_notify_rest%.*}"
|
|
557
|
+
_dsh_private_notify_index="${_dsh_private_notify_rest##*.}"
|
|
558
|
+
[[ "$_dsh_private_notify_index" =~ ^[0-9]+$ ]] || continue
|
|
559
|
+
_dsh_private_notify_message_items[_dsh_private_notify_index]="msg:${_dsh_private_notify_name}=$_dsh_private_notify_value"
|
|
560
|
+
;;
|
|
561
|
+
esac
|
|
562
|
+
done
|
|
563
|
+
for _dsh_private_notify_value in "${_dsh_private_notify_list_items[@]}"; do
|
|
564
|
+
_dsh_private_notify_payload+=("$_dsh_private_notify_value")
|
|
565
|
+
done
|
|
566
|
+
for _dsh_private_notify_value in "${_dsh_private_notify_message_items[@]}"; do
|
|
567
|
+
_dsh_private_notify_payload+=("$_dsh_private_notify_value")
|
|
568
|
+
done
|
|
569
|
+
|
|
570
|
+
if [[ -n "$_dsh_private_notify_transport" ]]; then
|
|
571
|
+
dsh::notify::send "$_dsh_private_notify_event" --transport "$_dsh_private_notify_transport" \
|
|
572
|
+
"${_dsh_private_notify_payload[@]}"
|
|
573
|
+
else
|
|
574
|
+
dsh::notify::send "$_dsh_private_notify_event" "${_dsh_private_notify_payload[@]}"
|
|
575
|
+
fi
|
|
576
|
+
}
|