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 2 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
21 changes: 21 additions & 0 deletions command.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@ import (
"fmt"
"io"
"os"
"os/signal"
"path/filepath"
"sort"
"strings"
"syscall"

flag "github.com/spf13/pflag"
)
Expand Down Expand Up @@ -138,6 +140,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
Expand Down Expand Up @@ -930,6 +934,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,
decanus marked this conversation as resolved.
Show resolved Hide resolved
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() {
_ = <-sigchan

c.OnKillRun(c, argWoFlags)
decanus marked this conversation as resolved.
Show resolved Hide resolved
}()
}

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