1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
|
package integrationtests
import (
"bufio"
"context"
"fmt"
"os"
"os/exec"
"sync"
"syscall"
"time"
)
// The exit code and the Go error of the command terminated.
type exitPromise func() (int, error)
func runCommand(ctx context.Context, stdoutFile, cmdStr string,
args ...string) (int, error) {
stdinCh, _, exit, err := startCommand(ctx, cmdStr, args...)
if err != nil {
return -1, err
}
fd, err := os.Create(stdoutFile)
if err != nil {
return -2, err
}
var wg sync.WaitGroup
wg.Add(1)
defer wg.Wait()
go func() {
defer fd.Close()
defer wg.Done()
for line := range stdinCh {
fd.WriteString(line)
fd.WriteString("\n")
}
}()
return exit()
}
func runCommandRetry(ctx context.Context, retries int, stdoutFile, cmd string,
args ...string) (exitCode int, err error) {
for i := 0; i < retries; i++ {
time.Sleep(time.Second)
if exitCode, err = runCommand(ctx, stdoutFile, cmd, args...); exitCode == 0 {
return
}
}
return
}
func startCommand(ctx context.Context, cmdStr string,
args ...string) (<-chan string, <-chan string, exitPromise, error) {
stdoutCh := make(chan string)
stderrCh := make(chan string)
if _, err := os.Stat(cmdStr); err != nil {
return stdoutCh, stderrCh, nil,
fmt.Errorf("no such executable '%s', please compile first: %v", cmdStr, err)
}
cmd := exec.CommandContext(ctx, cmdStr, args...)
cmdStdout, err := cmd.StdoutPipe()
if err != nil {
return stdoutCh, stderrCh, nil, err
}
cmdStderr, err := cmd.StderrPipe()
err = cmd.Start()
if err != nil {
return stdoutCh, stderrCh, nil, err
}
go func() {
defer close(stdoutCh)
scanner := bufio.NewScanner(cmdStdout)
scanner.Split(bufio.ScanLines)
for scanner.Scan() {
stdoutCh <- scanner.Text()
}
}()
go func() {
close(stderrCh)
scanner := bufio.NewScanner(cmdStderr)
scanner.Split(bufio.ScanLines)
for scanner.Scan() {
stderrCh <- scanner.Text()
}
}()
return stdoutCh, stderrCh, func() (int, error) {
err := cmd.Wait()
return exitCodeFromError(err), err
}, nil
}
func exitCodeFromError(err error) int {
if err != nil {
if exitError, ok := err.(*exec.ExitError); ok {
ws := exitError.Sys().(syscall.WaitStatus)
return ws.ExitStatus()
}
}
return 0
}
|