-
Notifications
You must be signed in to change notification settings - Fork 56
/
main.go
243 lines (216 loc) · 5.2 KB
/
main.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
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
package main
import (
"bufio"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"github.com/urfave/cli"
)
var (
noop = false
build = ""
version = "dev-build"
thread = false
)
type StdinScanner struct {
*bufio.Scanner
tee bool
}
func NewStdinScanner(tee bool) *StdinScanner {
return &StdinScanner{bufio.NewScanner(os.Stdin), tee}
}
func (s *StdinScanner) StreamBytes() chan []byte {
ch := make(chan []byte)
s.Split(bufio.ScanBytes)
go func() {
for s.Scan() {
b := s.Bytes()
ch <- b
if s.tee {
fmt.Printf("%s", b)
}
}
failOnError(s.Err(), "error reading input")
close(ch)
}()
return ch
}
func (s *StdinScanner) StreamLines() chan string {
ch := make(chan string)
s.Split(bufio.ScanLines)
go func() {
for s.Scan() {
ch <- s.Text()
if s.tee {
fmt.Println(s.Text())
}
}
failOnError(s.Err(), "error reading input")
close(ch)
}()
return ch
}
func writeTemp(byteCh chan []byte) string {
tmp, err := ioutil.TempFile(os.TempDir(), "slackcat-")
failOnError(err, "unable to create tmpfile")
w := bufio.NewWriter(tmp)
for b := range byteCh {
_, err := w.Write(b)
failOnError(err, "error writing to tmpfile")
}
w.Flush()
return tmp.Name()
}
func handleUsageError(c *cli.Context, err error, _ bool) error {
fmt.Fprintf(c.App.Writer, "%s %s\n\n", "Incorrect Usage.", err.Error())
cli.ShowAppHelp(c)
return cli.NewExitError("", 1)
}
func printFullVersion(c *cli.Context) {
fmt.Fprintf(c.App.Writer, "%v version %v, build %v\n", c.App.Name, c.App.Version, build)
}
func main() {
cli.VersionPrinter = printFullVersion
app := cli.NewApp()
app.Name = "slackcat"
app.Usage = "redirect a file to slack"
app.Version = version
app.OnUsageError = handleUsageError
app.Flags = []cli.Flag{
cli.StringFlag{
Name: "channel, c",
Usage: "Slack channel or group to post to",
},
cli.StringFlag{
Name: "comment",
Usage: "Initial comment for snippet",
},
cli.BoolFlag{
Name: "configure",
Usage: "Configure Slackcat via oauth",
},
cli.StringFlag{
Name: "filename, n",
Usage: "Filename for upload. Defaults to current timestamp",
},
cli.StringFlag{
Name: "filetype",
Usage: "Specify filetype for syntax highlighting",
},
cli.BoolFlag{
Name: "list",
Usage: "List team channel names",
},
cli.BoolFlag{
Name: "noop",
Usage: "Skip posting file to Slack. Useful for testing",
},
cli.BoolFlag{
Name: "stream, s",
Usage: "Stream messages to Slack continuously instead of uploading a single snippet",
},
cli.BoolFlag{
Name: "tee, t",
Usage: "Print stdin to screen before posting",
},
cli.StringFlag{
Name: "token",
Usage: "Optional Slack token to use, ignoring config file",
},
cli.StringFlag{
Name: "username, u",
Usage: "Stream messages as given bot user. Defaults to auth user",
},
cli.StringFlag{
Name: "iconemoji, i",
Usage: "Stream messages as given bot icon emoji. Defaults to auth user's icon",
},
cli.BoolFlag{
Name: "thread",
Usage: "Send subsequent messages as threaded reply to orignial message",
},
}
app.Action = func(c *cli.Context) {
var config *Config
if c.Bool("configure") {
configureOA()
os.Exit(0)
}
if c.String("token") != "" {
config = &Config{
Teams: map[string]string{
"default": c.String("token"),
},
DefaultTeam: "default",
}
} else {
configPath, exists := getConfigPath()
if !exists {
exitErr(fmt.Errorf("missing config file at %s\nuse --configure to create", configPath))
}
config = ReadConfig(configPath)
}
if c.Bool("list") {
for teamName, token := range config.Teams {
InitAPI(token)
for _, n := range listChannels() {
fmt.Printf("[%s] [channel] %s\n", teamName, n)
}
for _, n := range listGroups() {
fmt.Printf("[%s] [group] %s\n", teamName, n)
}
for _, n := range listIms() {
fmt.Printf("[%s] [im] %s\n", teamName, n)
}
}
os.Exit(0)
}
team, channel, err := config.parseChannelOpt(c.String("channel"))
failOnError(err)
noop = c.Bool("noop")
thread = c.Bool("thread")
username := c.String("username")
iconEmoji := c.String("iconemoji")
fileName := c.String("filename")
fileType := c.String("filetype")
fileComment := c.String("comment")
token := config.Teams[team]
if token == "" {
exitErr(fmt.Errorf("no such team: %s", team))
}
InitAPI(token)
slackcat := newSlackcat(username, iconEmoji, channel)
if len(c.Args()) > 0 {
if c.Bool("stream") {
output("filepath provided, ignoring stream option")
}
filePath := c.Args()[0]
if fileName == "" {
fileName = filepath.Base(filePath)
}
slackcat.postFile(filePath, fileName, fileType, fileComment)
os.Exit(0)
}
scanner := NewStdinScanner(c.Bool("tee"))
if c.Bool("stream") {
// If threaded then send comment first to start thread
if thread {
var s []string
if fileComment != "" {
s = append(s, fileComment)
} else {
s = append(s, "Slackcat Stream Output:")
}
slackcat.postMsg(s)
}
slackcat.stream(scanner.StreamLines())
} else {
filePath := writeTemp(scanner.StreamBytes())
defer os.Remove(filePath)
slackcat.postFile(filePath, fileName, fileType, fileComment)
os.Exit(0)
}
}
app.Run(os.Args)
}