forked from AliyunContainerService/pouch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
image.go
107 lines (87 loc) · 2.56 KB
/
image.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
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
95
96
97
98
99
100
101
102
103
104
105
106
107
package main
import (
"fmt"
"github.com/alibaba/pouch/pkg/utils"
"github.com/spf13/cobra"
)
// imagesDescription is used to describe image command in detail and auto generate command doc.
var imagesDescription = "List all images in Pouchd." +
"This is useful when you wish to have a look at images and Pouchd will show all local images with their NAME and SIZE." +
"All local images will be shown in a table format you can use."
type imageSize int64
func (i imageSize) String() string {
return utils.FormatSize(int64(i))
}
// ImageCommand use to implement 'images' command.
type ImageCommand struct {
baseCommand
// flags for image command
flagQuiet bool
flagDigest bool
}
// Init initialize images command.
func (i *ImageCommand) Init(c *Cli) {
i.cli = c
i.cmd = &cobra.Command{
Use: "images",
Short: "List all images",
Long: imagesDescription,
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
return i.runImages(args)
},
Example: imagesExample(),
}
i.addFlags()
}
// addFlags adds flags for specific command.
func (i *ImageCommand) addFlags() {
flagSet := i.cmd.Flags()
flagSet.BoolVarP(&i.flagQuiet, "quiet", "q", false, "Only show image numeric ID")
flagSet.BoolVar(&i.flagDigest, "digest", false, "Show images with digest")
}
// runImages is the entry of images container command.
func (i *ImageCommand) runImages(args []string) error {
apiClient := i.cli.Client()
imageList, err := apiClient.ImageList()
if err != nil {
return fmt.Errorf("failed to get image list: %v", err)
}
if i.flagQuiet {
for _, image := range imageList {
fmt.Println(image.ID)
}
return nil
}
display := i.cli.NewTableDisplay()
if i.flagDigest {
display.AddRow([]string{"IMAGE ID", "IMAGE NAME", "DIGEST", "SIZE"})
} else {
display.AddRow([]string{"IMAGE ID", "IMAGE NAME", "SIZE"})
}
for _, image := range imageList {
if i.flagDigest {
display.AddRow([]string{
image.ID,
image.Name,
image.Digest,
fmt.Sprintf("%s", imageSize(image.Size)),
})
} else {
display.AddRow([]string{
image.ID,
image.Name,
fmt.Sprintf("%s", imageSize(image.Size)),
})
}
}
display.Flush()
return nil
}
// imagesExample shows examples in images command, and is used in auto-generated cli docs.
func imagesExample() string {
return `$ pouch images ls
IMAGE ID IMAGE NAME SIZE
bbc3a0323522 docker.io/library/busybox:latest 2.64 KB
b81f317384d7 docker.io/library/nginx:latest 1.98 KB`
}