-
Notifications
You must be signed in to change notification settings - Fork 14
/
utils.sh
executable file
·181 lines (140 loc) · 3.54 KB
/
utils.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#!/bin/sh
#
# A utility script for
#
#####################################################################
SCRIPT_NAME="$0"
SCRIPT_DIR="$(cd "$(dirname "$0")"; pwd)"
ARGS=("$@")
# ------------------------------------------------------------------------
# script functions
do_usage_mini() {
cat >&2 <<EOF
Usage:
$SCRIPT_NAME <options> [ channels | channel-info <channel-id> | invite <email> <channel-ids> | team-info ]
Options:
-d, --domain The Slack team domain
-t, --token The Slack authentication token
-v, --verbose Display verbose output
EOF
}
do_usage() {
do_usage_mini
cat >&2 <<EOF
Description:
A utility script that can be used with a Slack domain and authentication
token to access Slack API features.
If you prefer, you can provide the Slack team domain and token as shell
environment variables: SLACK_DOMAIN and SLACK_TOKEN.
Commands:
channels
List all Slack channel names and ids for this team.
channel-info <channel-id>
List all Slack channel names and ids for this team.
invite <email> <channel-ids>
Invite a user to one or more channels for this Slack team.
team-info
Get information about this Slack team.
EOF
}
log_error() {
>&2 echo "ERROR: $1"
}
_slack_rpc() {
local SLACK_URL="https://${SLACK_DOMAIN}.slack.com/api/$1"
local PAYLOAD="$2"
local JQ_PARAM="$3"
curl ${OUTPUT_VERBOSE:--#} -X POST "$SLACK_URL" --data "$PAYLOAD" --compressed | jq "$JQ_PARAM"
}
do_list_channels() {
local PAYLOAD="token=${SLACK_TOKEN}&exclude_archived=1"
_slack_rpc "channels.list" "$PAYLOAD" '.channels[] | {name: .name, id: .id}'
}
do_channel_info() {
# TODO check number of params
local CHANNEL="${PARAMS[0]}"
if [ -z "$CHANNEL" ]; then
do_usage_mini
log_error "ERROR: The Slack channel id must be supplied as a parameter."
exit 2
fi
local PAYLOAD="token=${SLACK_TOKEN}&channel=$CHANNEL"
_slack_rpc "channels.info" "$PAYLOAD"
}
do_team_info() {
local PAYLOAD="token=${SLACK_TOKEN}"
_slack_rpc "team.info" "$PAYLOAD"
}
do_invite_email() {
# TODO check number of params
local EMAIL="${PARAMS[0]}"
local CHANNELS="${PARAMS[1]}"
if [ -z "$EMAIL" ]; then
do_usage_mini
log_error "ERROR: An email address must be supplied as the first parameter."
exit 2
fi
local PAYLOAD="token=${SLACK_TOKEN}&email=$EMAIL&channels=$CHANNELS"
_slack_rpc "users.admin.invite" "$PAYLOAD"
}
# ------------------------------------------------------------------------
# read the options
while getopts ":d:t:v" opt; do
case $opt in
d)
SLACK_DOMAIN="$OPTARG";
;;
t)
SLACK_TOKEN="$OPTARG";
;;
v)
OUTPUT_VERBOSE="-vvv";
;;
*)
;;
esac
done
# Extract the overflowing arguments
PARAMS=("${ARGS[@]:$OPTIND}")
CMD_INDEX=`expr $OPTIND - 1`
CMD=${ARGS[ $CMD_INDEX ]}
# ------------------------------------------------------------------------
#
if [ ! `command -v jq` ]; then
log_error "The 'jq' command was not found."
exit 1
fi
if [ ! `command -v curl` ]; then
log_error "The 'curl' command was not found."
exit 1
fi
if [ -z "$SLACK_DOMAIN" ]; then
do_usage
log_error "Please set the Slack domain parameter."
exit 1
fi
if [ -z "$SLACK_TOKEN" ]; then
do_usage
log_error "Please set the Slack token parameter."
exit 1
fi
# ------------------------------------------------------------------------
#
case "$CMD" in
channels)
do_list_channels
;;
channel-info)
do_channel_info
;;
invite)
do_invite_email
;;
team-info)
do_team_info
;;
*)
do_usage
;;
esac
exit 0