Skip to content

Commit 7c137bf

Browse files
committed
feat: listen for shutdown correctly
Closes #5 Signed-off-by: Brian McGee <[email protected]>
1 parent 9b84155 commit 7c137bf

File tree

1 file changed

+10
-5
lines changed

1 file changed

+10
-5
lines changed

internal/cli/format.go

+10-5
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,16 @@ package cli
33
import (
44
"context"
55
"fmt"
6+
"os"
7+
"os/signal"
8+
"syscall"
69
"time"
710

811
"git.numtide.com/numtide/treefmt/internal/cache"
912
"git.numtide.com/numtide/treefmt/internal/format"
1013

1114
"github.com/charmbracelet/log"
1215
"github.com/juju/errors"
13-
"github.com/ztrue/shutdown"
1416
"golang.org/x/sync/errgroup"
1517
)
1618

@@ -33,9 +35,6 @@ func (f *Format) Run() error {
3335
ctx, cancel := context.WithCancel(context.Background())
3436
defer cancel()
3537

36-
// register shutdown hook
37-
shutdown.Add(cancel)
38-
3938
// read config
4039
cfg, err := format.ReadConfigFile(Cli.ConfigFile)
4140
if err != nil {
@@ -186,7 +185,13 @@ func (f *Format) Run() error {
186185
return cache.ChangeSet(ctx, Cli.TreeRoot, pathsCh)
187186
})
188187

189-
// shutdown.Listen(syscall.SIGINT, syscall.SIGTERM)
188+
// listen for shutdown and call cancel if required
189+
go func() {
190+
exit := make(chan os.Signal, 1)
191+
signal.Notify(exit, os.Interrupt, syscall.SIGTERM)
192+
<-exit
193+
cancel()
194+
}()
190195

191196
return eg.Wait()
192197
}

0 commit comments

Comments
 (0)