mirror of
https://github.com/spf13/cobra
synced 2025-04-27 09:07:19 +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"
|
"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
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue