|
1 | 1 | package inspect |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "bytes" |
4 | 5 | "fmt" |
5 | 6 | "io" |
6 | 7 | "sort" |
| 8 | + "strings" |
7 | 9 | "text/tabwriter" |
8 | 10 |
|
| 11 | + "github.com/docker/app/render" |
9 | 12 | "github.com/docker/app/types" |
| 13 | + "github.com/docker/app/types/settings" |
| 14 | + composetypes "github.com/docker/cli/cli/compose/types" |
10 | 15 | ) |
11 | 16 |
|
12 | 17 | // Inspect dumps the metadata of an app |
13 | | -func Inspect(out io.Writer, app *types.App) error { |
| 18 | +func Inspect(out io.Writer, app *types.App, argSettings map[string]string) error { |
| 19 | + // Render the compose file |
| 20 | + config, err := render.Render(app, argSettings) |
| 21 | + if err != nil { |
| 22 | + return err |
| 23 | + } |
| 24 | + |
| 25 | + // Extract all the settings |
| 26 | + settingsKeys, allSettings, err := extractSettings(app, argSettings) |
| 27 | + if err != nil { |
| 28 | + return err |
| 29 | + } |
| 30 | + |
| 31 | + var sections []*bytes.Buffer |
| 32 | + |
| 33 | + // Add Meta data |
| 34 | + addMetadata(§ions, app) |
| 35 | + |
| 36 | + // Add Service section |
| 37 | + addSection(§ions, len(config.Services), func(w io.Writer) { |
| 38 | + for _, service := range config.Services { |
| 39 | + fmt.Fprintf(w, "%s\t%d\t%s\t%s\n", service.Name, getReplicas(service), getPorts(service), service.Image) |
| 40 | + } |
| 41 | + }, "Service", "Replicas", "Ports", "Image") |
| 42 | + |
| 43 | + // Add Network section |
| 44 | + addSection(§ions, len(config.Networks), func(w io.Writer) { |
| 45 | + for name := range config.Networks { |
| 46 | + fmt.Fprintln(w, name) |
| 47 | + } |
| 48 | + }, "Network") |
| 49 | + |
| 50 | + // Add Volume section |
| 51 | + addSection(§ions, len(config.Volumes), func(w io.Writer) { |
| 52 | + for name := range config.Volumes { |
| 53 | + fmt.Fprintln(w, name) |
| 54 | + } |
| 55 | + }, "Volume") |
| 56 | + |
| 57 | + // Add Secret section |
| 58 | + addSection(§ions, len(config.Secrets), func(w io.Writer) { |
| 59 | + for name := range config.Secrets { |
| 60 | + fmt.Fprintln(w, name) |
| 61 | + } |
| 62 | + }, "Secret") |
| 63 | + |
| 64 | + // Add Setting section |
| 65 | + addSection(§ions, len(settingsKeys), func(w io.Writer) { |
| 66 | + for _, k := range settingsKeys { |
| 67 | + fmt.Fprintf(w, "%s\t%s\n", k, allSettings[k]) |
| 68 | + } |
| 69 | + }, "Setting", "Value") |
| 70 | + |
| 71 | + // Print all sections |
| 72 | + printSections(out, sections) |
| 73 | + |
| 74 | + return nil |
| 75 | +} |
| 76 | + |
| 77 | +func addMetadata(sections *[]*bytes.Buffer, app *types.App) { |
| 78 | + buf := &bytes.Buffer{} |
14 | 79 | meta := app.Metadata() |
15 | | - // extract settings |
16 | | - settings := app.Settings().Flatten() |
| 80 | + fmt.Fprintln(buf, meta.Name, meta.Version) |
| 81 | + if maintainers := meta.Maintainers.String(); maintainers != "" { |
| 82 | + fmt.Fprintln(buf) |
| 83 | + fmt.Fprintln(buf, "Maintained by:", maintainers) |
| 84 | + } |
| 85 | + if meta.Description != "" { |
| 86 | + fmt.Fprintln(buf) |
| 87 | + fmt.Fprintln(buf, meta.Description) |
| 88 | + } |
| 89 | + *sections = append(*sections, buf) |
| 90 | +} |
| 91 | + |
| 92 | +func addSection(sections *[]*bytes.Buffer, len int, printer func(io.Writer), headers ...string) { |
| 93 | + if len == 0 { |
| 94 | + return |
| 95 | + } |
| 96 | + buf := &bytes.Buffer{} |
| 97 | + w := tabwriter.NewWriter(buf, 0, 0, 1, ' ', 0) |
| 98 | + printHeaders(w, headers...) |
| 99 | + printer(w) |
| 100 | + w.Flush() |
| 101 | + *sections = append(*sections, buf) |
| 102 | +} |
| 103 | + |
| 104 | +// printSections makes sure there isn't any extra line at the end of the command output |
| 105 | +func printSections(out io.Writer, sections []*bytes.Buffer) { |
| 106 | + ss := make([]string, len(sections)) |
| 107 | + for i, section := range sections { |
| 108 | + ss[i] = section.String() |
| 109 | + } |
| 110 | + fmt.Fprint(out, strings.Join(ss, "\n")) |
| 111 | +} |
| 112 | + |
| 113 | +func printHeaders(w io.Writer, headers ...string) { |
| 114 | + fmt.Fprintln(w, strings.Join(headers, "\t")) |
| 115 | + dashes := make([]string, len(headers)) |
| 116 | + for i, h := range headers { |
| 117 | + dashes[i] = strings.Repeat("-", len(h)) |
| 118 | + } |
| 119 | + fmt.Fprintln(w, strings.Join(dashes, "\t")) |
| 120 | +} |
| 121 | + |
| 122 | +func getReplicas(service composetypes.ServiceConfig) int { |
| 123 | + if service.Deploy.Replicas != nil { |
| 124 | + return int(*service.Deploy.Replicas) |
| 125 | + } |
| 126 | + return 1 |
| 127 | +} |
| 128 | + |
| 129 | +func getPorts(service composetypes.ServiceConfig) string { |
| 130 | + var ports []string |
| 131 | + for _, port := range service.Ports { |
| 132 | + if port.Published > 0 { |
| 133 | + ports = append(ports, fmt.Sprintf("%d", port.Published)) |
| 134 | + } |
| 135 | + } |
| 136 | + return strings.Join(ports, ",") |
| 137 | +} |
| 138 | + |
| 139 | +func extractSettings(app *types.App, argSettings map[string]string) ([]string, map[string]string, error) { |
| 140 | + allSettings, err := mergeAndFlattenSettings(app, argSettings) |
| 141 | + if err != nil { |
| 142 | + return nil, nil, err |
| 143 | + } |
17 | 144 | // sort the keys to get consistent output |
18 | 145 | var settingsKeys []string |
19 | | - for k := range settings { |
| 146 | + for k := range allSettings { |
20 | 147 | settingsKeys = append(settingsKeys, k) |
21 | 148 | } |
22 | 149 | 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, "") |
| 150 | + return settingsKeys, allSettings, nil |
| 151 | +} |
| 152 | + |
| 153 | +func mergeAndFlattenSettings(app *types.App, argSettings map[string]string) (map[string]string, error) { |
| 154 | + sArgs, err := settings.FromFlatten(argSettings) |
| 155 | + if err != nil { |
| 156 | + return nil, err |
33 | 157 | } |
34 | | - w := tabwriter.NewWriter(out, 0, 0, 1, ' ', 0) |
35 | | - fmt.Fprintln(w, "Setting\tDefault") |
36 | | - fmt.Fprintln(w, "-------\t-------") |
37 | | - for _, k := range settingsKeys { |
38 | | - fmt.Fprintf(w, "%s\t%s\n", k, settings[k]) |
| 158 | + s, err := settings.Merge(app.Settings(), sArgs) |
| 159 | + if err != nil { |
| 160 | + return nil, err |
39 | 161 | } |
40 | | - w.Flush() |
41 | | - return nil |
| 162 | + return s.Flatten(), nil |
42 | 163 | } |
0 commit comments