/
dump_raw.go
69 lines (53 loc) · 1.36 KB
/
dump_raw.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
package main
import (
"bytes"
"encoding/json"
"fmt"
"github.com/Sirupsen/logrus"
"github.com/justone/pmb/api"
)
type DumpRawCommand struct {
Pretty bool `short:"p" long:"pretty" description:"Pretty print message contents."`
Ignore []string `short:"i" long:"ignore" description:"Message types to ignore."`
}
var dumpRawCommand DumpRawCommand
func (x *DumpRawCommand) Execute(args []string) error {
bus := pmb.GetPMB(globalOptions.Broker)
id := pmb.GenerateRandomID("dumpRaw")
conn, err := bus.ConnectClient(id, !globalOptions.TrustKey)
if err != nil {
return err
}
return runDumpRaw(conn)
}
func init() {
parser.AddCommand("dump-raw",
"Print out each raw JSON message as it comes through. (low level)",
"",
&dumpRawCommand)
}
func runDumpRaw(conn *pmb.Connection) error {
ignoreTypes := make(map[string]bool)
for _, ign := range dumpRawCommand.Ignore {
ignoreTypes[ign] = true
}
for {
message := <-conn.In
if _, ok := ignoreTypes[message.Contents["type"].(string)]; ok {
logrus.Debugf("ignoring message of type %s", message.Contents["type"].(string))
continue
}
if dumpRawCommand.Pretty {
var out bytes.Buffer
err := json.Indent(&out, []byte(message.Raw), "", " ")
if err != nil {
fmt.Println(err)
} else {
fmt.Printf("%s\n", out.Bytes())
}
} else {
fmt.Printf("%s\n", message.Raw)
}
}
return nil
}