-
Notifications
You must be signed in to change notification settings - Fork 0
/
decode.go
83 lines (68 loc) · 1.71 KB
/
decode.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
package main
import (
"context"
"encoding/json"
"fmt"
"os"
v1 "google.golang.org/grpc/binarylog/grpc_binarylog_v1"
"google.golang.org/protobuf/encoding/protojson"
"mkm.pub/binlog/reader"
)
type DecodeCmd struct {
CmdCommon
CallID uint64 `optional:""`
// if true, decodes `data` into `decoded` to help with editing.
Expand bool `optional:""`
}
func (cmd *DecodeCmd) Run(cli *Context) error {
f, err := openFile(cmd.LogInputFile, cli.Follow)
if err != nil {
return err
}
defer f.Close()
ctx := context.Background()
entries, errCh := reader.Read(ctx, f)
conversations := map[uint64]conversation{}
w := os.Stdout
for e := range entries {
if cmd.CallID != 0 {
if e.CallId != cmd.CallID {
continue
}
}
conv := conversations[e.CallId]
conv.Record(e)
conversations[e.CallId] = conv
res, err := protojson.MarshalOptions{Multiline: true}.Marshal(e)
if err != nil {
return fmt.Errorf("cannot marshal dynamic proto: %w", err)
}
if cmd.Expand && e.Type == v1.GrpcLogEntry_EVENT_TYPE_CLIENT_MESSAGE {
var unstructured map[string]any
if err := json.Unmarshal(res, &unstructured); err != nil {
return err
}
if true {
delete(unstructured["message"].(map[string]any), "data")
msgType, err := conv.RequestMessageType(cli)
if err != nil {
return err
}
decoded, err := formatEntry(e, msgType)
if err != nil {
return fmt.Errorf("decoding as %q: %w", msgType, err)
}
unstructured["message"].(map[string]any)["decoded"] = json.RawMessage(decoded)
}
res, err = json.MarshalIndent(unstructured, "", " ")
if err != nil {
return err
}
}
fmt.Fprintf(w, "%s\n", res)
}
if err := <-errCh; err != nil {
return err
}
return nil
}