@@ -20,72 +20,87 @@ import (
2020)
2121
2222type service struct {
23- Name string
24- Image string
25- Replicas int
26- Mode string
27- Ports string
23+ Name string `json:",omitempty"`
24+ Image string `json:",omitempty"`
25+ Replicas int `json:",omitempty"`
26+ Mode string `json:",omitempty"`
27+ Ports string `json:",omitempty"`
2828}
2929
3030type attachment struct {
31- Path string
32- Size int64
31+ Path string `json:",omitempty"`
32+ Size int64 `json:",omitempty"`
3333}
3434
3535type appInfo struct {
36- Metadata metadata.AppMetadata
37- Services []service
38- Networks []string
39- Volumes []string
40- Secrets []string
36+ Metadata metadata.AppMetadata `json:",omitempty"`
37+ Services []service `json:",omitempty"`
38+ Networks []string `json:",omitempty"`
39+ Volumes []string `json:",omitempty"`
40+ Secrets []string `json:",omitempty"`
4141 parametersKeys []string
42- Parameters map [string ]string
43- Attachments []attachment
42+ Parameters map [string ]string `json:",omitempty"`
43+ Attachments []attachment `json:",omitempty"`
4444}
4545
4646// Inspect dumps the metadata of an app
4747func Inspect (out io.Writer , app * types.App , argParameters map [string ]string , imageMap map [string ]bundle.Image ) error {
48- outputFormat := os .Getenv (internal .DockerInspectFormatEnvVar )
49-
5048 // Render the compose file
5149 config , err := render .Render (app , argParameters , imageMap )
5250 if err != nil {
5351 return err
5452 }
5553
54+ // Collect all the relevant information about the application
5655 appInfo , err := getAppInfo (app , config , argParameters )
5756 if err != nil {
5857 return err
5958 }
6059
61- if outputFormat == "json" {
62- js , err := json .MarshalIndent (appInfo , "" , " " )
63- if err != nil {
64- return err
65- }
66- fmt .Fprintln (out , string (js ))
67- return nil
60+ outputFormat := os .Getenv (internal .DockerInspectFormatEnvVar )
61+ return printAppInfo (out , appInfo , outputFormat )
62+ }
63+
64+ func printAppInfo (out io.Writer , app appInfo , format string ) error {
65+ switch format {
66+ case "pretty" :
67+ return printTable (out , app )
68+ case "json" :
69+ return printJSON (out , app )
70+ default :
71+ return fmt .Errorf ("unknown format %q" , format )
72+ }
73+ }
74+
75+ func printJSON (out io.Writer , appInfo appInfo ) error {
76+ js , err := json .MarshalIndent (appInfo , "" , " " )
77+ if err != nil {
78+ return err
6879 }
80+ fmt .Fprintln (out , string (js ))
81+ return nil
82+ }
6983
84+ func printTable (out io.Writer , appInfo appInfo ) error {
7085 // Add Meta data
7186 printMetadata (out , appInfo )
7287
7388 // Add Service section
74- printSection (out , len (config .Services ), func (w io.Writer ) {
89+ printSection (out , len (appInfo .Services ), func (w io.Writer ) {
7590 for _ , service := range appInfo .Services {
7691 fmt .Fprintf (w , "%s\t %d\t %s\t %s\n " , service .Name , service .Replicas , service .Ports , service .Image )
7792 }
7893 }, "Service" , "Replicas" , "Ports" , "Image" )
7994
8095 // Add Network section
81- printSection (out , len (config .Networks ), func (w io.Writer ) {
96+ printSection (out , len (appInfo .Networks ), func (w io.Writer ) {
8297 for _ , name := range appInfo .Networks {
8398 fmt .Fprintln (w , name )
8499 }
85100 }, "Network" )
86101
87102 // Add Volume section
88- printSection (out , len (config .Volumes ), func (w io.Writer ) {
103+ printSection (out , len (appInfo .Volumes ), func (w io.Writer ) {
89104 for _ , name := range appInfo .Volumes {
90105 fmt .Fprintln (w , name )
91106 }
@@ -105,14 +120,13 @@ func Inspect(out io.Writer, app *types.App, argParameters map[string]string, ima
105120 }
106121 }, "Parameter" , "Value" )
107122
108- // // Add Attachments section
123+ // Add Attachments section
109124 printSection (out , len (appInfo .Attachments ), func (w io.Writer ) {
110125 for _ , attachment := range appInfo .Attachments {
111126 sizeString := units .HumanSize (float64 (attachment .Size ))
112127 fmt .Fprintf (w , "%s\t %s\n " , attachment .Path , sizeString )
113128 }
114129 }, "Attachment" , "Size" )
115-
116130 return nil
117131}
118132
0 commit comments