mirror of
https://github.com/spf13/cobra
synced 2024-11-14 09:47:16 +00:00
implemented OnKillRun
This commit is contained in:
parent
a516d4132c
commit
9b6f7e82fd
1 changed files with 21 additions and 0 deletions
21
command.go
21
command.go
|
@ -23,9 +23,11 @@ import (
|
|||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"os/signal"
|
||||
"path/filepath"
|
||||
"sort"
|
||||
"strings"
|
||||
"syscall"
|
||||
|
||||
flag "github.com/spf13/pflag"
|
||||
)
|
||||
|
@ -136,6 +138,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)
|
||||
|
||||
// groups for subcommands
|
||||
commandgroups []*Group
|
||||
|
@ -906,6 +910,23 @@ 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.SIGKILL,
|
||||
syscall.SIGTERM,
|
||||
syscall.SIGQUIT,
|
||||
)
|
||||
|
||||
go func() {
|
||||
_ = <-sigchan
|
||||
|
||||
c.OnKillRun(c, argWoFlags)
|
||||
}()
|
||||
}
|
||||
|
||||
if err := c.ValidateArgs(argWoFlags); err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue