| 1 |
package imageprogress |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"io" |
| 6 |
) |
| 7 |
|
| 8 |
// NewPushWriter creates a writer that periodically reports |
| 9 |
// on push 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 NewPushWriter(printFn func(string)) io.WriteCloser { |
| 13 |
return newWriter(pushReporter(printFn), pushLayersChanged) |
| 14 |
} |
| 15 |
|
| 16 |
func pushReporter(printFn func(string)) func(report) { |
| 17 |
return func(r report) { |
| 18 |
var pctComplete float32 = 0.0 |
| 19 |
pctComplete += float32(r.count(statusComplete)) / float32(r.totalCount()) |
| 20 |
pctComplete += float32(r.count(statusPushing)) / float32(r.totalCount()) * r.percentProgress(statusPushing) / 100.0 |
| 21 |
pctComplete *= 100.0 |
| 22 |
|
| 23 |
printFn(fmt.Sprintf("Pushed %d/%d layers, %.0f%% complete", r.count(statusComplete), r.totalCount(), pctComplete)) |
| 24 |
} |
| 25 |
} |
| 26 |
|
| 27 |
func pushLayersChanged(older, newer report) bool { |
| 28 |
return older.count(statusComplete) != newer.count(statusComplete) |
| 29 |
} |