goext/cmdext/cmdrunner.go

195 lines
3.6 KiB
Go
Raw Permalink Normal View History

2022-12-23 20:08:59 +01:00
package cmdext
import (
"bufio"
2023-01-30 19:55:55 +01:00
"io"
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
}
err = cmd.Start()
if err != nil {
return CommandResult{}, err
}
2023-01-30 19:55:55 +01:00
errch := make(chan error, 3)
2022-12-23 20:08:59 +01:00
go func() { errch <- cmd.Wait() }()
2023-01-30 19:55:55 +01:00
// [1] read raw stdout
stdoutBufferReader, stdoutBufferWriter := io.Pipe()
stdout := ""
go func() {
buf := make([]byte, 128)
for true {
n, out := stdoutPipe.Read(buf)
if n > 0 {
txt := string(buf[:n])
stdout += txt
_, _ = stdoutBufferWriter.Write(buf[:n])
for _, lstr := range opt.listener {
lstr.ReadRawStdout(buf[:n])
}
}
if out == io.EOF {
break
}
if out != nil {
errch <- out
_ = cmd.Process.Kill()
break
}
}
_ = stdoutBufferWriter.Close()
}()
// [2] read raw stderr
stderrBufferReader, stderrBufferWriter := io.Pipe()
stderr := ""
go func() {
buf := make([]byte, 128)
for true {
n, err := stderrPipe.Read(buf)
if n > 0 {
txt := string(buf[:n])
stderr += txt
_, _ = stderrBufferWriter.Write(buf[:n])
for _, lstr := range opt.listener {
lstr.ReadRawStderr(buf[:n])
}
}
if err == io.EOF {
break
}
if err != nil {
errch <- err
_ = cmd.Process.Kill()
break
}
}
_ = stderrBufferWriter.Close()
}()
2022-12-23 20:08:59 +01:00
combch := make(chan string, 32)
stopCombch := make(chan bool)
2023-01-30 19:55:55 +01:00
// [3] collect stdout line-by-line
2022-12-23 20:08:59 +01:00
go func() {
2023-01-30 19:55:55 +01:00
scanner := bufio.NewScanner(stdoutBufferReader)
2022-12-23 20:08:59 +01:00
for scanner.Scan() {
txt := scanner.Text()
2023-01-30 19:55:55 +01:00
for _, lstr := range opt.listener {
lstr.ReadStdoutLine(txt)
}
2022-12-23 20:08:59 +01:00
combch <- txt
}
}()
2023-01-30 19:55:55 +01:00
// [4] collect stderr line-by-line
2022-12-23 20:08:59 +01:00
go func() {
2023-01-30 19:55:55 +01:00
scanner := bufio.NewScanner(stderrBufferReader)
2022-12-23 20:08:59 +01:00
for scanner.Scan() {
txt := scanner.Text()
2023-01-30 19:55:55 +01:00
for _, lstr := range opt.listener {
lstr.ReadStderrLine(txt)
}
2022-12-23 20:08:59 +01:00
combch <- txt
}
}()
2023-01-30 19:55:55 +01:00
defer func() { stopCombch <- true }()
// [5] combine stdcombined
2022-12-23 20:08:59 +01:00
stdcombined := ""
go func() {
for {
select {
case txt := <-combch:
2023-01-30 19:55:55 +01:00
stdcombined += txt + "\n" // this comes from bufio.Scanner and has no newlines...
2022-12-23 20:08:59 +01:00
case <-stopCombch:
return
}
}
}()
2023-01-30 19:55:55 +01:00
// [6] run
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-29 21:27:55 +01:00
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 {
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{
StdOut: stdout,
StdErr: stderr,
StdCombined: 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{
StdOut: stdout,
StdErr: stderr,
StdCombined: stdcombined,
ExitCode: 0,
CommandTimedOut: false,
2022-12-23 20:08:59 +01:00
}, nil
}
}
}