goext/cmdext/cmdrunner.go

117 lines
2.1 KiB
Go
Raw Permalink Normal View History

2022-12-23 20:08:59 +01:00
package cmdext
import (
"bufio"
"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...)
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
}
err = cmd.Start()
if err != nil {
return CommandResult{}, err
}
errch := make(chan error, 1)
go func() { errch <- cmd.Wait() }()
combch := make(chan string, 32)
stopCombch := make(chan bool)
stdout := ""
go func() {
scanner := bufio.NewScanner(stdoutPipe)
for scanner.Scan() {
txt := scanner.Text()
stdout += txt
combch <- txt
}
}()
stderr := ""
go func() {
scanner := bufio.NewScanner(stderrPipe)
for scanner.Scan() {
txt := scanner.Text()
stderr += txt
combch <- txt
}
}()
defer func() {
stopCombch <- true
}()
stdcombined := ""
go func() {
for {
select {
case txt := <-combch:
stdcombined += txt
case <-stopCombch:
return
}
}
}()
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()
return CommandResult{
StdOut: stdout,
StdErr: stderr,
StdCombined: stdcombined,
ExitCode: -1,
CommandTimedOut: true,
}, nil
2022-12-23 20:08:59 +01:00
2023-01-29 21:27:55 +01:00
case err := <-errch:
if exiterr, ok := err.(*exec.ExitError); ok {
2022-12-23 20:08:59 +01:00
return CommandResult{
StdOut: stdout,
StdErr: stderr,
StdCombined: stdcombined,
2023-01-29 21:27:55 +01:00
ExitCode: exiterr.ExitCode(),
CommandTimedOut: false,
}, nil
} else if err != nil {
return CommandResult{}, err
} else {
return CommandResult{
StdOut: stdout,
StdErr: stderr,
StdCombined: stdcombined,
ExitCode: 0,
CommandTimedOut: false,
2022-12-23 20:08:59 +01:00
}, nil
}
}
}