/
main.go
137 lines (119 loc) · 3.01 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
package main
import (
"bufio"
"flag"
"fmt"
"io"
"log"
"os"
"reflect"
"github.com/proio-org/go-proio"
_ "github.com/proio-org/go-proio-pb/model/eic"
_ "github.com/proio-org/go-proio-pb/model/example"
_ "github.com/proio-org/go-proio-pb/model/lcio"
_ "github.com/proio-org/go-proio-pb/model/mc"
)
var (
ignore = flag.Bool("i", false, "ignore the specified tags instead of isolating them")
event = flag.Int64("e", -1, "list specified event, numbered consecutively from the start of the stream starting with 0")
printMetadata = flag.Bool("m", false, "print metadata as string")
)
func printUsage() {
fmt.Fprintf(os.Stderr,
`Usage: proio-ls [options] <proio-input-file> [tags...]
proio-ls will list the contents of a proio stream. For each event, the tags
are listed in alphabetical order followed by all entries with that tag (this
means that entries with multiple tags will be printed multiple times).
Optionally, tags can be specified, in which case only those tags will be shown.
The -i flag can be specified to ignore the specified tags, instead of isolating
them. The -e flag can be used to isolate a specific event by its index.
options:
`,
)
flag.PrintDefaults()
}
func main() {
flag.Usage = printUsage
flag.Parse()
if flag.NArg() < 1 {
printUsage()
log.Fatal("Invalid arguments")
}
var reader *proio.Reader
var err error
filename := flag.Arg(0)
if filename == "-" {
stdin := bufio.NewReader(os.Stdin)
reader = proio.NewReader(stdin)
} else {
reader, err = proio.Open(filename)
}
if err != nil {
log.Fatal(err)
}
defer reader.Close()
singleEvent := false
startingEvent := uint64(0)
if *event >= 0 {
singleEvent = true
startingEvent = uint64(*event)
totalSkipped := uint64(0)
for {
var nSkipped uint64
if nSkipped, err = reader.Skip(startingEvent - totalSkipped); err == io.EOF {
log.Fatal(err)
}
totalSkipped += nSkipped
if totalSkipped == startingEvent {
break
}
}
}
argTags := make(map[string]bool)
for i := 1; i < flag.NArg(); i++ {
argTags[flag.Arg(i)] = true
}
nEventsRead := uint64(0)
lastMetadata := make(map[string][]byte)
rescanLoop:
for {
var event *proio.Event
for event = range reader.ScanEvents(1) {
if *ignore {
for tag := range argTags {
event.DeleteTag(tag)
}
} else if len(argTags) > 0 {
for _, tag := range event.Tags() {
if !argTags[tag] {
event.DeleteTag(tag)
}
}
}
if !reflect.DeepEqual(event.Metadata, lastMetadata) {
fmt.Println("========== META DATA ==========")
for key, bytes := range event.Metadata {
fmt.Printf("%v: ", key)
if *printMetadata {
fmt.Println(string(bytes))
} else {
fmt.Printf("%v bytes\n", len(bytes))
}
}
fmt.Println()
lastMetadata = event.Metadata
}
fmt.Println("========== EVENT", nEventsRead+startingEvent, "==========")
fmt.Print(event)
nEventsRead++
if singleEvent {
break rescanLoop
}
}
if reader.Err == io.EOF {
break
} else {
log.Print(reader.Err)
}
}
}