-
Notifications
You must be signed in to change notification settings - Fork 18
/
unroll.go
49 lines (40 loc) · 1008 Bytes
/
unroll.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
package containerbackend
import (
"bufio"
"encoding/json"
"io"
"strings"
)
func unrollStream(reader io.Reader, logChan chan<- string, errChan chan<- error) {
buildProgressReader := bufio.NewReader(reader)
for {
// Docker build progress is line-based
line, _, err := buildProgressReader.ReadLine()
if err != nil {
if err == io.EOF {
break
}
errChan <- err
return
}
// Each line is a JSON object with the actual message wrapped in it
msg := &struct {
Stream string
}{}
if err := json.Unmarshal(line, &msg); err != nil {
errChan <- err
return
}
// We're only interested with messages containing the "stream" field, as these are the most helpful
if msg.Stream == "" {
continue
}
// Cut the unnecessary formatting done by the Docker daemon for some reason
progressMessage := strings.TrimSpace(msg.Stream)
// Some messages contain only "\n", so filter these out
if progressMessage == "" {
continue
}
logChan <- progressMessage
}
}