-
Notifications
You must be signed in to change notification settings - Fork 31
/
main.go
110 lines (87 loc) · 1.86 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
package main
import (
"errors"
"flag"
"fmt"
"io"
"os"
"google.golang.org/protobuf/proto"
"github.com/kdudkov/goatak/pkg/cot"
"github.com/kdudkov/goatak/pkg/cotproto"
)
func main() {
format := flag.String("format", "", "dump format (text|json|gpx|stats)")
uid := flag.String("uid", "", "uid to show")
typ := flag.String("type", "", "type to show")
n := flag.Int("n", 10, "")
flag.Parse()
files := flag.Args()
if len(files) == 0 {
fmt.Println("usage: takreplay <filename>")
flag.PrintDefaults()
os.Exit(1)
}
var dmp Dumper
switch *format {
case "", "text":
dmp = new(TextDumper)
case "json":
dmp = new(JsonDumper)
case "json2":
dmp = new(Json2Dumper)
case "gpx":
if *uid == "" {
fmt.Println("need uid to make gpx")
os.Exit(1)
}
dmp = &GpxDumper{name: *uid}
case "stats":
dmp = new(StatsDumper)
case "broadcast":
dmp = NewBroadcastDumper(*n)
default:
fmt.Printf("invalid format %s\n", *format)
os.Exit(1)
}
for _, file := range files {
f, err := os.Open(file)
if err != nil {
panic(err)
}
if err := readFile(f, *uid, *typ, dmp); !errors.Is(err, io.EOF) {
fmt.Println(err)
}
}
dmp.Stop()
}
func readFile(f *os.File, uid, typ string, dmp Dumper) error {
dmp.Start()
lenBuf := make([]byte, 2)
for {
if _, err := io.ReadFull(f, lenBuf); err != nil {
return err
}
n := uint32(lenBuf[0]) + uint32(lenBuf[1])*256
buf := make([]byte, n)
if _, err := io.ReadFull(f, buf); err != nil {
return err
}
m := new(cotproto.TakMessage)
if err := proto.Unmarshal(buf, m); err != nil {
return err
}
if uid != "" && m.GetCotEvent().GetUid() != uid {
continue
}
if typ != "" && !cot.MatchPattern(m.GetCotEvent().GetType(), typ) {
continue
}
msg, err := cot.CotFromProto(m, "", "")
if err != nil {
return err
}
if err = dmp.Process(msg); err != nil {
return err
}
}
}