-
Notifications
You must be signed in to change notification settings - Fork 51
/
protocol_node_writer.go
77 lines (63 loc) · 1.22 KB
/
protocol_node_writer.go
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
package main
import (
"bufio"
"bytes"
"io"
)
type protocolNodeWriter struct {
node *remoteExecutionNode
protocol *syncProtocol
stdout io.Writer
buffer *bytes.Buffer
}
func newProtocolNodeWriter(
node *remoteExecutionNode,
protocol *syncProtocol,
) *protocolNodeWriter {
return &protocolNodeWriter{
node: node,
stdout: node.stdout,
protocol: protocol,
buffer: &bytes.Buffer{},
}
}
func (writer *protocolNodeWriter) Write(data []byte) (int, error) {
written, err := writer.buffer.Write(data)
if err != nil {
return written, err
}
reader := bufio.NewReader(writer.buffer)
for {
line, err := reader.ReadString('\n')
if err != nil {
if err == io.EOF {
_, err := io.WriteString(writer.buffer, line)
if err != nil {
return 0, err
}
break
}
}
switch {
case writer.protocol.IsSyncCommand(line):
tracef(
"%s sent sync command: '%s'",
writer.node.String(),
line,
)
err := writer.protocol.SendSync(writer.node, line)
if err != nil {
return 0, err
}
default:
_, err := io.WriteString(writer.stdout, line)
if err != nil {
return 0, err
}
}
}
return written, nil
}
func (writer *protocolNodeWriter) Close() error {
return nil
}