forked from AliyunContainerService/pouch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
image_progress.go
50 lines (43 loc) · 1.56 KB
/
image_progress.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
package jsonstream
import (
"fmt"
"github.com/containerd/containerd/pkg/progress"
)
const (
// PullStatusDownloading represents downloading status.
PullStatusDownloading = "downloading"
// PullStatusWaiting represents waiting status.
PullStatusWaiting = "waiting"
// PullStatusResolving represents resolving status.
PullStatusResolving = "resolving"
// PullStatusResolved represents resolved status.
PullStatusResolved = "resolved"
// PullStatusExists represents exist status.
PullStatusExists = "exists"
// PullStatusDone represents done status.
PullStatusDone = "done"
// PushStatusUploading represents uploading status.
PushStatusUploading = "uploading"
)
// ProcessStatus returns the status of download or upload image
//
// NOTE: if the stdout is not terminal, it should only show the reference and
// status without progress bar.
func ProcessStatus(short bool, msg JSONMessage) string {
if short || msg.Detail == nil {
return fmt.Sprintf("%s:\t%s\n", msg.ID, msg.Status)
}
switch msg.Status {
case PullStatusResolving, PullStatusWaiting:
return fmt.Sprintf("%s:\t%s\t%40r\t\n", msg.ID, msg.Status, progress.Bar(0.0))
case PullStatusDownloading, PushStatusUploading:
bar := progress.Bar(0)
current, total := progress.Bytes(msg.Detail.Current), progress.Bytes(msg.Detail.Total)
if msg.Detail.Total > 0 {
bar = progress.Bar(float64(current) / float64(total))
}
return fmt.Sprintf("%s:\t%s\t%40r\t%8.8s/%s\t\n", msg.ID, msg.Status, bar, current, total)
default:
return fmt.Sprintf("%s:\t%s\t%40r\t\n", msg.ID, msg.Status, progress.Bar(1.0))
}
}