-
Notifications
You must be signed in to change notification settings - Fork 19
/
list.go
132 lines (121 loc) · 3.61 KB
/
list.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package image
import (
"context"
"fmt"
"os"
"strings"
"text/tabwriter"
"github.com/docker/go-units"
"github.com/rancher/kim/pkg/apis/services/images"
imagesv1 "github.com/rancher/kim/pkg/apis/services/images/v1alpha1"
"github.com/rancher/kim/pkg/client"
criv1 "k8s.io/cri-api/pkg/apis/runtime/v1alpha2"
)
type List struct {
All bool `usage:"Show all images (default hides tag-less images)" short:"a"`
Digests bool `usage:"Show digests"`
//Filter string `usage:"Filter output based on conditions provided" short:"f"`
//Format string `usage:"Pretty-print images using a Go template"`
NoTrunc bool `usage:"Don't truncate output"`
Quiet bool `usage:"Only show image IDs" short:"q"`
}
func (s *List) Do(ctx context.Context, k8s *client.Interface, names []string) error {
return client.Images(ctx, k8s, func(ctx context.Context, imagesClient imagesv1.ImagesClient) error {
req := &imagesv1.ImageListRequest{}
// TODO filtering not working as expected
if len(names) > 0 {
req.Filter = &criv1.ImageFilter{
Image: &criv1.ImageSpec{
Image: names[0],
},
}
}
res, err := imagesClient.List(ctx, req)
if err != nil {
return err
}
images.Sort(res.Images)
// output in table format by default.
display := newTableDisplay(20, 1, 3, ' ', 0)
if !s.Quiet {
if s.Digests {
display.AddRow([]string{columnImage, columnTag, columnDigest, columnImageID, columnSize})
} else {
display.AddRow([]string{columnImage, columnTag, columnImageID, columnSize})
}
}
for _, image := range res.Images {
if s.Quiet {
fmt.Printf("%s\n", image.Id)
continue
}
imageName, repoDigest := images.NormalizeRepoDigest(image.RepoDigests)
repoTagPairs := images.NormalizeRepoTagPair(image.RepoTags, imageName)
size := units.HumanSizeWithPrecision(float64(image.GetSize_()), 3)
id := image.Id
if !s.NoTrunc {
id = images.TruncateID(id, "sha256:", 13)
repoDigest = images.TruncateID(repoDigest, "sha256:", 13)
}
for _, repoTagPair := range repoTagPairs {
if !s.All && repoDigest == "<none>" {
continue
}
if s.Digests {
display.AddRow([]string{repoTagPair[0], repoTagPair[1], repoDigest, id, size})
} else {
display.AddRow([]string{repoTagPair[0], repoTagPair[1], id, size})
}
}
continue
fmt.Printf("ID: %s\n", image.Id)
for _, tag := range image.RepoTags {
fmt.Printf("RepoTags: %s\n", tag)
}
for _, digest := range image.RepoDigests {
fmt.Printf("RepoDigests: %s\n", digest)
}
if image.Size_ != 0 {
fmt.Printf("Size: %d\n", image.Size_)
}
if image.Uid != nil {
fmt.Printf("Uid: %v\n", image.Uid)
}
if image.Username != "" {
fmt.Printf("Username: %v\n", image.Username)
}
fmt.Printf("\n")
}
display.Flush()
return nil
})
}
const (
columnImage = "IMAGE"
columnImageID = "IMAGE ID"
columnSize = "SIZE"
columnTag = "TAG"
columnDigest = "DIGEST"
)
// display use to output something on screen with table format.
type display struct {
w *tabwriter.Writer
}
// newTableDisplay creates a display instance, and uses to format output with table.
func newTableDisplay(minwidth, tabwidth, padding int, padchar byte, flags uint) *display {
w := tabwriter.NewWriter(os.Stdout, minwidth, tabwidth, padding, padchar, 0)
return &display{w}
}
// AddRow add a row of data.
func (d *display) AddRow(row []string) {
fmt.Fprintln(d.w, strings.Join(row, "\t"))
}
// Flush output all rows on screen.
func (d *display) Flush() error {
return d.w.Flush()
}
// ClearScreen clear all output on screen.
func (d *display) ClearScreen() {
fmt.Fprint(os.Stdout, "\033[2J")
fmt.Fprint(os.Stdout, "\033[H")
}