1
0
Fork 0
mirror of https://github.com/spf13/cobra synced 2025-04-11 09:17:18 +00:00
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"
"io"
"os"
"os/signal"
"path/filepath"
"sort"
"strings"
"syscall"
flag "github.com/spf13/pflag"
)
@ -144,6 +146,8 @@ type Command struct {
PersistentPostRun func(cmd *Command, args []string)
// PersistentPostRunE: PersistentPostRun but returns an 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
commandgroups []*Group
@ -965,6 +969,22 @@ func (c *Command) execute(a []string) (err error) {
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 {
return err
}