-
Notifications
You must be signed in to change notification settings - Fork 2
/
with_slack_message
executable file
·85 lines (72 loc) · 2.2 KB
/
with_slack_message
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
#!/usr/bin/env bash
set -eu -o pipefail
script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
# shellcheck source=slack.bash
source "$script_dir/slack.bash"
help() {
cat << 'HELP'
Usage: with_slack_message --channel CHANNEL
[--message-on-start MESSAGE] [--message-on-cancel MESSAGE]
[--message-on-success MESSAGE] [--message-on-fail MESSAGE]
[--user-name NAME] [--user-icon URL]
COMMAND
Run command and post slack message
HELP
}
while test "$#" -gt 0; do
case "$1" in
--help ) help; exit 0 ;;
--channel ) channel=$2; shift 2 ;;
--message-on-start ) message_on_start=$2; shift 2 ;;
--message-on-cancel ) message_on_cancel=$2; shift 2 ;;
--message-on-success ) message_on_success=$2; shift 2 ;;
--message-on-fail ) message_on_fail=$2; shift 2 ;;
--user-name ) user_name=$2; shift 2 ;;
--user-icon ) user_icon=$2; shift 2 ;;
--* ) echo "error: unknown option $1"; exit 1 ;;
* ) break ;;
esac
done
: "${SLACK_API_TOKEN?}"
: "${channel?}"
: "${message_on_start:=}"
: "${message_on_cancel:=}"
: "${message_on_success:=}"
: "${message_on_fail:=}"
: "${user_name:="Bot"}"
: "${user_icon:="https://upload.wikimedia.org/wikipedia/commons/thumb/c/cd/GNOME_Builder_Icon_%28hicolor%29.svg/240px-GNOME_Builder_Icon_%28hicolor%29.svg.png"}"
if test "$#" -eq 0; then
help >&2
exit 1
fi
post_message_if_not_empty() {
local channel="${1?}"
local message="${2?}"
if test -n "$message"; then
post_text_message --channel "$channel" --text "$message" \
--user-name "$user_name" --user-icon "$user_icon"
fi
}
on_exit() {
local exit_status="$?"
if test "$exit_status" -eq 0; then
post_message_if_not_empty "$channel" "$message_on_success"
else
post_message_if_not_empty "$channel" "$message_on_fail"
fi
return "$exit_status"
}
on_cancel() {
local exit_status=0
trap -- EXIT
kill -s HUP "$pid"
wait "$pid" || exit_status=$?
post_message_if_not_empty "$channel" "$message_on_cancel"
return "$exit_status"
}
trap 'on_exit' EXIT
trap 'on_cancel' HUP INT QUIT TERM
post_message_if_not_empty "$channel" "$message_on_start"
"$@" &
pid=$!
wait "$pid"