@@ -4,39 +4,137 @@ import (
44 "fmt"
55 "io"
66 "sort"
7+ "strings"
78 "text/tabwriter"
89
10+ "github.com/docker/app/render"
911 "github.com/docker/app/types"
12+ "github.com/docker/app/types/settings"
13+ composetypes "github.com/docker/cli/cli/compose/types"
1014)
1115
1216// Inspect dumps the metadata of an app
13- func Inspect (out io.Writer , app * types.App ) error {
17+ func Inspect (out io.Writer , app * types.App , argSettings map [string ]string ) error {
18+ // Render the compose file
19+ config , err := render .Render (app , argSettings )
20+ if err != nil {
21+ return err
22+ }
23+
24+ // Extract all the settings
25+ settingsKeys , allSettings , err := extractSettings (app , argSettings )
26+ if err != nil {
27+ return err
28+ }
29+
30+ // Add Meta data
31+ printMetadata (out , app )
32+
33+ // Add Service section
34+ printSection (out , len (config .Services ), func (w io.Writer ) {
35+ for _ , service := range config .Services {
36+ fmt .Fprintf (w , "%s\t %d\t %s\t %s\n " , service .Name , getReplicas (service ), getPorts (service .Ports ), service .Image )
37+ }
38+ }, "Service" , "Replicas" , "Ports" , "Image" )
39+
40+ // Add Network section
41+ printSection (out , len (config .Networks ), func (w io.Writer ) {
42+ for name := range config .Networks {
43+ fmt .Fprintln (w , name )
44+ }
45+ }, "Network" )
46+
47+ // Add Volume section
48+ printSection (out , len (config .Volumes ), func (w io.Writer ) {
49+ for name := range config .Volumes {
50+ fmt .Fprintln (w , name )
51+ }
52+ }, "Volume" )
53+
54+ // Add Secret section
55+ printSection (out , len (config .Secrets ), func (w io.Writer ) {
56+ for name := range config .Secrets {
57+ fmt .Fprintln (w , name )
58+ }
59+ }, "Secret" )
60+
61+ // Add Setting section
62+ printSection (out , len (settingsKeys ), func (w io.Writer ) {
63+ for _ , k := range settingsKeys {
64+ fmt .Fprintf (w , "%s\t %s\n " , k , allSettings [k ])
65+ }
66+ }, "Setting" , "Value" )
67+
68+ return nil
69+ }
70+
71+ func printMetadata (out io.Writer , app * types.App ) {
1472 meta := app .Metadata ()
15- // extract settings
16- settings := app .Settings ().Flatten ()
73+ fmt .Fprintln (out , meta .Name , meta .Version )
74+ if maintainers := meta .Maintainers .String (); maintainers != "" {
75+ fmt .Fprintln (out )
76+ fmt .Fprintln (out , "Maintained by:" , maintainers )
77+ }
78+ if meta .Description != "" {
79+ fmt .Fprintln (out )
80+ fmt .Fprintln (out , meta .Description )
81+ }
82+ }
83+
84+ func printSection (out io.Writer , len int , printer func (io.Writer ), headers ... string ) {
85+ if len == 0 {
86+ return
87+ }
88+ fmt .Fprintln (out )
89+ w := tabwriter .NewWriter (out , 0 , 0 , 1 , ' ' , 0 )
90+ var plural string
91+ if len > 1 {
92+ plural = "s"
93+ }
94+ headers [0 ] = fmt .Sprintf ("%s%s (%d)" , headers [0 ], plural , len )
95+ printHeaders (w , headers ... )
96+ printer (w )
97+ w .Flush ()
98+ }
99+
100+ func printHeaders (w io.Writer , headers ... string ) {
101+ fmt .Fprintln (w , strings .Join (headers , "\t " ))
102+ dashes := make ([]string , len (headers ))
103+ for i , h := range headers {
104+ dashes [i ] = strings .Repeat ("-" , len (h ))
105+ }
106+ fmt .Fprintln (w , strings .Join (dashes , "\t " ))
107+ }
108+
109+ func getReplicas (service composetypes.ServiceConfig ) int {
110+ if service .Deploy .Replicas != nil {
111+ return int (* service .Deploy .Replicas )
112+ }
113+ return 1
114+ }
115+
116+ func extractSettings (app * types.App , argSettings map [string ]string ) ([]string , map [string ]string , error ) {
117+ allSettings , err := mergeAndFlattenSettings (app , argSettings )
118+ if err != nil {
119+ return nil , nil , err
120+ }
17121 // sort the keys to get consistent output
18122 var settingsKeys []string
19- for k := range settings {
123+ for k := range allSettings {
20124 settingsKeys = append (settingsKeys , k )
21125 }
22126 sort .Slice (settingsKeys , func (i , j int ) bool { return settingsKeys [i ] < settingsKeys [j ] })
23- // build maintainers string
24- maintainers := meta .Maintainers .String ()
25- fmt .Fprintf (out , "%s %s\n " , meta .Name , meta .Version )
26- if maintainers != "" {
27- fmt .Fprintf (out , "Maintained by: %s\n " , maintainers )
28- fmt .Fprintln (out , "" )
29- }
30- if meta .Description != "" {
31- fmt .Fprintf (out , "%s\n " , meta .Description )
32- fmt .Fprintln (out , "" )
127+ return settingsKeys , allSettings , nil
128+ }
129+
130+ func mergeAndFlattenSettings (app * types.App , argSettings map [string ]string ) (map [string ]string , error ) {
131+ sArgs , err := settings .FromFlatten (argSettings )
132+ if err != nil {
133+ return nil , err
33134 }
34- w := tabwriter .NewWriter (out , 0 , 0 , 1 , ' ' , 0 )
35- fmt .Fprintln (w , "Setting\t Default" )
36- fmt .Fprintln (w , "-------\t -------" )
37- for _ , k := range settingsKeys {
38- fmt .Fprintf (w , "%s\t %s\n " , k , settings [k ])
135+ s , err := settings .Merge (app .Settings (), sArgs )
136+ if err != nil {
137+ return nil , err
39138 }
40- w .Flush ()
41- return nil
139+ return s .Flatten (), nil
42140}
0 commit comments