-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmelt.go
226 lines (181 loc) · 4.36 KB
/
melt.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
package main
import (
"bytes"
"encoding/json"
"flag"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"net/url"
"os"
)
var (
conf = flag.String("conf", os.Getenv("HOME")+"/.melt", "Path to the config")
meltKey = flag.String("key", "", "Define the key of the link to send to melt host")
head = flag.String("head", "", "Specify a header that will be in top of the input")
hip = flag.Bool("hip", false, "Enable Use of HipChat")
hipRoom = flag.String("room", "", "HipChat: Name of the room to send the input to on HipChat")
//hipTitle = flag.String("title", "", "HipChat: Title of your message in HipChat")
hipMelt = flag.Bool("melt", false, "HipChat: Melt content and give a link to content")
// command = flag.String("command", "", "Enter a command to execute")
)
const (
MeltDefault = "https://melt.grindvoll.org"
HipChatDefault = "https://api.hipchat.com/v2"
)
func init() {
flag.Parse()
}
type ConfigData struct {
MeltHost string
HipChatHost string
HipChatToken string
}
type MeltResponse struct {
Key string `json:"key"`
Data string `json:"data"`
Ok bool `json:"ok"`
Message string `json:"message"`
}
// {"message":"'"$MESSAGE"'","message_format":"html","color":"yellow","notify":false}'
type HipChatData struct {
Format string `json:"message_format"`
Color string `json:"color"`
Notify bool `json:"notify"`
Message string `json:"message"`
}
type HipResponse map[string]interface{}
/*type HipResponse struct {
Code int
Message string
Type string
}
*/
func readConfig(f string) *ConfigData {
c := new(ConfigData)
var file *os.File
defer file.Close()
file, err := os.Open(f)
if os.IsNotExist(err) {
c.MeltHost = MeltDefault
c.HipChatHost = HipChatDefault
file, err := os.Create(f)
if err != nil {
log.Fatalln(err)
}
fEnc := json.NewEncoder(file)
fEnc.Encode(&c)
} else if err != nil {
log.Fatalln(err)
} else {
cDec := json.NewDecoder(file)
err = cDec.Decode(c)
if err != nil {
log.Fatalln(err)
}
}
return c
}
// readStdin will read data from stdin so we can safely output the data in json later
func readStdin() []byte {
in, err := ioutil.ReadAll(os.Stdin)
if err != nil {
log.Fatalln(err)
}
return in
}
func addHead(head string) io.Reader {
byteHead := []byte(head + "\n")
return io.MultiReader(bytes.NewReader(byteHead), os.Stdin)
}
func meltPost(key, head string, c *ConfigData) (url string) {
resp := new(http.Response)
var postMeltTo string
var Data io.Reader
if key == "" {
postMeltTo = c.MeltHost + "/documents"
} else {
postMeltTo = c.MeltHost + "/documents/custom/" + key
}
if head != "" {
Data = addHead(head)
} else {
Data = os.Stdin
}
resp, err := http.Post(postMeltTo, "text/plain", Data)
defer resp.Body.Close()
if err != nil {
log.Fatalln(err)
}
rDec := json.NewDecoder(resp.Body)
m := new(MeltResponse)
err = rDec.Decode(m)
if err != nil {
log.Fatalln(err)
}
if m.Ok != true {
log.Fatalln("Error:", m.Message)
}
url = c.MeltHost + "/" + m.Key
return
}
func hipRoomPost(room, key, head string, short bool, c *ConfigData) {
resp := new(http.Response)
var hip HipChatData
var vHip = make(url.Values)
vHip.Add("auth_token", c.HipChatToken)
postHipTo, err := url.Parse(c.HipChatHost + "/room/" + room + "/notification")
if err != nil {
log.Fatalln(err)
}
postHipTo.RawQuery = vHip.Encode()
hip.Color = "yellow"
hip.Format = "text"
hip.Notify = false
if head != "" {
hip.Message = fmt.Sprintf("%s\n", head)
}
if short {
hip.Message += meltPost(key, head, c)
} else {
s, err := ioutil.ReadAll(os.Stdin)
if err != nil {
log.Fatalln(err)
}
hip.Message += string(s)
}
bHip, err := json.Marshal(&hip)
if err != nil {
log.Fatalln(err)
}
resp, err = http.Post(postHipTo.String(), "application/json", bytes.NewReader(bHip))
defer resp.Body.Close()
if err != nil {
log.Fatalln(err)
}
rDec := json.NewDecoder(resp.Body)
h := make(HipResponse)
rDec.Decode(&h)
// fmt.Println(postHipTo)
if len(h) > 0 {
fmt.Println(h)
}
}
func main() {
Config := readConfig(*conf)
//fmt.Printf("%s", Config.HipChatToken)
//fmt.Println(getRoom())
// fmt.Println(string(readStdin()))
if *hip {
if *hipRoom != "" {
hipRoomPost(*hipRoom, *meltKey, *head, *hipMelt, Config)
} else {
log.Fatalln("HipChat: No room specified")
flag.PrintDefaults()
}
} else {
fmt.Println(meltPost(*meltKey, *head, Config))
}
}