-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflowdocktee.sh
executable file
·78 lines (69 loc) · 1.29 KB
/
flowdocktee.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
#!/usr/bin/env bash
api_token=""
flow=""
organization=""
textWrapper="\`\`\`"
function escape_string() {
local result=$(echo "$1" \
| sed 's/\\/\\\\/g' \
| sed 's/"/\\"/g' \
| sed "s/'/\\'/g")
echo "$result"
}
function parse_arguments() {
while [[ $# -gt 0 ]]; do
opt=$1
shift
case "$opt" in
--config)
CONFIG=$1
shift
;;
--notify)
NOTIFY=@team
;;
esac
done
}
function read_and_pipe_line() {
echo "$1"
line=$(escape_string "$1")
if [[ -z "$text" ]]; then
text="$line"
else
text="$text\n$line"
fi
}
function send() {
message="$NOTIFY\n$textWrapper\n$1\n$textWrapper"
json="{\
\"event\": \"message\", \
\"content\": \"$message\" \
}"
curl \
-H "Content-Type: application/json" \
-X POST \
-d "$json" "$url" > /dev/null 2>&1
}
function set_environment() {
if [[ -n "$HOME" && -e "$HOME/.flowdocktee" ]]; then
. "$HOME/.flowdocktee"
fi
if [[ -e "$CONFIG" ]]; then
. $CONFIG
fi
}
function set_url() {
url="https://"$api_token"@api.flowdock.com/flows/"$organization"/"$flow"/messages"
}
function main() {
parse_arguments "$@"
set_environment
set_url
text=""
while IFS='' read -r line; do
read_and_pipe_line "$line"
done
send "$text"
}
main "$@"