/
hipcat.go
193 lines (165 loc) · 3.85 KB
/
hipcat.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
// Copyright 2014 Paul Hammond.
// This software is licensed under the MIT license, see LICENSE.txt for details.
package main
import (
"bufio"
"bytes"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"log"
"net/http"
"net/url"
"os"
"os/user"
"strings"
"github.com/ogier/pflag"
)
// Config defines structure of the config file.
type Config struct {
HipchatURL string `json:"hipchat_url"`
Room string `json:"room"`
APIToken string `json:"api_token"`
}
// Load loads in a found config file.
func (c *Config) Load() error {
err := c.loadConfigFiles()
if err != nil {
return err
}
c.loadEnvVars()
if c.HipchatURL == "" {
return errors.New("Could not find a HipchatURL in HIPCHAT_URL, /etc/hipcat.conf, /.hipcat.conf, ./hipcat.conf")
}
if c.APIToken == "" {
return errors.New("Could not find an APIToken in HIPCAT_API_TOKEN, /etc/hipcat.conf, /.hipcat.conf, ./hipcat.conf")
}
return nil
}
func (c *Config) loadEnvVars() {
envs := []string{"HIPCHAT_URL", "HIPCAT_ROOM", "HIPCAT_API_TOKEN"}
for _, env := range envs {
envVal := os.Getenv(env)
if envVal == "" {
continue
}
switch env {
case "HIPCHAT_URL":
c.HipchatURL = envVal
case "HIPCAT_ROOM":
c.Room = envVal
case "HIPCAT_API_TOKEN":
c.APIToken = envVal
}
}
}
func (c *Config) loadConfigFiles() error {
homeDir := ""
usr, err := user.Current()
if err == nil {
homeDir = usr.HomeDir
}
for _, path := range []string{"/etc/hipcat.conf", homeDir + "/.hipcat.conf", "./hipcat.conf"} {
file, err := os.Open(path)
if os.IsNotExist(err) {
continue
}
if err != nil {
return err
}
err = json.NewDecoder(file).Decode(c)
if err != nil {
return err
}
}
return nil
}
func (c *Config) bindFlags() {
pflag.StringVarP(&c.Room, "room", "r", c.Room, "room")
}
// RoomMessage contains the message for the Hipchat room.
type RoomMessage struct {
Message string `json:"message"`
}
// Encode encodes the RoomMessage into a json string.
func (m RoomMessage) Encode() (string, error) {
b, err := json.Marshal(m)
if err != nil {
return "", err
}
return string(b), nil
}
// Post posts the RoomMessage to the Hipchat server.
func (m RoomMessage) Post(config *Config) error {
encoded, err := m.Encode()
if err != nil {
return err
}
URL, err := url.Parse(fmt.Sprintf("%s/v2/room/%s/message",
config.HipchatURL, config.Room))
if err != nil {
return err
}
var req *http.Request
req, err = http.NewRequest("POST", URL.String(), bytes.NewBuffer([]byte(encoded)))
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", config.APIToken))
req.Header.Set("Content-Type", "application/json")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusCreated {
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
return fmt.Errorf("Not OK: %d, %s", resp.StatusCode, body)
}
return nil
}
func main() {
pflag.Usage = func() {
fmt.Fprintln(os.Stderr, "Usage: hipcat [-r room] [message]")
}
cfg := Config{}
err := cfg.Load()
if err != nil {
log.Fatalf("Failed to load config: %v", err)
}
cfg.bindFlags()
pflag.Parse()
if cfg.Room == "" {
log.Println("Could not find a Room in HIPCAT_ROOM, /etc/hipcat.conf, /.hipcat.conf, ./hipcat.conf or passed with -r")
pflag.Usage()
os.Exit(1)
}
// was there a message on the command line? If so use it.
args := pflag.Args()
if len(args) > 0 {
msg := RoomMessage{
Message: strings.Join(args, " "),
}
err = msg.Post(&cfg)
if err != nil {
log.Fatalf("Post failed: %v", err)
}
return
}
// ...Otherwise scan stdin
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
msg := RoomMessage{
Message: scanner.Text(),
}
err = msg.Post(&cfg)
if err != nil {
log.Fatalf("Post failed: %v", err)
}
}
if err = scanner.Err(); err != nil {
log.Fatalf("Error reading: %v", err)
}
}