add exit event

This commit is contained in:
Shane C 2024-07-06 15:09:17 -04:00
parent 750686fd1f
commit bde6ae681e
Signed by: shane
GPG key ID: E46B5FEA35B22FF9
3 changed files with 60 additions and 5 deletions

View file

@ -2,9 +2,16 @@ package linux
const ( const (
EventOutput = iota EventOutput = iota
EventStderr EventExit
) )
type EventOutputData struct { type EventOutputData struct {
Output string Output string
CmdOptions CommandOptions
}
type EventExitData struct {
HasSucceeded bool
ExitCode int
CmdOptions CommandOptions
} }

View file

@ -32,4 +32,5 @@ var (
ErrCommandNotFound = errors.New("error command not found") ErrCommandNotFound = errors.New("error command not found")
ErrCommandNotExecutable = errors.New("error command not executable") ErrCommandNotExecutable = errors.New("error command not executable")
ErrInvalidHandler = errors.New("invalid handler") ErrInvalidHandler = errors.New("invalid handler")
ErrRunningEvt = errors.New("error running event")
) )

View file

@ -39,6 +39,8 @@ func (cmd *LinuxCommand) AddHandler(handler interface{}) error {
case func(data EventOutputData) error: case func(data EventOutputData) error:
cmd.handlers[EventOutput] = h cmd.handlers[EventOutput] = h
break break
case func(data EventExitData) error:
cmd.handlers[EventExit] = h
default: default:
return ErrInvalidHandler return ErrInvalidHandler
} }
@ -94,6 +96,7 @@ func (cmd *LinuxCommand) Run() error {
if err := command.Start(); err != nil { if err := command.Start(); err != nil {
var exitErr *exec.ExitError var exitErr *exec.ExitError
if errors.As(err, &exitErr) { if errors.As(err, &exitErr) {
fmt.Println(exitErr.String())
if exitErr.ExitCode() == 127 { if exitErr.ExitCode() == 127 {
return ErrCommandNotFound return ErrCommandNotFound
} else if _, ok := cmd.Options.CustomErrors[int8(exitErr.ExitCode())]; ok { } 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 h, ok := cmd.handlers[EventOutput]; ok {
if err := h.(func(data EventOutputData) error)(EventOutputData{ if err := h.(func(data EventOutputData) error)(EventOutputData{
Output: line, Output: line,
CmdOptions: cmd.Options,
}); err != nil { }); err != nil {
return return
} }
@ -130,6 +134,7 @@ func (cmd *LinuxCommand) Run() error {
if h, ok := cmd.handlers[EventOutput]; ok { if h, ok := cmd.handlers[EventOutput]; ok {
if err := h.(func(data EventOutputData) error)(EventOutputData{ if err := h.(func(data EventOutputData) error)(EventOutputData{
Output: line, Output: line,
CmdOptions: cmd.Options,
}); err != nil { }); err != nil {
return return
} }
@ -165,16 +170,58 @@ func (cmd *LinuxCommand) Run() error {
return ErrCommandNotExecutable return ErrCommandNotExecutable
} }
var exitInfo *EventExitData
if _, ok := cmd.handlers[EventExit]; ok {
exitInfo = &EventExitData{
HasSucceeded: true,
CmdOptions: cmd.Options,
}
}
if err := command.Wait(); err != nil { if err := command.Wait(); err != nil {
var exitErr *exec.ExitError var exitErr *exec.ExitError
if errors.As(err, &exitErr) { if errors.As(err, &exitErr) && exitErr.String() != "signal: interrupt" {
if exitErr.ExitCode() == 127 { if exitErr.ExitCode() == 127 {
return ErrCommandNotFound return ErrCommandNotFound
} else if _, ok := cmd.Options.CustomErrors[int8(exitErr.ExitCode())]; ok { } else if _, ok := cmd.Options.CustomErrors[int8(exitErr.ExitCode())]; ok {
return cmd.Options.CustomErrors[int8(exitErr.ExitCode())] if h, ok := cmd.handlers[EventExit]; ok {
} else { if exitInfo == nil {
return fmt.Errorf("%s: %w", ErrRunningCmd.Error(), err) 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)
} }
} }