goext/cmdext/cmdrunner.go

130 lines
2.9 KiB
Go
Raw Permalink Normal View History

2022-12-23 20:08:59 +01:00
package cmdext
import (
2023-01-31 22:41:12 +01:00
"gogs.mikescher.com/BlackForestBytes/goext/mathext"
"gogs.mikescher.com/BlackForestBytes/goext/syncext"
2022-12-23 20:08:59 +01:00
"os/exec"
"time"
)
type CommandResult struct {
StdOut string
StdErr string
StdCombined string
ExitCode int
CommandTimedOut bool
}
2023-01-29 21:27:55 +01:00
func run(opt CommandRunner) (CommandResult, error) {
cmd := exec.Command(opt.program, opt.args...)
2023-01-29 22:07:28 +01:00
cmd.Env = append(cmd.Env, opt.env...)
2022-12-23 20:08:59 +01:00
stdoutPipe, err := cmd.StdoutPipe()
if err != nil {
return CommandResult{}, err
}
stderrPipe, err := cmd.StderrPipe()
if err != nil {
return CommandResult{}, err
}
2023-01-31 22:41:12 +01:00
preader := pipeReader{
stdout: stdoutPipe,
stderr: stderrPipe,
}
2022-12-23 20:08:59 +01:00
err = cmd.Start()
if err != nil {
return CommandResult{}, err
}
2023-01-31 22:41:12 +01:00
type resultObj struct {
stdout string
stderr string
stdcombined string
err error
}
2023-01-30 19:55:55 +01:00
2023-01-31 22:41:12 +01:00
outputChan := make(chan resultObj)
2023-01-30 19:55:55 +01:00
go func() {
2023-01-31 22:41:12 +01:00
// we need to first fully read the pipes and then call Wait
// see https://pkg.go.dev/os/exec#Cmd.StdoutPipe
2023-01-30 19:55:55 +01:00
2023-01-31 22:41:12 +01:00
stdout, stderr, stdcombined, err := preader.Read(opt.listener)
if err != nil {
outputChan <- resultObj{stdout, stderr, stdcombined, err}
2022-12-23 20:08:59 +01:00
}
2023-01-30 19:55:55 +01:00
2023-01-31 22:41:12 +01:00
err = cmd.Wait()
if err != nil {
outputChan <- resultObj{stdout, stderr, stdcombined, err}
2022-12-23 20:08:59 +01:00
}
2023-01-31 22:41:12 +01:00
outputChan <- resultObj{stdout, stderr, stdcombined, nil}
2022-12-23 20:08:59 +01:00
}()
2023-01-29 21:27:55 +01:00
var timeoutChan <-chan time.Time = make(chan time.Time, 1)
if opt.timeout != nil {
timeoutChan = time.After(*opt.timeout)
}
select {
2022-12-23 20:08:59 +01:00
2023-01-29 21:27:55 +01:00
case <-timeoutChan:
_ = cmd.Process.Kill()
2023-01-30 19:55:55 +01:00
for _, lstr := range opt.listener {
lstr.Timeout()
}
2023-01-31 22:41:12 +01:00
if fallback, ok := syncext.ReadChannelWithTimeout(outputChan, mathext.Min(32*time.Millisecond, *opt.timeout)); ok {
// most of the time the cmd.Process.Kill() should also ahve finished the pipereader
// and we can at least return the already collected stdout, stderr, etc
return CommandResult{
StdOut: fallback.stdout,
StdErr: fallback.stderr,
StdCombined: fallback.stdcombined,
ExitCode: -1,
CommandTimedOut: true,
}, nil
} else {
return CommandResult{
StdOut: "",
StdErr: "",
StdCombined: "",
ExitCode: -1,
CommandTimedOut: true,
}, nil
}
case outobj := <-outputChan:
if exiterr, ok := outobj.err.(*exec.ExitError); ok {
2023-01-30 19:55:55 +01:00
excode := exiterr.ExitCode()
for _, lstr := range opt.listener {
lstr.Finished(excode)
}
2022-12-23 20:08:59 +01:00
return CommandResult{
2023-01-31 22:41:12 +01:00
StdOut: outobj.stdout,
StdErr: outobj.stderr,
StdCombined: outobj.stdcombined,
2023-01-30 19:55:55 +01:00
ExitCode: excode,
2023-01-29 21:27:55 +01:00
CommandTimedOut: false,
}, nil
} else if err != nil {
return CommandResult{}, err
} else {
2023-01-30 19:55:55 +01:00
for _, lstr := range opt.listener {
lstr.Finished(0)
}
2023-01-29 21:27:55 +01:00
return CommandResult{
2023-01-31 22:41:12 +01:00
StdOut: outobj.stdout,
StdErr: outobj.stderr,
StdCombined: outobj.stdcombined,
2023-01-29 21:27:55 +01:00
ExitCode: 0,
CommandTimedOut: false,
2022-12-23 20:08:59 +01:00
}, nil
}
}
}