feat: listen for shutdown correctly

Closes #5

Signed-off-by: Brian McGee <brian@bmcgee.ie>
This commit is contained in:
Brian McGee 2023-12-26 10:11:32 +00:00
parent 9b84155265
commit 7c137bfe35
Signed by: brianmcgee
GPG Key ID: D49016E76AD1E8C0

View File

@ -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()
}