Skip to content

Commit

Permalink
feat: listen for shutdown correctly
Browse files Browse the repository at this point in the history
Closes #5

Signed-off-by: Brian McGee <[email protected]>
  • Loading branch information
brianmcgee committed Dec 26, 2023
1 parent 9b84155 commit 7c137bf
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions internal/cli/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@ package cli
import (
"context"
"fmt"
"os"
"os/signal"
"syscall"
"time"

"git.numtide.com/numtide/treefmt/internal/cache"
"git.numtide.com/numtide/treefmt/internal/format"

"github.com/charmbracelet/log"
"github.com/juju/errors"
"github.com/ztrue/shutdown"
"golang.org/x/sync/errgroup"
)

Expand All @@ -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 {
Expand Down Expand Up @@ -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()
}

0 comments on commit 7c137bf

Please sign in to comment.