-
Notifications
You must be signed in to change notification settings - Fork 0
/
patman.go
216 lines (183 loc) · 4.17 KB
/
patman.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
package patman
import (
"bufio"
"flag"
"fmt"
"log"
"os"
"regexp"
"strings"
"golang.org/x/exp/slices"
)
var input string
var index string
var format string
var mem int
var help bool
var pipelines [][]Command
var pipelineNames []string
var delimiter string
var joinDelimiter string
var stdoutBufferSize int
func init() {
flag.StringVar(&input, "file", "", "input file")
flag.StringVar(&index, "index", "", "index property used to aggregate logs")
flag.StringVar(&format, "format", "stdout", "format to be used for output, pipelines are printed in order")
flag.BoolVar(&help, "help", false, "shows help message")
flag.BoolVar(&help, "h", false, "shows help message")
flag.IntVar(&mem, "mem", 10, "Buffer size in MB")
flag.StringVar(&delimiter, "delimiter", "", "split input into a sequence of lines using a custom delimiter")
flag.StringVar(&joinDelimiter, "join", "", "join output using a custom delimiter. Writes to stdout")
flag.IntVar(&stdoutBufferSize, "buffer", 0, "flush stdout in batches to increase performance")
}
func Run() {
flag.Parse()
if help {
flag.Usage()
usage()
os.Exit(0)
}
for _, raw := range os.Args[1:] {
var ops []string
for key := range operators {
ops = append(ops, key)
}
if !regexp.MustCompile("(" + strings.Join(ops, "|") + ")\\(").MatchString(raw) {
continue
}
parser := NewParser(raw)
cmds, err := parser.Parse()
if err != nil {
log.Fatal(err)
}
for _, cmd := range cmds {
if cmd.Name == "name" {
pipelineNames = append(pipelineNames, cmd.Arg)
}
}
pipelines = append(pipelines, cmds)
}
if index != "" {
indexPipeline := false
for _, name := range pipelineNames {
if name == index {
indexPipeline = true
}
}
if !indexPipeline {
fmt.Printf("index `%s` must have a matching named pipeline\n", index)
os.Exit(1)
}
}
scanner := bufio.NewScanner(os.Stdin)
f, openFileErr := os.Open(input)
if openFileErr == nil {
scanner = bufio.NewScanner(f)
}
var print printer
for name, p := range printers {
if name == format {
print = p
}
}
// using custom formats in all other cases
if print == nil && format != "" {
print = handleCustomFormatPrint
}
if joinDelimiter != "" {
print = handleJoinPrint
}
if stdoutBufferSize > 0 {
print = handleBufferedStdoutPrint
defer flushBufferedStdout()
}
usedMem := mem * 1024 * 1024
buf := make([]byte, 0, usedMem)
scanner.Buffer(buf, usedMem)
if delimiter != "" {
scanner.Split(ScanDelimiter(delimiter))
} else {
scanner.Split(bufio.ScanLines)
}
for scanner.Scan() {
var results [][]string // match, name
for _, pipeline := range pipelines {
match, name := handle(scanner.Text(), pipeline)
if match != "" {
results = append(results, []string{match, name})
}
}
if len(pipelineNames) > 0 {
// TODO: Double check if sorting is broken after generics
slices.SortFunc(results, func(a, b []string) int {
// Unnamed pipelines should be pushed last
aIndex := -1
bIndex := -1
for i, name := range pipelineNames {
if name == a[1] {
aIndex = i
}
if name == b[1] {
bIndex = i
}
}
if aIndex < 0 {
return 1
}
if bIndex < 0 {
return -1
}
return aIndex - bIndex
})
}
// do not perform in memory buffering if no index is provided
if index == "" {
print(results)
continue
}
buffered := buffer(results)
if buffered != nil {
print(buffered)
}
}
if err := scanner.Err(); err != nil {
panic(err)
}
if openFileErr == nil {
f.Close()
}
}
func handle(line string, cmds []Command) (string, string) {
match := ""
name := ""
cmd := cmds[0]
if cmd.Name == "name" {
name = cmd.Arg
match = line
} else {
match = operators[cmd.Name].Operator(line, cmd.Arg)
}
if len(cmds) > 1 {
return handle(match, cmds[1:])
}
return match, name
}
func usage() {
fmt.Println("Available commands:")
for name, entry := range operators {
if entry.Usage == "" {
continue
}
cmd := name
if entry.Alias != "" {
cmd = name + ", " + entry.Alias
}
fmt.Println(" ", cmd)
if entry.Usage != "" {
fmt.Println(" ", entry.Usage)
}
if entry.Example != "" {
fmt.Println(" e.g.", entry.Example)
}
}
}