From 7c137bfe356ef4ecf29571cc4f79052a103f39f4 Mon Sep 17 00:00:00 2001 From: Brian McGee Date: Tue, 26 Dec 2023 10:11:32 +0000 Subject: [PATCH] feat: listen for shutdown correctly Closes #5 Signed-off-by: Brian McGee --- internal/cli/format.go | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/internal/cli/format.go b/internal/cli/format.go index 2d1b890..5a0dbd8 100644 --- a/internal/cli/format.go +++ b/internal/cli/format.go @@ -3,6 +3,9 @@ package cli import ( "context" "fmt" + "os" + "os/signal" + "syscall" "time" "git.numtide.com/numtide/treefmt/internal/cache" @@ -10,7 +13,6 @@ import ( "github.com/charmbracelet/log" "github.com/juju/errors" - "github.com/ztrue/shutdown" "golang.org/x/sync/errgroup" ) @@ -33,9 +35,6 @@ func (f *Format) Run() error { ctx, cancel := context.WithCancel(context.Background()) defer cancel() - // register shutdown hook - shutdown.Add(cancel) - // read config cfg, err := format.ReadConfigFile(Cli.ConfigFile) if err != nil { @@ -186,7 +185,13 @@ func (f *Format) Run() error { return cache.ChangeSet(ctx, Cli.TreeRoot, pathsCh) }) - // shutdown.Listen(syscall.SIGINT, syscall.SIGTERM) + // listen for shutdown and call cancel if required + go func() { + exit := make(chan os.Signal, 1) + signal.Notify(exit, os.Interrupt, syscall.SIGTERM) + <-exit + cancel() + }() return eg.Wait() }