add exit event
This commit is contained in:
parent
750686fd1f
commit
bde6ae681e
3 changed files with 60 additions and 5 deletions
|
@ -2,9 +2,16 @@ package linux
|
|||
|
||||
const (
|
||||
EventOutput = iota
|
||||
EventStderr
|
||||
EventExit
|
||||
)
|
||||
|
||||
type EventOutputData struct {
|
||||
Output string
|
||||
CmdOptions CommandOptions
|
||||
}
|
||||
|
||||
type EventExitData struct {
|
||||
HasSucceeded bool
|
||||
ExitCode int
|
||||
CmdOptions CommandOptions
|
||||
}
|
||||
|
|
|
@ -32,4 +32,5 @@ var (
|
|||
ErrCommandNotFound = errors.New("error command not found")
|
||||
ErrCommandNotExecutable = errors.New("error command not executable")
|
||||
ErrInvalidHandler = errors.New("invalid handler")
|
||||
ErrRunningEvt = errors.New("error running event")
|
||||
)
|
||||
|
|
53
linux/run.go
53
linux/run.go
|
@ -39,6 +39,8 @@ func (cmd *LinuxCommand) AddHandler(handler interface{}) error {
|
|||
case func(data EventOutputData) error:
|
||||
cmd.handlers[EventOutput] = h
|
||||
break
|
||||
case func(data EventExitData) error:
|
||||
cmd.handlers[EventExit] = h
|
||||
default:
|
||||
return ErrInvalidHandler
|
||||
}
|
||||
|
@ -94,6 +96,7 @@ func (cmd *LinuxCommand) Run() error {
|
|||
if err := command.Start(); err != nil {
|
||||
var exitErr *exec.ExitError
|
||||
if errors.As(err, &exitErr) {
|
||||
fmt.Println(exitErr.String())
|
||||
if exitErr.ExitCode() == 127 {
|
||||
return ErrCommandNotFound
|
||||
} else if _, ok := cmd.Options.CustomErrors[int8(exitErr.ExitCode())]; ok {
|
||||
|
@ -114,6 +117,7 @@ func (cmd *LinuxCommand) Run() error {
|
|||
if h, ok := cmd.handlers[EventOutput]; ok {
|
||||
if err := h.(func(data EventOutputData) error)(EventOutputData{
|
||||
Output: line,
|
||||
CmdOptions: cmd.Options,
|
||||
}); err != nil {
|
||||
return
|
||||
}
|
||||
|
@ -130,6 +134,7 @@ func (cmd *LinuxCommand) Run() error {
|
|||
if h, ok := cmd.handlers[EventOutput]; ok {
|
||||
if err := h.(func(data EventOutputData) error)(EventOutputData{
|
||||
Output: line,
|
||||
CmdOptions: cmd.Options,
|
||||
}); err != nil {
|
||||
return
|
||||
}
|
||||
|
@ -165,16 +170,58 @@ func (cmd *LinuxCommand) Run() error {
|
|||
return ErrCommandNotExecutable
|
||||
}
|
||||
|
||||
var exitInfo *EventExitData
|
||||
|
||||
if _, ok := cmd.handlers[EventExit]; ok {
|
||||
exitInfo = &EventExitData{
|
||||
HasSucceeded: true,
|
||||
CmdOptions: cmd.Options,
|
||||
}
|
||||
}
|
||||
|
||||
if err := command.Wait(); err != nil {
|
||||
var exitErr *exec.ExitError
|
||||
if errors.As(err, &exitErr) {
|
||||
if errors.As(err, &exitErr) && exitErr.String() != "signal: interrupt" {
|
||||
if exitErr.ExitCode() == 127 {
|
||||
return ErrCommandNotFound
|
||||
} else if _, ok := cmd.Options.CustomErrors[int8(exitErr.ExitCode())]; ok {
|
||||
return cmd.Options.CustomErrors[int8(exitErr.ExitCode())]
|
||||
} else {
|
||||
if h, ok := cmd.handlers[EventExit]; ok {
|
||||
if exitInfo == nil {
|
||||
return fmt.Errorf("%s: %w", ErrRunningCmd.Error(), err)
|
||||
}
|
||||
exitInfo.HasSucceeded = false
|
||||
exitInfo.ExitCode = exitErr.ExitCode()
|
||||
err := h.(func(data EventExitData) error)(*exitInfo)
|
||||
if err != nil {
|
||||
return fmt.Errorf("%s: %w", ErrRunningEvt.Error(), err)
|
||||
}
|
||||
}
|
||||
return cmd.Options.CustomErrors[int8(exitErr.ExitCode())]
|
||||
} else {
|
||||
if h, ok := cmd.handlers[EventExit]; ok {
|
||||
if exitInfo == nil {
|
||||
return fmt.Errorf("%s: %w", ErrRunningEvt.Error(), err)
|
||||
}
|
||||
exitInfo.HasSucceeded = false
|
||||
exitInfo.ExitCode = exitErr.ExitCode()
|
||||
err := h.(func(data EventExitData) error)(*exitInfo)
|
||||
if err != nil {
|
||||
return fmt.Errorf("%s: %w", ErrRunningEvt.Error(), err)
|
||||
}
|
||||
}
|
||||
return fmt.Errorf("%s: %w", ErrRunningCmd.Error(), err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if h, ok := cmd.handlers[EventExit]; ok {
|
||||
if exitInfo == nil {
|
||||
return nil
|
||||
}
|
||||
exitInfo.ExitCode = 0
|
||||
err := h.(func(data EventExitData) error)(*exitInfo)
|
||||
if err != nil {
|
||||
return fmt.Errorf("%s: %w", ErrRunningEvt.Error(), err)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue