forked from paulhammond/slackcat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
slackcat.go
163 lines (140 loc) · 3.29 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
151
152
153
154
155
156
157
158
159
160
161
162
163
// Copyright 2014 Paul Hammond.
// This software is licensed under the MIT license, see LICENSE.txt for details.
package main
import (
"bufio"
"encoding/json"
"errors"
"fmt"
"log"
"net/http"
"net/url"
"os"
"os/user"
"strings"
"github.com/ogier/pflag"
)
type Config struct {
WebhookUrl string `json:"webhook_url"`
Channel string `json:"channel"`
Username *string `json:"username"`
}
func ReadConfig() (*Config, error) {
homeDir := ""
usr, err := user.Current()
if err == nil {
homeDir = usr.HomeDir
}
for _, path := range []string{"/etc/slackcat.conf", homeDir + "/.slackcat.conf", "./slackcat.conf"} {
file, err := os.Open(path)
if os.IsNotExist(err) {
continue
}
if err != nil {
return nil, err
}
json.NewDecoder(file)
conf := Config{}
err = json.NewDecoder(file).Decode(&conf)
if err != nil {
return nil, err
}
return &conf, nil
}
return nil, errors.New("Config file not found")
}
type SlackMsg struct {
Channel string `json:"channel"`
Username string `json:"username,omitempty"`
Text string `json:"text"`
Parse string `json:"parse"`
IconEmoji string `json:"icon_emoji,omitempty"`
}
func (m SlackMsg) Encode() (string, error) {
b, err := json.Marshal(m)
if err != nil {
return "", err
}
return string(b), nil
}
func (m SlackMsg) Post(WebhookURL string) error {
encoded, err := m.Encode()
if err != nil {
return err
}
resp, err := http.PostForm(WebhookURL, url.Values{"payload": {encoded}})
if err != nil {
return err
}
if resp.StatusCode != http.StatusOK {
return errors.New("Not OK")
}
return nil
}
func username() string {
username := "<unknown>"
usr, err := user.Current()
if err == nil {
username = usr.Username
}
hostname := "<unknown>"
host, err := os.Hostname()
if err == nil {
hostname = host
}
return fmt.Sprintf("%s@%s", username, hostname)
}
func main() {
cfg, err := ReadConfig()
if err != nil {
log.Fatalf("Could not read config: %v", err)
}
// By default use "user@server", unless overridden by config. cfg.Username
// can be "", implying Slack should use the default username, so we have
// to check if the value was set, not just for a non-empty string.
defaultName := username()
if cfg.Username != nil {
defaultName = *cfg.Username
}
pflag.Usage = func() {
fmt.Fprintln(os.Stderr, "Usage: slackcat [-c #channel] [-n name] [-i icon] [message]")
}
channel := pflag.StringP("channel", "c", cfg.Channel, "channel")
name := pflag.StringP("name", "n", defaultName, "name")
icon := pflag.StringP("icon", "i", "", "icon")
pflag.Parse()
// was there a message on the command line? If so use it.
args := pflag.Args()
if len(args) > 0 {
msg := SlackMsg{
Channel: *channel,
Username: *name,
Parse: "full",
Text: strings.Join(args, " "),
IconEmoji: *icon,
}
err = msg.Post(cfg.WebhookUrl)
if err != nil {
log.Fatalf("Post failed: %v", err)
}
return
}
// ...Otherwise scan stdin
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
msg := SlackMsg{
Channel: *channel,
Username: *name,
Parse: "full",
Text: scanner.Text(),
IconEmoji: *icon,
}
err = msg.Post(cfg.WebhookUrl)
if err != nil {
log.Fatalf("Post failed: %v", err)
}
}
if err := scanner.Err(); err != nil {
log.Fatalf("Error reading: %v", err)
}
}