-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrace.go
More file actions
94 lines (76 loc) · 1.86 KB
/
Copy pathtrace.go
File metadata and controls
94 lines (76 loc) · 1.86 KB
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
package progress
import (
"bytes"
"fmt"
"io"
"time"
"github.com/tonistiigi/units"
)
type knownTask struct {
started time.Time
name string
cached bool
}
type traceRenderer struct {
name string
startTime time.Time
knownTasks map[uint64]*knownTask
buf *bytes.Buffer
}
func (t *traceRenderer) update(te *TaskEvent) {
if te.ID == 0 {
return
}
secs := fmt.Sprintf("%.1f", time.Since(t.startTime).Seconds())
header := fmt.Sprintf("[%5s]", secs)
if task, ok := t.knownTasks[te.ID]; !ok {
t.knownTasks[te.ID] = &knownTask{
started: te.StartTime,
name: te.Name,
cached: te.Cached,
}
fmt.Fprintf(t.buf, "%s START %q\n", header, te.Name)
} else {
task.cached = task.cached || te.Cached
if len(te.Logs) > 0 {
logs, _ := bytes.CutSuffix(te.Logs, []byte("\n"))
for _, line := range bytes.Split(logs, []byte("\n")) {
fmt.Fprintf(t.buf, "%s %s: %s\n", header, t.name, string(line))
}
}
if te.IsDone {
secsDone := fmt.Sprintf("%.1f", time.Since(task.started).Seconds())
var copied string
if te.Current != 0 {
copied = fmt.Sprintf("%.2f", units.Bytes(te.Current))
if te.Total != 0 {
copied = fmt.Sprintf("%s / %.2f", copied, units.Bytes(te.Total))
}
copied = fmt.Sprintf("(%s) ", copied)
}
var errStr string
if te.HasErr {
errStr = fmt.Sprintf(" with ERR %s", te.Err)
}
status := "DONE"
if task.cached {
status = "CACHED"
}
fmt.Fprintf(t.buf, "%s %s %q %sin %ss%s\n", header, status, task.name, copied, secsDone, errStr)
}
}
}
func (t *traceRenderer) render(w io.Writer, _ int, _ bool) {
if t.buf.Len() > 0 {
_, _ = w.Write(t.buf.Bytes())
t.buf.Reset()
}
}
func newTraceRenderer(name string) *traceRenderer {
return &traceRenderer{
name: name,
startTime: time.Now(),
knownTasks: make(map[uint64]*knownTask),
buf: bytes.NewBuffer(nil),
}
}