feat: listen for shutdown correctly
Closes #5 Signed-off-by: Brian McGee <brian@bmcgee.ie>
This commit is contained in:
parent
9b84155265
commit
7c137bfe35
|
@ -3,6 +3,9 @@ package cli
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"os/signal"
|
||||||
|
"syscall"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"git.numtide.com/numtide/treefmt/internal/cache"
|
"git.numtide.com/numtide/treefmt/internal/cache"
|
||||||
|
@ -10,7 +13,6 @@ import (
|
||||||
|
|
||||||
"github.com/charmbracelet/log"
|
"github.com/charmbracelet/log"
|
||||||
"github.com/juju/errors"
|
"github.com/juju/errors"
|
||||||
"github.com/ztrue/shutdown"
|
|
||||||
"golang.org/x/sync/errgroup"
|
"golang.org/x/sync/errgroup"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -33,9 +35,6 @@ func (f *Format) Run() error {
|
||||||
ctx, cancel := context.WithCancel(context.Background())
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
// register shutdown hook
|
|
||||||
shutdown.Add(cancel)
|
|
||||||
|
|
||||||
// read config
|
// read config
|
||||||
cfg, err := format.ReadConfigFile(Cli.ConfigFile)
|
cfg, err := format.ReadConfigFile(Cli.ConfigFile)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -186,7 +185,13 @@ func (f *Format) Run() error {
|
||||||
return cache.ChangeSet(ctx, Cli.TreeRoot, pathsCh)
|
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()
|
return eg.Wait()
|
||||||
}
|
}
|
||||||
|
|
Reference in New Issue
Block a user