diff --git a/cmd/catp/catp/app.go b/cmd/catp/catp/app.go index 4ccd48c..615bf83 100644 --- a/cmd/catp/catp/app.go +++ b/cmd/catp/catp/app.go @@ -10,11 +10,13 @@ import ( "io" "log" "os" + "os/signal" "path/filepath" "runtime/pprof" "sort" "strings" "sync/atomic" + "syscall" "time" "github.com/bool64/dev/version" @@ -238,6 +240,16 @@ func Main(options ...func(o *Options)) error { //nolint:funlen,cyclop,gocognit,g r.sizes[fn] = st.Size() } + shutdown := make(chan os.Signal, 1) + signal.Notify(shutdown, syscall.SIGINT, syscall.SIGTERM) + + go func() { + <-shutdown + + println("received signal, shutting down...") + atomic.StoreInt64(&r.closed, 1) + }() + if r.parallel >= 2 { pr := r.pr pr.Start(func(t *progress.Task) { diff --git a/cmd/catp/catp/catp.go b/cmd/catp/catp/catp.go index 4582453..87c11a7 100644 --- a/cmd/catp/catp/catp.go +++ b/cmd/catp/catp/catp.go @@ -28,6 +28,8 @@ type runner struct { mu sync.Mutex output io.Writer + closed int64 + pr *progress.Progress progressJSON string @@ -220,6 +222,10 @@ func (r *runner) scanFile(filename string, rd io.Reader, out io.Writer) { for s.Scan() { lines++ + if atomic.LoadInt64(&r.closed) > 0 { + break + } + if r.limiter != nil { _ = r.limiter.Wait(context.Background()) //nolint:errcheck // No failure condition here. }