Skip to content
Merged
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
43 changes: 36 additions & 7 deletions cmd/catp/catp/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@
lastBytesUncompressed int64

noProgress bool

hasOptions bool
options Options
}

// st renders Status as a string.
Expand Down Expand Up @@ -150,7 +153,7 @@
}
}

func (r *runner) scanFile(rd io.Reader, out io.Writer) {
func (r *runner) scanFile(filename string, rd io.Reader, out io.Writer) {
s := bufio.NewScanner(rd)
s.Buffer(make([]byte, 64*1024), 10*1024*1024)

Expand All @@ -164,17 +167,29 @@
lines = 0
}

if !r.shouldWrite(s.Bytes()) {
line := s.Bytes()

if !r.shouldWrite(line) {
continue
}

if r.hasOptions {
if r.options.PrepareLine != nil {
line = r.options.PrepareLine(filename, lines, line)
}

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

View workflow job for this annotation

GitHub Actions / test (stable)

2 statement(s) on lines 176:179 are not covered by tests.

if line == nil {
continue

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

View workflow job for this annotation

GitHub Actions / test (stable)

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

atomic.AddInt64(&r.matches, 1)

if r.parallel > 1 && r.outDir == "" {
r.mu.Lock()
}

if _, err := out.Write(append(s.Bytes(), '\n')); err != nil {
if _, err := out.Write(append(line, '\n')); err != nil {
r.lastErr = err

if r.parallel > 1 && r.outDir == "" {
Expand Down Expand Up @@ -249,7 +264,7 @@
return shouldWrite
}

func (r *runner) cat(filename string) (err error) {
func (r *runner) cat(filename string) (err error) { //nolint:gocyclo
file, err := os.Open(filename) //nolint:gosec
if err != nil {
return err
Expand Down Expand Up @@ -329,8 +344,8 @@
})
}

if len(r.pass) > 0 || len(r.skip) > 0 || r.parallel > 1 {
r.scanFile(rd, out)
if len(r.pass) > 0 || len(r.skip) > 0 || r.parallel > 1 || r.hasOptions {
r.scanFile(filename, rd, out)
} else {
r.readFile(rd, out)
}
Expand Down Expand Up @@ -407,15 +422,29 @@
return nil
}

// Options allows behavior customisations.
type Options struct {
// PrepareLine is invoked for every line, if result is nil, line is skipped.
PrepareLine func(filename string, lineNr int, line []byte) []byte
}

// Main is the entry point for catp CLI tool.
func Main() error { //nolint:funlen,cyclop,gocognit,gocyclo,maintidx
func Main(options ...func(o *Options)) error { //nolint:funlen,cyclop,gocognit,gocyclo,maintidx
var (
pass stringFlags
skip stringFlags
)

r := &runner{}

if len(options) > 0 {
r.hasOptions = true

for _, opt := range options {
opt(&r.options)
}

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

View workflow job for this annotation

GitHub Actions / test (stable)

3 statement(s) on lines 440:445 are not covered by tests.
}

flag.Var(&pass, "pass", "filter matching, may contain multiple AND patterns separated by ^,\n"+
"if filter matches, line is passed to the output (unless filtered out by -skip)\n"+
"each -pass value is added with OR logic,\n"+
Expand Down
Loading