Skip to content

Commit 77d45be

Browse files
committed
handle sigterm
1 parent 5d1eb81 commit 77d45be

1 file changed

Lines changed: 7 additions & 1 deletion

File tree

cmd/root.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import (
55
"fmt"
66
"log"
77
"os"
8+
"os/signal"
9+
"syscall"
810

911
"github.com/spf13/cobra"
1012
"github.com/spf13/viper"
@@ -33,7 +35,11 @@ func SetVersionInfo(version string) {
3335
// Execute adds all child commands to the root command and sets flags appropriately.
3436
// This is called by main.main(). It only needs to happen once to the rootCmd.
3537
func Execute() {
36-
if err := rootCmd.ExecuteContext(context.Background()); err != nil {
38+
// Create a context that is canceled on interrupt signals (Ctrl+C, SIGTERM)
39+
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
40+
defer stop()
41+
42+
if err := rootCmd.ExecuteContext(ctx); err != nil {
3743
fmt.Fprintln(os.Stderr, err)
3844
os.Exit(1)
3945
}

0 commit comments

Comments
 (0)