-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.go
457 lines (422 loc) · 11.3 KB
/
app.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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
package main
import (
_ "embed"
"fmt"
"os"
"strings"
"github.com/nekrassov01/access-log-parser"
"github.com/urfave/cli/v2"
)
//go:embed completion/alpen.bash
var bashCompletion string
//go:embed completion/alpen.zsh
var zshCompletion string
//go:embed completion/alpen.ps1
var pwshCompletion string
type shell int
const (
bash shell = iota
zsh
pwsh
)
var shells = []string{
"bash",
"zsh",
"pwsh",
}
func (s shell) String() string {
if s >= 0 && int(s) < len(shells) {
return shells[s]
}
return ""
}
type format int
const (
JSON format = iota
PrettyJSON
Text
LTSV
)
var formats = []string{
"json",
"pretty-json",
"text",
"ltsv",
}
func (f format) String() string {
if f >= 0 && int(f) < len(formats) {
return formats[f]
}
return ""
}
type app struct {
cli *cli.App
destination
flag
}
type destination struct {
completion string
input string
file string
gzip string
zip string
output string
skip cli.IntSlice
metadata bool
glob string
}
type flag struct {
completion *cli.StringFlag
input *cli.StringFlag
file *cli.PathFlag
gzip *cli.PathFlag
zip *cli.PathFlag
output *cli.StringFlag
skip *cli.IntSliceFlag
metadata *cli.BoolFlag
glob *cli.StringFlag
}
func newApp() *app {
a := app{}
a.flag.completion = &cli.StringFlag{
Name: "completion",
Aliases: []string{"c"},
Usage: fmt.Sprintf("select a shell to display completion scripts: %s", pipeJoin(shells)),
Destination: &a.destination.completion,
}
a.flag.input = &cli.StringFlag{
Name: "input",
Aliases: []string{"i"},
Usage: "input from string",
Destination: &a.destination.input,
}
a.flag.file = &cli.PathFlag{
Name: "file-path",
Aliases: []string{"f"},
Usage: "input from file path",
Destination: &a.destination.file,
}
a.flag.gzip = &cli.PathFlag{
Name: "gzip-path",
Aliases: []string{"g"},
Usage: "input from gzip file path",
Destination: &a.destination.gzip,
}
a.flag.zip = &cli.PathFlag{
Name: "zip-path",
Aliases: []string{"z"},
Usage: "input from zip file path",
Destination: &a.destination.zip,
}
a.flag.output = &cli.StringFlag{
Name: "output",
Aliases: []string{"o"},
Usage: fmt.Sprintf("select output format: %s", pipeJoin(formats)),
Destination: &a.destination.output,
Value: JSON.String(),
}
a.flag.skip = &cli.IntSliceFlag{
Name: "skip",
Aliases: []string{"s"},
Usage: "skip records by index",
Destination: &a.destination.skip,
}
a.flag.metadata = &cli.BoolFlag{
Name: "metadata",
Aliases: []string{"m"},
Usage: "enable metadata output",
Destination: &a.destination.metadata,
}
a.flag.glob = &cli.StringFlag{
Name: "glob-pattern",
Aliases: []string{"G"},
Usage: "filter glob pattern: available for parsing zip only",
Destination: &a.destination.glob,
Value: "*",
}
flags := []cli.Flag{
a.flag.input,
a.flag.file,
a.flag.gzip,
a.flag.zip,
a.flag.output,
a.flag.skip,
a.flag.metadata,
a.flag.glob,
}
a.cli = &cli.App{
Name: Name,
Usage: "Access log parser/encoder CLI",
Version: Version,
Description: "A cli application for parsing various access logs",
HideHelpCommand: true,
EnableBashCompletion: true,
Action: a.doRootAction,
Flags: []cli.Flag{a.flag.completion},
Commands: []*cli.Command{
{
Name: "clf",
Description: "Parses apache common/combined log format and converts them to structured formats",
Usage: "Parses apache common/combined log format",
UsageText: fmt.Sprintf("%s clf", Name),
HideHelpCommand: true,
Flags: flags,
Before: a.doValidate,
Action: a.doCLFAction,
},
{
Name: "clfv",
Description: "Parses apache common/combined log format with vhost and converts them to structured formats",
Usage: "Parses apache common/combined log format with vhost",
UsageText: fmt.Sprintf("%s clfv", Name),
HideHelpCommand: true,
Flags: flags,
Before: a.doValidate,
Action: a.doCLFVAction,
},
{
Name: "s3",
Description: "Parses S3 access logs and converts them to structured formats",
Usage: "Parses S3 access logs",
UsageText: fmt.Sprintf("%s s3", Name),
HideHelpCommand: true,
Flags: flags,
Before: a.doValidate,
Action: a.doS3Action,
},
{
Name: "cf",
Description: "Parses CloudFront access logs and converts them to structured formats",
Usage: "Parses CloudFront access logs",
UsageText: fmt.Sprintf("%s cf", Name),
HideHelpCommand: true,
Flags: flags,
Before: a.doValidate,
Action: a.doCFAction,
},
{
Name: "alb",
Description: "Parses ALB access logs and converts them to structured formats",
Usage: "Parses ALB access logs",
UsageText: fmt.Sprintf("%s alb", Name),
HideHelpCommand: true,
Flags: flags,
Before: a.doValidate,
Action: a.doALBAction,
},
{
Name: "nlb",
Description: "Parses NLB access logs and converts them to structured formats",
Usage: "Parses NLB access logs",
UsageText: fmt.Sprintf("%s nlb", Name),
HideHelpCommand: true,
Flags: flags,
Before: a.doValidate,
Action: a.doNLBAction,
},
{
Name: "clb",
Description: "Parses CLB access logs and converts them to structured formats",
Usage: "Parses CLB access logs",
UsageText: fmt.Sprintf("%s clb", Name),
HideHelpCommand: true,
Flags: flags,
Before: a.doValidate,
Action: a.doCLBAction,
},
{
Name: "ltsv",
Description: "Parses LTSV format logs and converts them to other structured formats",
Usage: "Parses LTSV format logs",
UsageText: fmt.Sprintf("%s ltsv", Name),
HideHelpCommand: true,
Flags: flags,
Before: a.doValidate,
Action: a.doLTSVAction,
},
},
}
return &a
}
func (a *app) run() error {
return a.cli.Run(os.Args)
}
func (a *app) doCLFAction(c *cli.Context) error {
p, err := a.newParser(c, parser.NewApacheCLFRegexParser())
if err != nil {
return err
}
return a.out(c, p)
}
func (a *app) doCLFVAction(c *cli.Context) error {
p, err := a.newParser(c, parser.NewApacheCLFWithVHostRegexParser())
if err != nil {
return err
}
return a.out(c, p)
}
func (a *app) doS3Action(c *cli.Context) error {
p, err := a.newParser(c, parser.NewS3RegexParser())
if err != nil {
return err
}
return a.out(c, p)
}
func (a *app) doCFAction(c *cli.Context) error {
p, err := a.newParser(c, parser.NewCFRegexParser())
if err != nil {
return err
}
return a.out(c, p)
}
func (a *app) doALBAction(c *cli.Context) error {
p, err := a.newParser(c, parser.NewALBRegexParser())
if err != nil {
return err
}
return a.out(c, p)
}
func (a *app) doNLBAction(c *cli.Context) error {
p, err := a.newParser(c, parser.NewNLBRegexParser())
if err != nil {
return err
}
return a.out(c, p)
}
func (a *app) doCLBAction(c *cli.Context) error {
p, err := a.newParser(c, parser.NewCLBRegexParser())
if err != nil {
return err
}
return a.out(c, p)
}
func (a *app) doLTSVAction(c *cli.Context) error {
p, err := a.newParser(c, parser.NewLTSVParser())
if err != nil {
return err
}
return a.out(c, p)
}
func (a *app) newParser(c *cli.Context, p parser.Parser) (parser.Parser, error) {
switch c.String(a.flag.output.Name) {
case JSON.String():
case PrettyJSON.String():
p.SetLineHandler(parser.PrettyJSONLineHandler)
p.SetMetadataHandler(parser.PrettyJSONMetadataHandler)
case Text.String():
p.SetLineHandler(parser.KeyValuePairLineHandler)
p.SetMetadataHandler(parser.KeyValuePairMetadataHandler)
case LTSV.String():
p.SetLineHandler(parser.LTSVLineHandler)
p.SetMetadataHandler(parser.LTSVMetadataHandler)
default:
return nil, fmt.Errorf(
"cannot parse command line flags: invalid output format: allowed values: %s",
pipeJoin(formats),
)
}
return p, nil
}
func (a *app) out(c *cli.Context, p parser.Parser) error {
result, results, err := a.dispatch(c, p)
if err != nil {
return err
}
a.printResult(result, results)
return nil
}
func (a *app) dispatch(c *cli.Context, p parser.Parser) (result *parser.Result, results []*parser.Result, err error) {
switch {
case c.IsSet(a.flag.input.Name):
result, err = p.ParseString(a.destination.input, a.destination.skip.Value())
return result, nil, err
case c.IsSet(a.flag.file.Name):
result, err = p.ParseFile(a.destination.file, a.destination.skip.Value())
return result, nil, err
case c.IsSet(a.flag.gzip.Name):
result, err = p.ParseGzip(a.destination.gzip, a.destination.skip.Value())
return result, nil, err
case c.IsSet(a.flag.zip.Name):
results, err = p.ParseZipEntries(a.destination.zip, a.destination.skip.Value(), a.destination.glob)
return nil, results, err
default:
return nil, nil, fmt.Errorf(
"cannot parse command line flags: no valid input provided: %s",
pipeJoin([]string{a.flag.input.Name, a.flag.file.Name, a.flag.gzip.Name, a.flag.zip.Name}),
)
}
}
func (a *app) printResult(result *parser.Result, results []*parser.Result) {
var builder strings.Builder
w := func(r *parser.Result) {
for i, data := range r.Data {
if i > 0 {
builder.WriteRune('\n')
}
builder.WriteString(data)
}
builder.WriteRune('\n')
if a.destination.metadata {
builder.WriteString(r.Metadata)
builder.WriteRune('\n')
}
}
switch {
case result != nil && results == nil:
w(result)
case result == nil && results != nil:
for _, r := range results {
w(r)
}
default:
}
fmt.Println(builder.String())
}
func (a *app) doRootAction(c *cli.Context) error {
if c.Args().Len() == 0 && c.NumFlags() == 0 {
return fmt.Errorf("cannot parse command line flags: no flag provided")
}
if c.IsSet(a.flag.completion.Name) {
switch a.destination.completion {
case bash.String():
fmt.Println(bashCompletion)
case zsh.String():
fmt.Println(zshCompletion)
case pwsh.String():
fmt.Println(pwshCompletion)
default:
return fmt.Errorf(
"cannot parse command line flags: invalid completion shell: allowed values: %s",
pipeJoin(shells),
)
}
}
return nil
}
func (a *app) doValidate(c *cli.Context) error {
if err := checkSingle(c, a.flag.input.Name, a.flag.file.Name, a.flag.gzip.Name, a.flag.zip.Name); err != nil {
return err
}
return checkValidPair(c, a.flag.zip.Name, a.flag.glob.Name)
}
func checkSingle(c *cli.Context, flags ...string) error {
i := 0
for _, flag := range flags {
if c.IsSet(flag) {
i++
}
}
if i > 1 {
return fmt.Errorf("cannot parse command line flags: only one flag can be used: %s", pipeJoin(flags))
}
return nil
}
func checkValidPair(c *cli.Context, a, b string) error {
if !c.IsSet(a) && c.IsSet(b) {
return fmt.Errorf("cannot parse command line flags: `%s` is available for `%s` only", b, a)
}
return nil
}
func pipeJoin(s []string) string {
return strings.Join(s, "|")
}