-
Notifications
You must be signed in to change notification settings - Fork 0
/
filter.go
53 lines (43 loc) · 814 Bytes
/
filter.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
package main
import (
"context"
"encoding/binary"
"os"
"google.golang.org/protobuf/proto"
"mkm.pub/binlog/reader"
)
type FilterCmd struct {
CmdCommon
CallID uint64 `optional:""`
}
func (cmd *FilterCmd) 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)
w := os.Stdout
for e := range entries {
b, err := proto.Marshal(e)
if err != nil {
return err
}
if cmd.CallID != 0 {
if e.CallId != cmd.CallID {
continue
}
}
if err := binary.Write(w, binary.BigEndian, uint32(len(b))); err != nil {
return err
}
if _, err := w.Write(b); err != nil {
return err
}
}
if err := <-errCh; err != nil {
return err
}
return nil
}