This commit is contained in:
Dean Eigenmann 2025-03-13 00:43:46 +01:00 committed by GitHub
commit 8df25352af
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -23,9 +23,11 @@ import (
"fmt" "fmt"
"io" "io"
"os" "os"
"os/signal"
"path/filepath" "path/filepath"
"sort" "sort"
"strings" "strings"
"syscall"
flag "github.com/spf13/pflag" flag "github.com/spf13/pflag"
) )
@ -144,6 +146,8 @@ type Command struct {
PersistentPostRun func(cmd *Command, args []string) PersistentPostRun func(cmd *Command, args []string)
// PersistentPostRunE: PersistentPostRun but returns an error. // PersistentPostRunE: PersistentPostRun but returns an error.
PersistentPostRunE func(cmd *Command, args []string) error PersistentPostRunE func(cmd *Command, args []string) error
// OnKillRun: run if a commands execution is exited
OnKillRun func(cmd *Command, args []string, os.Signal)
// groups for subcommands // groups for subcommands
commandgroups []*Group commandgroups []*Group
@ -965,6 +969,22 @@ func (c *Command) execute(a []string) (err error) {
argWoFlags = a argWoFlags = a
} }
if c.OnKillRun != nil {
sigchan := make(chan os.Signal)
signal.Notify(
sigchan,
syscall.SIGINT,
syscall.SIGTERM,
syscall.SIGQUIT,
)
go func() {
s := <-sigchan
c.OnKillRun(c, argWoFlags, s)
}()
}
if err := c.ValidateArgs(argWoFlags); err != nil { if err := c.ValidateArgs(argWoFlags); err != nil {
return err return err
} }