This repository was archived by the owner on Jul 18, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 178
Expand file tree
/
Copy pathlist.go
More file actions
144 lines (128 loc) · 3.58 KB
/
list.go
File metadata and controls
144 lines (128 loc) · 3.58 KB
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
133
134
135
136
137
138
139
140
141
142
143
144
package image
import (
"sort"
"time"
"github.com/docker/app/internal/packager"
"github.com/docker/app/internal/relocated"
"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
}
bundleStore, err := appstore.BundleStore()
if err != nil {
return err
}
return runList(dockerCli, options, bundleStore)
},
}
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, bundleStore store.BundleStore) error {
images, err := getImageDescriptors(bundleStore)
if err != nil {
return err
}
ctx := formatter.Context{
Output: dockerCli.Out(),
Format: NewImageFormat(options.format, options.quiet, options.digests),
}
sortImages(images)
return Write(ctx, images)
}
func getImageDescriptors(bundleStore store.BundleStore) ([]imageDesc, error) {
references, err := bundleStore.List()
if err != nil {
return nil, err
}
images := make([]imageDesc, len(references))
for i, ref := range references {
b, err := bundleStore.Read(ref)
if err != nil {
return nil, err
}
images[i] = getImageDesc(b, ref)
}
return images, nil
}
func getImageID(bundle *relocated.Bundle, ref reference.Reference) (string, error) {
id, ok := ref.(store.ID)
if !ok {
var err error
id, err = store.FromBundle(bundle)
if err != nil {
return "", err
}
}
return stringid.TruncateID(id.String()), nil
}
func sortImages(images []imageDesc) {
sort.SliceStable(images, func(i, j int) bool {
if images[i].Created.IsZero() {
return false
}
if images[j].Created.IsZero() {
return true
}
return images[i].Created.After(images[j].Created)
})
}
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"`
Created time.Time `json:"created,omitempty"`
}
func getImageDesc(bundle *relocated.Bundle, 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()
}
digest := bundle.RepoDigest
var created time.Time
if payload, err := packager.CustomPayload(bundle.Bundle); err == nil {
if createdPayload, ok := payload.(packager.CustomPayloadCreated); ok {
created = createdPayload.CreatedTime()
}
}
return imageDesc{
ID: id,
Name: bundle.Name,
Repository: repository,
Tag: tag,
Digest: digest.String(),
Created: created,
}
}