Skip to content

Commit c8d80d1

Browse files
authored
Fix handling of parallel errors again, add glob expansion (#20)
1 parent 72e7a0c commit c8d80d1

File tree

2 files changed

+54
-13
lines changed

2 files changed

+54
-13
lines changed

cmd/catp/catp/app.go

Lines changed: 48 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@ import (
55
"bufio"
66
"bytes"
77
"encoding/json"
8+
"errors"
89
"flag"
910
"fmt"
1011
"io"
1112
"log"
1213
"os"
1314
"path"
15+
"path/filepath"
1416
"runtime/pprof"
1517
"strings"
1618
"sync"
@@ -551,7 +553,8 @@ func Main() error { //nolint:funlen,cyclop,gocognit,gocyclo,maintidx
551553
r.sizes = make(map[string]int64)
552554
r.progressJSON = *progressJSON
553555
r.pr = &progress.Progress{
554-
Interval: 5 * time.Second,
556+
Interval: 5 * time.Second,
557+
IncrementalSpeed: true,
555558
Print: func(status progress.Status) {
556559
s := r.st(status)
557560

@@ -563,9 +566,32 @@ func Main() error { //nolint:funlen,cyclop,gocognit,gocyclo,maintidx
563566
},
564567
}
565568

566-
for i := 0; i < flag.NArg(); i++ {
567-
fn := flag.Arg(i)
569+
var files []string
568570

571+
for _, f := range flag.Args() {
572+
glob, err := filepath.Glob(f)
573+
if err != nil {
574+
return err
575+
}
576+
577+
for _, f := range glob {
578+
alreadyThere := false
579+
580+
for _, e := range files {
581+
if e == f {
582+
alreadyThere = true
583+
584+
break
585+
}
586+
}
587+
588+
if !alreadyThere {
589+
files = append(files, f)
590+
}
591+
}
592+
}
593+
594+
for _, fn := range files {
569595
st, err := os.Stat(fn)
570596
if err != nil {
571597
return fmt.Errorf("failed to read file stats %s: %w", fn, err)
@@ -586,10 +612,11 @@ func Main() error { //nolint:funlen,cyclop,gocognit,gocyclo,maintidx
586612
})
587613

588614
sem := make(chan struct{}, r.parallel)
589-
errs := make(chan error, 1)
615+
errs := make(chan error, r.parallel)
616+
617+
for _, fn := range files {
618+
fn := fn
590619

591-
for i := 0; i < flag.NArg(); i++ {
592-
i := i
593620
select {
594621
case err := <-errs:
595622
return err
@@ -601,8 +628,8 @@ func Main() error { //nolint:funlen,cyclop,gocognit,gocyclo,maintidx
601628
<-sem
602629
}()
603630

604-
if err := r.cat(flag.Arg(i)); err != nil {
605-
errs <- err
631+
if err := r.cat(fn); err != nil {
632+
errs <- fmt.Errorf("%s: %w", fn, err)
606633
}
607634
}()
608635
}
@@ -616,13 +643,21 @@ func Main() error { //nolint:funlen,cyclop,gocognit,gocyclo,maintidx
616643

617644
close(errs)
618645

619-
if err := <-errs; err != nil {
620-
return err
646+
var errValues []error
647+
648+
for err := range errs {
649+
if err != nil {
650+
errValues = append(errValues, err)
651+
}
652+
}
653+
654+
if errValues != nil {
655+
return errors.Join(errValues...)
621656
}
622657
} else {
623-
for i := 0; i < flag.NArg(); i++ {
624-
if err := r.cat(flag.Arg(i)); err != nil {
625-
return err
658+
for _, fn := range files {
659+
if err := r.cat(fn); err != nil {
660+
return fmt.Errorf("%s: %w", fn, err)
626661
}
627662
}
628663
}

progress.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"fmt"
66
"io"
77
"runtime"
8+
"sync"
89
"sync/atomic"
910
"time"
1011
)
@@ -24,6 +25,8 @@ type Status struct {
2425

2526
// Progress reports reading performance.
2627
type Progress struct {
28+
mu sync.Mutex
29+
2730
Interval time.Duration
2831
Print func(status Status)
2932
ShowHeapStats bool
@@ -240,6 +243,9 @@ func (p *Progress) speedStatus(s *Status) {
240243
}
241244

242245
func (p *Progress) printStatus(last bool) {
246+
p.mu.Lock()
247+
defer p.mu.Unlock()
248+
243249
s := Status{}
244250
s.Task = p.task.Task
245251
s.LinesCompleted = p.Lines()

0 commit comments

Comments
 (0)