Debian Codegraph @

imageprogress/pull.go

raw
1 package imageprogress
2
3 import (
4 "fmt"
5 "io"
6 )
7
8 // NewPullWriter creates a writer that periodically reports
9 // on pull progress of a Docker image. It only reports when the state of the
10 // different layers has changed and uses time thresholds to limit the
11 // rate of the reports.
12 func NewPullWriter(printFn func(string)) io.WriteCloser {
13 return newWriter(pullReporter(printFn), pullLayersChanged)
14 }
15
16 func pullReporter(printFn func(string)) func(report) {
17 extracting := false
18 return func(r report) {
19 if extracting {
20 return
21 }
22 if r.count(statusDownloading) == 0 &&
23 r.count(statusPending) == 0 &&
24 r.count(statusExtracting) > 0 {
25
26 printFn(fmt.Sprintf("Pulled %[1]d/%[1]d layers, 100%% complete", r.totalCount()))
27 printFn("Extracting")
28 extracting = true
29 return
30 }
31
32 completeCount := r.count(statusComplete) + r.count(statusExtracting)
33 var pctComplete float32 = 0.0
34 pctComplete += float32(completeCount) / float32(r.totalCount())
35 pctComplete += float32(r.count(statusDownloading)) / float32(r.totalCount()) * r.percentProgress(statusDownloading) / 100.0
36 pctComplete *= 100.0
37 printFn(fmt.Sprintf("Pulled %d/%d layers, %.0f%% complete", completeCount, r.totalCount(), pctComplete))
38 }
39 }
40
41 func pullLayersChanged(older, newer report) bool {
42 olderCompleteCount := older.count(statusComplete) + older.count(statusExtracting)
43 newerCompleteCount := newer.count(statusComplete) + newer.count(statusExtracting)
44 return olderCompleteCount != newerCompleteCount
45 }