Skip to content
This repository was archived by the owner on Jul 18, 2025. It is now read-only.

Commit 8908b28

Browse files
committed
Remove App-only fields from CNAB image inspect out
Removes the App-only SERVICE fields from the `app image inspect --pretty` output when inspecting a non-App CNAB: * REPLICAS * PORTS Added unit test for image inspect of CNABs with pretty format. Refactored parameter key sorting code to use simpler `sort.Strings()` Signed-off-by: Nick Adcock <[email protected]>
1 parent db25844 commit 8908b28

File tree

3 files changed

+58
-14
lines changed

3 files changed

+58
-14
lines changed

internal/inspect/inspect.go

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ func ImageInspect(out io.Writer, app *types.App, argParameters map[string]string
111111
}
112112

113113
outputFormat := os.Getenv(internal.DockerInspectFormatEnvVar)
114-
return printImageAppInfo(out, appInfo, outputFormat)
114+
return printImageAppInfo(out, appInfo, outputFormat, true)
115115
}
116116

117117
func ImageInspectCNAB(out io.Writer, bndl *bundle.Bundle, outputFormat string) error {
@@ -138,6 +138,7 @@ func ImageInspectCNAB(out io.Writer, bndl *bundle.Bundle, outputFormat string) e
138138
params[v.Definition] = ""
139139
}
140140
}
141+
sort.Strings(paramKeys)
141142

142143
services := []Service{}
143144
for k, v := range bndl.Images {
@@ -146,6 +147,9 @@ func ImageInspectCNAB(out io.Writer, bndl *bundle.Bundle, outputFormat string) e
146147
Image: v.Image,
147148
})
148149
}
150+
sort.SliceStable(services, func(i, j int) bool {
151+
return services[i].Name < services[j].Name
152+
})
149153

150154
appInfo := ImageAppInfo{
151155
Metadata: meta,
@@ -154,7 +158,7 @@ func ImageInspectCNAB(out io.Writer, bndl *bundle.Bundle, outputFormat string) e
154158
Services: services,
155159
}
156160

157-
return printImageAppInfo(out, appInfo, outputFormat)
161+
return printImageAppInfo(out, appInfo, outputFormat, false)
158162
}
159163

160164
func printAppInfo(out io.Writer, app AppInfo, format string) error {
@@ -168,10 +172,10 @@ func printAppInfo(out io.Writer, app AppInfo, format string) error {
168172
}
169173
}
170174

171-
func printImageAppInfo(out io.Writer, app ImageAppInfo, format string) error {
175+
func printImageAppInfo(out io.Writer, app ImageAppInfo, format string, isApp bool) error {
172176
switch format {
173177
case "pretty":
174-
return printTable(out, app)
178+
return printTable(out, app, isApp)
175179
case "json":
176180
return printJSON(out, app)
177181
default:
@@ -209,16 +213,24 @@ func printAppTable(out io.Writer, info AppInfo) error {
209213
return nil
210214
}
211215

212-
func printTable(out io.Writer, appInfo ImageAppInfo) error {
216+
func printTable(out io.Writer, appInfo ImageAppInfo, isApp bool) error {
213217
// Add Meta data
214218
printYAML(out, appInfo.Metadata)
215219

216220
// Add Service section
217-
printSection(out, len(appInfo.Services), func(w io.Writer) {
218-
for _, service := range appInfo.Services {
219-
fmt.Fprintf(w, "%s\t%d\t%s\t%s\n", service.Name, service.Replicas, service.Ports, service.Image)
220-
}
221-
}, "SERVICE", "REPLICAS", "PORTS", "IMAGE")
221+
if isApp {
222+
printSection(out, len(appInfo.Services), func(w io.Writer) {
223+
for _, service := range appInfo.Services {
224+
fmt.Fprintf(w, "%s\t%d\t%s\t%s\n", service.Name, service.Replicas, service.Ports, service.Image)
225+
}
226+
}, "SERVICE", "REPLICAS", "PORTS", "IMAGE")
227+
} else {
228+
printSection(out, len(appInfo.Services), func(w io.Writer) {
229+
for _, service := range appInfo.Services {
230+
fmt.Fprintf(w, "%s\t%s\n", service.Name, service.Image)
231+
}
232+
}, "SERVICE", "IMAGE")
233+
}
222234

223235
// Add Network section
224236
printSection(out, len(appInfo.Networks), func(w io.Writer) {
@@ -369,7 +381,7 @@ func extractParameters(app *types.App, argParameters map[string]string) ([]strin
369381
for k := range allParameters {
370382
parametersKeys = append(parametersKeys, k)
371383
}
372-
sort.Slice(parametersKeys, func(i, j int) bool { return parametersKeys[i] < parametersKeys[j] })
384+
sort.Strings(parametersKeys)
373385
return parametersKeys, allParameters, nil
374386
}
375387

internal/inspect/inspect_test.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -133,16 +133,24 @@ text: hello`),
133133
})
134134
}
135135

136-
func TestImageInspectCNAB(t *testing.T) {
136+
func TestImageInspectCNABFormatJSON(t *testing.T) {
137+
testImageInspectCNAB(t, "json")
138+
}
139+
140+
func TestImageInspectCNABFormatPretty(t *testing.T) {
141+
testImageInspectCNAB(t, "pretty")
142+
}
143+
144+
func testImageInspectCNAB(t *testing.T, format string) {
137145
s := golden.Get(t, "bundle-json.golden")
138146
var bndl bundle.Bundle
139147
err := json.Unmarshal(s, &bndl)
140148
assert.NilError(t, err)
141149

142-
expected := golden.Get(t, "inspect-bundle-json.golden")
150+
expected := golden.Get(t, fmt.Sprintf("inspect-bundle-%s.golden", format))
143151

144152
outBuffer := new(bytes.Buffer)
145-
err = ImageInspectCNAB(outBuffer, &bndl, "json")
153+
err = ImageInspectCNAB(outBuffer, &bndl, format)
146154
assert.NilError(t, err)
147155

148156
result := outBuffer.String()
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
version: 0.1.0
2+
name: packing
3+
description: hello
4+
maintainers:
5+
- name: dev1
6+
7+
- name: dev2
8+
9+
10+
11+
SERVICE IMAGE
12+
app-watcher watcher
13+
debug busybox:latest
14+
front nginx
15+
monitor busybox:latest
16+
17+
PARAMETER VALUE
18+
com.docker.app.args
19+
com.docker.app.inspect-format json
20+
com.docker.app.kubernetes-namespace
21+
com.docker.app.orchestrator
22+
com.docker.app.render-format yaml
23+
com.docker.app.share-registry-creds false
24+
watcher.cmd foo

0 commit comments

Comments
 (0)