Skip to content

Commit 2aea9b5

Browse files
authored
Add pass and skip from CSV (#30)
1 parent 0f316b8 commit 2aea9b5

2 files changed

Lines changed: 38 additions & 3 deletions

File tree

cmd/catp/catp/app.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,19 @@ func Main(options ...func(o *Options)) error { //nolint:funlen,cyclop,gocognit,g
2929
r := &runner{}
3030

3131
flag.Var(flagFunc(func(v string) error {
32-
r.filters = append(r.filters, filter{pass: true, ms: v, and: bytes.Split([]byte(v), []byte("^"))})
32+
r.filters = append(r.filters, filter{pass: true, and: bytes.Split([]byte(v), []byte("^"))})
3333

3434
return nil
3535
}), "pass", "filter matching, may contain multiple AND patterns separated by ^,\n"+
3636
"if filter matches, line is passed to the output (may be filtered out by preceding -skip)\n"+
3737
"other -pass values are evaluated if preceding pass/skip did not match,\n"+
3838
"for example, you can use \"-pass bar^baz -pass foo -skip fo\" to only keep lines that have (bar AND baz) OR foo, but not fox")
3939

40+
flag.Var(flagFunc(func(v string) error {
41+
return r.loadCSVFilter(v, true)
42+
}), "pass-csv", "filter matching, loads pass params from CSV file,\n"+
43+
"each line is treated as -pass, each column value is AND condition.")
44+
4045
flag.BoolFunc("pass-any", "finishes matching and gets the value even if previous -pass did not match,\n"+
4146
"if previous -skip matched, the line would be skipped any way.", func(s string) error {
4247
r.filters = append(r.filters, filter{pass: true})
@@ -45,7 +50,12 @@ func Main(options ...func(o *Options)) error { //nolint:funlen,cyclop,gocognit,g
4550
})
4651

4752
flag.Var(flagFunc(func(v string) error {
48-
r.filters = append(r.filters, filter{pass: false, ms: v, and: bytes.Split([]byte(v), []byte("^"))})
53+
return r.loadCSVFilter(v, false)
54+
}), "skip-csv", "filter matching, loads skip params from CSV file,\n"+
55+
"each line is treated as -skip, each column value is AND condition.")
56+
57+
flag.Var(flagFunc(func(v string) error {
58+
r.filters = append(r.filters, filter{pass: false, and: bytes.Split([]byte(v), []byte("^"))})
4959

5060
return nil
5161
}), "skip", "filter matching, may contain multiple AND patterns separated by ^,\n"+

cmd/catp/catp/catp.go

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"bufio"
55
"bytes"
66
"context"
7+
"encoding/csv"
78
"encoding/json"
89
"fmt"
910
"io"
@@ -69,7 +70,6 @@ type (
6970
filter struct {
7071
pass bool // Skip is false.
7172
and [][]byte
72-
ms string
7373
}
7474
flagFunc func(v string) error
7575
)
@@ -504,3 +504,28 @@ type Options struct {
504504
// You can use buf to avoid allocations for a result, and change its capacity if needed.
505505
PrepareLine func(filename string, lineNr int, line []byte, buf *[]byte) []byte
506506
}
507+
508+
func (r *runner) loadCSVFilter(fn string, pass bool) error {
509+
f, err := os.Open(fn) //nolint:gosec
510+
if err != nil {
511+
return fmt.Errorf("failed to open CSV file %s: %w", fn, err)
512+
}
513+
514+
cr := csv.NewReader(f)
515+
516+
rows, err := cr.ReadAll()
517+
if err != nil {
518+
return fmt.Errorf("failed to read CSV file %s: %w", fn, err)
519+
}
520+
521+
for _, row := range rows {
522+
var and [][]byte
523+
for _, v := range row {
524+
and = append(and, []byte(v))
525+
}
526+
527+
r.filters = append(r.filters, filter{pass: pass, and: and})
528+
}
529+
530+
return nil
531+
}

0 commit comments

Comments
 (0)