Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OnKillRun feature #1914

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions command.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@
"fmt"
"io"
"os"
"os/signal"
"path/filepath"
"sort"
"strings"
"syscall"

flag "github.com/spf13/pflag"
)
Expand Down Expand Up @@ -138,9 +140,11 @@
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

Check failure on line 147 in command.go

View workflow job for this annotation

GitHub Actions / golangci-lint

syntax error: mixed named and unnamed parameters (typecheck)

Check failure on line 147 in command.go

View workflow job for this annotation

GitHub Actions / golangci-lint

mixed named and unnamed parameters (typecheck)

Check failure on line 147 in command.go

View workflow job for this annotation

GitHub Actions / golangci-lint

syntax error: mixed named and unnamed parameters) (typecheck)

Check failure on line 147 in command.go

View workflow job for this annotation

GitHub Actions / golangci-lint

syntax error: mixed named and unnamed parameters) (typecheck)

Check failure on line 147 in command.go

View workflow job for this annotation

GitHub Actions / ubuntu | 1.17.x

syntax error: mixed named and unnamed parameters

Check failure on line 147 in command.go

View workflow job for this annotation

GitHub Actions / macOS | 1.17.x

syntax error: mixed named and unnamed parameters

// args is actual args parsed from flags.
args []string
Expand Down Expand Up @@ -930,6 +934,22 @@
argWoFlags = a
}

if c.OnKillRun != nil {
sigchan := make(chan os.Signal)
signal.Notify(
sigchan,
syscall.SIGINT,
syscall.SIGTERM,
syscall.SIGQUIT,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we also need os.Interrupt for things to work on Windows

)

go func() {
s := <-sigchan

c.OnKillRun(c, argWoFlags, s)
}()
}

if err := c.ValidateArgs(argWoFlags); err != nil {
return err
}
Expand Down
Loading