Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions cmd/catp/catp/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,19 @@
r := &runner{}

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

return nil
}), "pass", "filter matching, may contain multiple AND patterns separated by ^,\n"+
"if filter matches, line is passed to the output (may be filtered out by preceding -skip)\n"+
"other -pass values are evaluated if preceding pass/skip did not match,\n"+
"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")

flag.Var(flagFunc(func(v string) error {
return r.loadCSVFilter(v, true)
}), "pass-csv", "filter matching, loads pass params from CSV file,\n"+

Check notice on line 42 in cmd/catp/catp/app.go

View workflow job for this annotation

GitHub Actions / test (stable)

1 statement(s) on lines 40:42 are not covered by tests.
"each line is treated as -pass, each column value is AND condition.")

flag.BoolFunc("pass-any", "finishes matching and gets the value even if previous -pass did not match,\n"+
"if previous -skip matched, the line would be skipped any way.", func(s string) error {
r.filters = append(r.filters, filter{pass: true})
Expand All @@ -45,7 +50,12 @@
})

flag.Var(flagFunc(func(v string) error {
r.filters = append(r.filters, filter{pass: false, ms: v, and: bytes.Split([]byte(v), []byte("^"))})
return r.loadCSVFilter(v, false)
}), "skip-csv", "filter matching, loads skip params from CSV file,\n"+

Check notice on line 54 in cmd/catp/catp/app.go

View workflow job for this annotation

GitHub Actions / test (stable)

1 statement(s) on lines 52:54 are not covered by tests.
"each line is treated as -skip, each column value is AND condition.")

flag.Var(flagFunc(func(v string) error {
r.filters = append(r.filters, filter{pass: false, and: bytes.Split([]byte(v), []byte("^"))})

return nil
}), "skip", "filter matching, may contain multiple AND patterns separated by ^,\n"+
Expand Down
27 changes: 26 additions & 1 deletion cmd/catp/catp/catp.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"bufio"
"bytes"
"context"
"encoding/csv"
"encoding/json"
"fmt"
"io"
Expand Down Expand Up @@ -69,7 +70,6 @@
filter struct {
pass bool // Skip is false.
and [][]byte
ms string
}
flagFunc func(v string) error
)
Expand Down Expand Up @@ -504,3 +504,28 @@
// You can use buf to avoid allocations for a result, and change its capacity if needed.
PrepareLine func(filename string, lineNr int, line []byte, buf *[]byte) []byte
}

func (r *runner) loadCSVFilter(fn string, pass bool) error {
f, err := os.Open(fn) //nolint:gosec
if err != nil {
return fmt.Errorf("failed to open CSV file %s: %w", fn, err)
}

Check notice on line 512 in cmd/catp/catp/catp.go

View workflow job for this annotation

GitHub Actions / test (stable)

3 statement(s) are not covered by tests.

cr := csv.NewReader(f)

rows, err := cr.ReadAll()
if err != nil {
return fmt.Errorf("failed to read CSV file %s: %w", fn, err)
}

Check notice on line 519 in cmd/catp/catp/catp.go

View workflow job for this annotation

GitHub Actions / test (stable)

4 statement(s) are not covered by tests.

for _, row := range rows {
var and [][]byte
for _, v := range row {
and = append(and, []byte(v))
}

Check notice on line 525 in cmd/catp/catp/catp.go

View workflow job for this annotation

GitHub Actions / test (stable)

6 statement(s) are not covered by tests.

r.filters = append(r.filters, filter{pass: pass, and: and})

Check notice on line 527 in cmd/catp/catp/catp.go

View workflow job for this annotation

GitHub Actions / test (stable)

1 statement(s) are not covered by tests.
}

return nil

Check notice on line 530 in cmd/catp/catp/catp.go

View workflow job for this annotation

GitHub Actions / test (stable)

1 statement(s) are not covered by tests.
}
Loading