1
1
package image
2
2
3
3
import (
4
- "bytes"
5
- "encoding/json"
6
- "fmt"
7
- "io"
8
- "strings"
9
- "text/tabwriter"
10
4
"time"
11
5
6
+ "github.com/docker/cli/cli/command/formatter"
7
+
12
8
"github.com/docker/app/internal/packager"
13
9
"github.com/docker/app/internal/relocated"
14
10
"github.com/docker/app/internal/store"
15
11
"github.com/docker/cli/cli/command"
16
12
"github.com/docker/cli/cli/config"
17
- "github.com/docker/cli/templates"
18
13
"github.com/docker/distribution/reference"
19
14
"github.com/docker/docker/pkg/stringid"
20
- units "github.com/docker/go-units"
21
- "github.com/pkg/errors"
22
15
"github.com/spf13/cobra"
23
16
)
24
17
25
18
type imageListOption struct {
26
- quiet bool
27
- digests bool
28
- template string
19
+ quiet bool
20
+ digests bool
21
+ format string
29
22
}
30
23
31
24
func listCmd (dockerCli command.Cli ) * cobra.Command {
@@ -51,7 +44,7 @@ func listCmd(dockerCli command.Cli) *cobra.Command {
51
44
flags := cmd .Flags ()
52
45
flags .BoolVarP (& options .quiet , "quiet" , "q" , false , "Only show numeric IDs" )
53
46
flags .BoolVarP (& options .digests , "digests" , "" , false , "Show image digests" )
54
- cmd .Flags ().StringVarP (& options .template , "format" , "f" , "" , "Format the output using the given syntax or Go template" )
47
+ cmd .Flags ().StringVarP (& options .format , "format" , "f" , "table " , "Format the output using the given syntax or Go template" )
55
48
cmd .Flags ().SetAnnotation ("format" , "experimentalCLI" , []string {"true" }) //nolint:errcheck
56
49
57
50
return cmd
@@ -63,10 +56,12 @@ func runList(dockerCli command.Cli, options imageListOption, bundleStore store.B
63
56
return err
64
57
}
65
58
66
- if options .quiet {
67
- return printImageIDs (dockerCli , images )
59
+ ctx := formatter.Context {
60
+ Output : dockerCli .Out (),
61
+ Format : NewImageFormat (options .format , options .quiet , options .digests ),
68
62
}
69
- return printImages (dockerCli , images , options )
63
+
64
+ return ImageWrite (ctx , images )
70
65
}
71
66
72
67
func getImageDescriptors (bundleStore store.BundleStore ) ([]imageDesc , error ) {
@@ -86,41 +81,6 @@ func getImageDescriptors(bundleStore store.BundleStore) ([]imageDesc, error) {
86
81
return images , nil
87
82
}
88
83
89
- func printImages (dockerCli command.Cli , list []imageDesc , options imageListOption ) error {
90
- if options .template == "json" {
91
- bytes , err := json .MarshalIndent (list , "" , " " )
92
- if err != nil {
93
- return errors .Errorf ("Failed to marshall json: %s" , err )
94
- }
95
- _ , err = dockerCli .Out ().Write (bytes )
96
- return err
97
- }
98
- if options .template != "" {
99
- tmpl , err := templates .Parse (options .template )
100
- if err != nil {
101
- return errors .Errorf ("Template parsing error: %s" , err )
102
- }
103
- return tmpl .Execute (dockerCli .Out (), list )
104
- }
105
-
106
- w := tabwriter .NewWriter (dockerCli .Out (), 0 , 0 , 1 , ' ' , 0 )
107
- printHeaders (w , options .digests )
108
- for _ , desc := range list {
109
- desc .println (w , options .digests )
110
- }
111
-
112
- return w .Flush ()
113
- }
114
-
115
- func printImageIDs (dockerCli command.Cli , refs []imageDesc ) error {
116
- var buf bytes.Buffer
117
- for _ , ref := range refs {
118
- fmt .Fprintln (& buf , ref .ID )
119
- }
120
- fmt .Fprint (dockerCli .Out (), buf .String ())
121
- return nil
122
- }
123
-
124
84
func getImageID (bundle * relocated.Bundle , ref reference.Reference ) (string , error ) {
125
85
id , ok := ref .(store.ID )
126
86
if ! ok {
@@ -133,22 +93,13 @@ func getImageID(bundle *relocated.Bundle, ref reference.Reference) (string, erro
133
93
return stringid .TruncateID (id .String ()), nil
134
94
}
135
95
136
- func printHeaders (w io.Writer , digests bool ) {
137
- headers := []string {"REPOSITORY" , "TAG" }
138
- if digests {
139
- headers = append (headers , "DIGEST" )
140
- }
141
- headers = append (headers , "APP IMAGE ID" , "APP NAME" , "CREATED" )
142
- fmt .Fprintln (w , strings .Join (headers , "\t " ))
143
- }
144
-
145
96
type imageDesc struct {
146
- ID string `json:"id,omitempty"`
147
- Name string `json:"name,omitempty"`
148
- Repository string `json:"repository,omitempty"`
149
- Tag string `json:"tag,omitempty"`
150
- Digest string `json:"digest,omitempty"`
151
- Created time.Duration `json:"created,omitempty"`
97
+ ID string `json:"id,omitempty"`
98
+ Name string `json:"name,omitempty"`
99
+ Repository string `json:"repository,omitempty"`
100
+ Tag string `json:"tag,omitempty"`
101
+ Digest string `json:"digest,omitempty"`
102
+ Created time.Time `json:"created,omitempty"`
152
103
}
153
104
154
105
func getImageDesc (bundle * relocated.Bundle , ref reference.Reference ) imageDesc {
@@ -166,10 +117,10 @@ func getImageDesc(bundle *relocated.Bundle, ref reference.Reference) imageDesc {
166
117
if t , ok := ref .(reference.Digested ); ok {
167
118
digest = t .Digest ().String ()
168
119
}
169
- var created time.Duration
120
+ var created time.Time
170
121
if payload , err := packager .CustomPayload (bundle .Bundle ); err == nil {
171
122
if createdPayload , ok := payload .(packager.CustomPayloadCreated ); ok {
172
- created = time . Now (). UTC (). Sub ( createdPayload .CreatedTime () )
123
+ created = createdPayload .CreatedTime ()
173
124
}
174
125
}
175
126
return imageDesc {
@@ -181,27 +132,3 @@ func getImageDesc(bundle *relocated.Bundle, ref reference.Reference) imageDesc {
181
132
Created : created ,
182
133
}
183
134
}
184
-
185
- func (desc imageDesc ) humanDuration () string {
186
- if desc .Created > 0 {
187
- return units .HumanDuration (desc .Created ) + " ago"
188
- }
189
- return ""
190
- }
191
-
192
- func (desc imageDesc ) println (w io.Writer , digests bool ) {
193
- values := []string {}
194
- values = append (values , orNone (desc .Repository ), orNone (desc .Tag ))
195
- if digests {
196
- values = append (values , orNone (desc .Digest ))
197
- }
198
- values = append (values , desc .ID , desc .Name , desc .humanDuration ())
199
- fmt .Fprintln (w , strings .Join (values , "\t " ))
200
- }
201
-
202
- func orNone (s string ) string {
203
- if s != "" {
204
- return s
205
- }
206
- return "<none>"
207
- }
0 commit comments