mirror of
https://github.com/spf13/cobra
synced 2025-04-11 09:17:18 +00:00
Merge 5c58f7bf02
into ceb39aba25
This commit is contained in:
commit
8df25352af
1 changed files with 20 additions and 0 deletions
20
command.go
20
command.go
|
@ -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
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue