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
|
package client
import (
"fmt"
"io"
"gitlab.com/gitlab-org/gitaly/proto/go/gitalypb"
)
type stdoutStderrResponse interface {
GetExitStatus() *gitalypb.ExitStatus
GetStderr() []byte
GetStdout() []byte
}
func streamHandler(recv func() (stdoutStderrResponse, error), send func(chan error), stdout, stderr io.Writer) (int32, error) {
var (
exitStatus int32
err error
resp stdoutStderrResponse
)
errC := make(chan error, 1)
go send(errC)
for {
resp, err = recv()
if err != nil {
break
}
if resp.GetExitStatus() != nil {
exitStatus = resp.GetExitStatus().GetValue()
}
if len(resp.GetStderr()) > 0 {
if _, err = stderr.Write(resp.GetStderr()); err != nil {
break
}
}
if len(resp.GetStdout()) > 0 {
if _, err = stdout.Write(resp.GetStdout()); err != nil {
break
}
}
}
if err == io.EOF {
err = nil
}
if err != nil {
return exitStatus, err
}
select {
case errSend := <-errC:
if errSend != nil {
// This should not happen
errSend = fmt.Errorf("stdin send error: %v", errSend)
}
return exitStatus, errSend
default:
return exitStatus, nil
}
}
|