Skip to content
This repository has been archived by the owner on Oct 14, 2024. It is now read-only.

Commit

Permalink
feat(cli): add OS signal handling (#1543)
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisgacsal authored Apr 9, 2024
1 parent 24e2724 commit 2546c37
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion cli/cmd/root/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@
package root

import (
"context"
"os"
"os/signal"
"syscall"

"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
Expand All @@ -40,7 +43,14 @@ var rootCmd = &cobra.Command{
// Execute adds all child commands to the root command and sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute() {
cobra.CheckErr(rootCmd.Execute())
// Create os.Signal aware context.Context which will trigger context cancellation
// upon receiving any of the listed signals.
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGHUP, syscall.SIGTERM)
defer func() {
cancel()
}()

cobra.CheckErr(rootCmd.ExecuteContext(ctx))
}

// nolint: gochecknoinits
Expand Down

0 comments on commit 2546c37

Please sign in to comment.