11package image
22
33import (
4- "bytes"
5- "encoding/json"
6- "fmt"
7- "io"
8- "strings"
9- "text/tabwriter"
104 "time"
115
6+ "github.com/docker/cli/cli/command/formatter"
7+
128 "github.com/docker/app/internal/packager"
139 "github.com/docker/app/internal/relocated"
1410 "github.com/docker/app/internal/store"
1511 "github.com/docker/cli/cli/command"
1612 "github.com/docker/cli/cli/config"
17- "github.com/docker/cli/templates"
1813 "github.com/docker/distribution/reference"
1914 "github.com/docker/docker/pkg/stringid"
20- units "github.com/docker/go-units"
21- "github.com/pkg/errors"
2215 "github.com/spf13/cobra"
2316)
2417
2518type imageListOption struct {
26- quiet bool
27- digests bool
28- template string
19+ quiet bool
20+ digests bool
21+ format string
2922}
3023
3124func listCmd (dockerCli command.Cli ) * cobra.Command {
@@ -51,7 +44,7 @@ func listCmd(dockerCli command.Cli) *cobra.Command {
5144 flags := cmd .Flags ()
5245 flags .BoolVarP (& options .quiet , "quiet" , "q" , false , "Only show numeric IDs" )
5346 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" )
5548 cmd .Flags ().SetAnnotation ("format" , "experimentalCLI" , []string {"true" }) //nolint:errcheck
5649
5750 return cmd
@@ -63,10 +56,12 @@ func runList(dockerCli command.Cli, options imageListOption, bundleStore store.B
6356 return err
6457 }
6558
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 ),
6862 }
69- return printImages (dockerCli , images , options )
63+
64+ return ImageWrite (ctx , images )
7065}
7166
7267func getImageDescriptors (bundleStore store.BundleStore ) ([]imageDesc , error ) {
@@ -86,41 +81,6 @@ func getImageDescriptors(bundleStore store.BundleStore) ([]imageDesc, error) {
8681 return images , nil
8782}
8883
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-
12484func getImageID (bundle * relocated.Bundle , ref reference.Reference ) (string , error ) {
12585 id , ok := ref .(store.ID )
12686 if ! ok {
@@ -133,22 +93,13 @@ func getImageID(bundle *relocated.Bundle, ref reference.Reference) (string, erro
13393 return stringid .TruncateID (id .String ()), nil
13494}
13595
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-
14596type 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"`
152103}
153104
154105func getImageDesc (bundle * relocated.Bundle , ref reference.Reference ) imageDesc {
@@ -166,10 +117,10 @@ func getImageDesc(bundle *relocated.Bundle, ref reference.Reference) imageDesc {
166117 if t , ok := ref .(reference.Digested ); ok {
167118 digest = t .Digest ().String ()
168119 }
169- var created time.Duration
120+ var created time.Time
170121 if payload , err := packager .CustomPayload (bundle .Bundle ); err == nil {
171122 if createdPayload , ok := payload .(packager.CustomPayloadCreated ); ok {
172- created = time . Now (). UTC (). Sub ( createdPayload .CreatedTime () )
123+ created = createdPayload .CreatedTime ()
173124 }
174125 }
175126 return imageDesc {
@@ -181,27 +132,3 @@ func getImageDesc(bundle *relocated.Bundle, ref reference.Reference) imageDesc {
181132 Created : created ,
182133 }
183134}
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