-
-
Notifications
You must be signed in to change notification settings - Fork 212
/
imagewriter.go
66 lines (54 loc) · 1.19 KB
/
imagewriter.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
package docker
import (
"bytes"
"encoding/json"
"fmt"
"io"
"strings"
)
type ImageDownload struct {
Status string
ProgressDetail ProgressDetail
Id string
Progress string
}
type ProgressDetail struct {
Current int64
Total int64
}
type ImageWriter struct {
io.Writer
Parent io.Writer
}
func (w *ImageWriter) Write(data []byte) (n int, err error) {
//what happens is the writer should send us a json blob, which has the status we want
//to handle this, we will parse it, and then format it to our upper-writer
//NOTE: WE CAN GET MULTIPLE LINES IN 1 BLOCK
var buf bytes.Buffer
n, err = buf.Write(data)
if err != nil {
return
}
for err != io.EOF {
d, e := buf.ReadBytes(byte('\n'))
if e != nil && e != io.EOF {
err = e
return
}
if d == nil || len(d) == 0 {
break
}
var imageDownload ImageDownload
err = json.Unmarshal(d, &imageDownload)
if err != nil {
return
}
message := fmt.Sprintf("%s %s %s", imageDownload.Status, imageDownload.Id, strings.ReplaceAll(imageDownload.Progress, "\u003e", ""))
message = strings.TrimSpace(message)
_, err = w.Parent.Write([]byte(message + "\n"))
}
if err == io.EOF {
err = nil
}
return
}