This repository has been archived by the owner on Jun 5, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
249 lines (220 loc) · 6.15 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
244
245
246
247
248
249
package main
import (
"encoding/json"
"errors"
"flag"
"fmt"
"io"
"net"
"os"
"time"
"github.com/pmezard/go-difflib/difflib"
)
var (
version = ""
enableUpdateDiff = true
enableSessionEvents = true
enableSubscriberEvents = true
enableSessionGyEvents = false
enableGroupEvents = false
diffContextLines = 2
)
func init() {
flag.BoolVar(&enableUpdateDiff, "diff", enableUpdateDiff, "Use diff for object.updated events")
flag.IntVar(&diffContextLines, "diff-context", diffContextLines, "Number of lines in diff context")
flag.BoolVar(&enableGroupEvents, "groups", enableGroupEvents, "Subscribe to group events")
flag.BoolVar(&enableSessionGyEvents, "session-gy", enableSessionGyEvents, "Subscribe to session Gy events")
flag.BoolVar(&enableSessionEvents, "sessions", enableSessionEvents, "Subscribe to session events")
flag.BoolVar(&enableSubscriberEvents, "subscribers", enableSubscriberEvents, "Subscribe to subscriber events")
}
func main() {
flag.Usage = func() {
fmt.Println("Usage:")
fmt.Printf(" %s [options] [PSM JSON-RPC address]\n", os.Args[0])
fmt.Println("")
fmt.Println("Options (with their default values):")
flag.PrintDefaults()
fmt.Println("")
fmt.Println("When no address is given, events are read from stdin.")
fmt.Println("")
fmt.Println("Examples:")
fmt.Printf(" %s < events.json # parse and clarify events from a capture file\n", os.Args[0])
fmt.Printf(" %s -diff=false 192.0.2.23:3994 # connect to PSM at 192.0.2.23, subscribe to all events\n", os.Args[0])
}
flag.Parse()
pr := newPeekingDecoder(os.Stdin)
timestampEvents := false
if version != "" {
fmt.Println("psmevents", version)
}
if addr := flag.Arg(0); addr != "" {
if _, _, err := net.SplitHostPort(addr); err != nil {
addr = net.JoinHostPort(addr, "3994")
}
conn, err := net.Dial("tcp", addr)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
pr = newPeekingDecoder(conn)
if enableSessionEvents {
if err := subscribeSessions(conn, pr); err != nil {
fmt.Println(err)
os.Exit(1)
}
fmt.Println("Subscribed to session events")
}
if enableSubscriberEvents {
if err := subscribeSubscribers(conn, pr); err != nil {
fmt.Println(err)
os.Exit(1)
}
fmt.Println("Subscribed to subscriber events")
}
if enableGroupEvents {
if err := subscribeGroups(conn, pr); err != nil {
fmt.Println(err)
os.Exit(1)
}
fmt.Println("Subscribed to group events")
}
if enableSessionGyEvents {
if err := subscribeSessionGy(conn, pr); err != nil {
fmt.Println(err)
os.Exit(1)
}
fmt.Println("Subscribed to session Gy events")
}
timestampEvents = true
}
i := 0
for {
var v interface{}
if err := pr.Decode(&v); err != nil {
if err == io.EOF {
break
}
fmt.Println(err)
return
}
if events, ok := v.([]interface{}); ok {
for _, event := range events {
event, ok := event.(map[string]interface{})
if !ok {
fmt.Println("No an event?")
continue
}
printHeader(i, timestampEvents)
if !enableUpdateDiff || printEventWithDiff(event, i) != nil {
printEvent(event, i)
}
i++
}
} else if event, ok := v.(map[string]interface{}); ok {
printHeader(i, timestampEvents)
if !enableUpdateDiff || printEventWithDiff(event, i) != nil {
printEvent(event, i)
}
i++
}
}
}
func printHeader(i int, timestamp bool) {
if timestamp {
fmt.Printf("*** Event %d at %v ***\n\n", i, time.Now())
} else {
fmt.Printf("*** Event %d ***\n\n", i)
}
}
func printEventWithDiff(event map[string]interface{}, i int) error {
if method, ok := event["method"].(string); ok && method == "object.updated" {
params, ok := event["params"].(map[string]interface{})
if !ok {
return errors.New("no events params")
}
objType, ok := params["type"].(string)
if !ok {
return errors.New("no type")
}
oldObj, ok := params["oldObject"].(map[string]interface{})
if !ok {
return errors.New("no oldObject")
}
newObj, ok := params["newObject"].(map[string]interface{})
if !ok {
return errors.New("no newObject")
}
oldBs, _ := json.MarshalIndent(oldObj, "", " ")
newBs, _ := json.MarshalIndent(newObj, "", " ")
diff := difflib.UnifiedDiff{
A: difflib.SplitLines(string(oldBs)),
B: difflib.SplitLines(string(newBs)),
FromFile: "old " + objType,
ToFile: "new " + objType,
Context: diffContextLines,
}
text, err := difflib.GetUnifiedDiffString(diff)
if err != nil {
return err
}
fmt.Println(text)
return nil
}
return errors.New("not diffable")
}
func printEvent(event map[string]interface{}, i int) {
if bs, err := json.MarshalIndent(event, "", " "); err != nil {
fmt.Println(err)
} else {
fmt.Printf("%s\n\n", bs)
}
}
func subscribeSessions(conn net.Conn, pr *peekingDecoder) error {
_, err := fmt.Fprintln(conn, `{"id":1, "method": "events.setObjectEventFilter", "params":["session", ["created", "updated", "deleted"]]}`)
if err != nil {
return err
}
return readResult(pr)
}
func subscribeSubscribers(conn net.Conn, pr *peekingDecoder) error {
_, err := fmt.Fprintln(conn, `{"id":1, "method": "events.setObjectEventFilter", "params":["subscriber", ["created", "updated", "deleted"]]}`)
if err != nil {
return err
}
return readResult(pr)
}
func subscribeGroups(conn net.Conn, pr *peekingDecoder) error {
_, err := fmt.Fprintln(conn, `{"id":1, "method": "events.setObjectEventFilter", "params":["group", ["created", "updated", "deleted"]]}`)
if err != nil {
return err
}
return readResult(pr)
}
func subscribeSessionGy(conn net.Conn, pr *peekingDecoder) error {
_, err := fmt.Fprintln(conn, `{"id":1, "method": "events.setObjectEventFilter", "params":["sessionGyServices", ["created", "updated", "deleted"]]}`)
if err != nil {
return err
}
return readResult(pr)
}
func readResult(pr *peekingDecoder) error {
for {
b, err := pr.NextByte()
if err != nil {
return err
}
if b == '{' {
break
}
var ignore interface{}
if err := pr.Decode(&ignore); err != nil {
return err
}
}
var v map[string]interface{}
pr.Decode(&v)
if err, ok := v["error"]; ok {
return errors.New(err.(map[string]interface{})["message"].(string))
}
return nil
}