This repository has been archived by the owner on Jun 13, 2021. It is now read-only.
/
list.go
122 lines (108 loc) · 3 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
package image
import (
"github.com/docker/app/internal/image"
"github.com/docker/app/internal/store"
"github.com/docker/cli/cli/command"
"github.com/docker/cli/cli/command/formatter"
"github.com/docker/cli/cli/config"
"github.com/docker/distribution/reference"
"github.com/docker/docker/pkg/stringid"
"github.com/spf13/cobra"
)
type imageListOption struct {
quiet bool
digests bool
format string
}
func listCmd(dockerCli command.Cli) *cobra.Command {
options := imageListOption{}
cmd := &cobra.Command{
Short: "List App images",
Use: "ls",
Aliases: []string{"list"},
RunE: func(cmd *cobra.Command, args []string) error {
appstore, err := store.NewApplicationStore(config.Dir())
if err != nil {
return err
}
imageStore, err := appstore.ImageStore()
if err != nil {
return err
}
return runList(dockerCli, options, imageStore)
},
}
flags := cmd.Flags()
flags.BoolVarP(&options.quiet, "quiet", "q", false, "Only show numeric IDs")
flags.BoolVarP(&options.digests, "digests", "", false, "Show image digests")
cmd.Flags().StringVarP(&options.format, "format", "f", "table", "Format the output using the given syntax or Go template")
cmd.Flags().SetAnnotation("format", "experimentalCLI", []string{"true"}) //nolint:errcheck
return cmd
}
func runList(dockerCli command.Cli, options imageListOption, imageStore store.ImageStore) error {
images, err := getImageDescriptors(imageStore)
if err != nil {
return err
}
ctx := formatter.Context{
Output: dockerCli.Out(),
Format: NewImageFormat(options.format, options.quiet, options.digests),
}
return Write(ctx, images)
}
func getImageDescriptors(imageStore store.ImageStore) ([]imageDesc, error) {
references, err := imageStore.List()
if err != nil {
return nil, err
}
images := make([]imageDesc, len(references))
for i, ref := range references {
b, err := imageStore.Read(ref)
if err != nil {
return nil, err
}
images[i] = getImageDesc(b, ref)
}
return images, nil
}
func getImageID(bundle *image.AppImage, ref reference.Reference) (string, error) {
id, ok := ref.(store.ID)
if !ok {
var err error
id, err = store.FromAppImage(bundle)
if err != nil {
return "", err
}
}
return stringid.TruncateID(id.String()), nil
}
type imageDesc struct {
ID string `json:"id,omitempty"`
Name string `json:"name,omitempty"`
Repository string `json:"repository,omitempty"`
Tag string `json:"tag,omitempty"`
Digest string `json:"digest,omitempty"`
}
func getImageDesc(bundle *image.AppImage, ref reference.Reference) imageDesc {
var id string
id, _ = getImageID(bundle, ref)
var repository string
if n, ok := ref.(reference.Named); ok {
repository = reference.FamiliarName(n)
}
var tag string
if t, ok := ref.(reference.Tagged); ok {
tag = t.Tag()
}
var digest string
if t, ok := ref.(reference.Digested); ok {
digest = t.Digest().String()
}
return imageDesc{
ID: id,
Name: bundle.Name,
Repository: repository,
Tag: tag,
Digest: digest,
}
}