Skip to content

Commit

Permalink
Add context to cancel command execution (#103)
Browse files Browse the repository at this point in the history
  • Loading branch information
micnncim committed Feb 5, 2021
1 parent 881ac91 commit a621775
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion pkg/cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ import (
"context"
"errors"
"fmt"
"os"
"os/signal"
"strings"
"syscall"
"time"

"github.com/spf13/cobra"
Expand Down Expand Up @@ -114,11 +117,22 @@ func NewCmdReap(streams genericclioptions.IOStreams) *cobra.Command {
return
}

ctx, cancel := context.WithCancel(context.Background())
defer cancel()

ch := make(chan os.Signal, 1)
signal.Notify(ch, os.Interrupt, syscall.SIGTERM)
go func() {
<-ch
r.Infof("Canceling execution...\n")
cancel()
}()

f := cmdutil.NewFactory(r.configFlags)

cmdutil.CheckErr(r.Validate(args))
cmdutil.CheckErr(r.Complete(f, args, cmd))
cmdutil.CheckErr(r.Run(context.Background(), f))
cmdutil.CheckErr(r.Run(ctx, f))
},
}

Expand Down

0 comments on commit a621775

Please sign in to comment.