-
Notifications
You must be signed in to change notification settings - Fork 56
/
slackcat.go
150 lines (128 loc) · 3.3 KB
/
slackcat.go
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
package main
import (
"fmt"
"os"
"os/signal"
"strconv"
"strings"
"time"
"github.com/slack-go/slack"
)
//Slackcat client
type Slackcat struct {
queue *StreamQ
shutdown chan os.Signal
username string
iconEmoji string
channelID string
channelName string
}
func newSlackcat(username, iconEmoji, channelname string) *Slackcat {
sc := &Slackcat{
queue: newStreamQ(),
shutdown: make(chan os.Signal, 1),
username: username,
iconEmoji: iconEmoji,
channelName: channelname,
}
sc.channelID = lookupSlackID(sc.channelName)
signal.Notify(sc.shutdown, os.Interrupt)
return sc
}
func (sc *Slackcat) trap() {
sigcount := 0
for sig := range sc.shutdown {
if sigcount > 0 {
exitErr(fmt.Errorf("aborted"))
}
output(fmt.Sprintf("got signal: %s", sig.String()))
output("press ctrl+c again to exit immediately")
sigcount++
go sc.exit()
}
}
func (sc *Slackcat) exit() {
for {
if sc.queue.IsEmpty() {
os.Exit(0)
} else {
output("flushing remaining messages to Slack...")
time.Sleep(3 * time.Second)
}
}
}
func (sc *Slackcat) stream(lines chan string) {
output("starting stream")
go func() {
for line := range lines {
sc.queue.Add(line)
}
sc.exit()
}()
go sc.processStreamQ()
go sc.trap()
select {}
}
func (sc *Slackcat) processStreamQ() {
if !(sc.queue.IsEmpty()) {
msglines := sc.queue.Flush()
if noop {
output(fmt.Sprintf("skipped posting of %s message lines to %s", strconv.Itoa(len(msglines)), sc.channelName))
} else {
sc.postMsg(msglines)
}
sc.queue.Ack()
}
time.Sleep(3 * time.Second)
sc.processStreamQ()
}
var CurMsgTS string
func (sc *Slackcat) postMsg(msglines []string) {
msg := strings.Join(msglines, "\n")
msg = strings.Replace(msg, "&", "%26amp%3B", -1)
msg = strings.Replace(msg, "<", "%26lt%3B", -1)
msg = strings.Replace(msg, ">", "%26gt%3B", -1)
msgOpts := []slack.MsgOption{slack.MsgOptionText(msg, false)}
if sc.username != "" {
msgOpts = append(msgOpts, slack.MsgOptionAsUser(false))
msgOpts = append(msgOpts, slack.MsgOptionUsername(sc.username))
} else {
msgOpts = append(msgOpts, slack.MsgOptionAsUser(true))
}
if sc.iconEmoji != "" {
msgOpts = append(msgOpts, slack.MsgOptionIconEmoji(sc.iconEmoji))
}
if thread {
if CurMsgTS != "" {
msgOpts = append(msgOpts, slack.MsgOptionTS(CurMsgTS))
}
}
var err error
_, CurMsgTS, err = api.PostMessage(sc.channelID, msgOpts...)
failOnError(err)
count := strconv.Itoa(len(msglines))
output(fmt.Sprintf("posted %s message lines to %s", count, sc.channelName))
}
func (sc *Slackcat) postFile(filePath, fileName, fileType, fileComment string) {
//default to timestamp for filename
if fileName == "" {
fileName = strconv.FormatInt(time.Now().Unix(), 10)
}
if noop {
output(fmt.Sprintf("skipping upload of file %s to %s", fileName, sc.channelName))
return
}
start := time.Now()
_, err := api.UploadFile(slack.FileUploadParameters{
File: filePath,
Filename: fileName,
Filetype: fileType,
Title: fileName,
InitialComment: fileComment,
Channels: []string{sc.channelID},
})
failOnError(err, "error uploading file to Slack")
duration := strconv.FormatFloat(time.Since(start).Seconds(), 'f', 3, 64)
output(fmt.Sprintf("file %s uploaded to %s (%ss)", fileName, sc.channelName, duration))
os.Exit(0)
}