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

Commit bd3038c

Browse files
committed
Check parameter presence
Older bundles don't have the inspect format parameter, we don't set it for them, we set it only on bundles that define that parameter. Signed-off-by: Djordje Lukic <djordje.lukic@docker.com>
1 parent 2752fe7 commit bd3038c

11 files changed

Lines changed: 72 additions & 79 deletions

e2e/commands_test.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,9 +170,7 @@ func TestInspectApp(t *testing.T) {
170170
cmd.Command = dockerCli.Command("app", "inspect", "simple-app:1.0.0")
171171
cmd.Dir = dir.Path()
172172
output := icmd.RunCmd(cmd).Assert(t, icmd.Success).Combined()
173-
fmt.Println(output)
174173
golden.Assert(t, output, "app-inspect.golden")
175-
176174
}
177175

178176
func TestBundle(t *testing.T) {

e2e/testdata/app-inspect.golden

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,22 +18,17 @@
1818
{
1919
"Name": "api",
2020
"Image": "python:3.6",
21-
"Replicas": 1,
22-
"Mode": "",
23-
"Ports": ""
21+
"Replicas": 1
2422
},
2523
{
2624
"Name": "db",
2725
"Image": "postgres:9.3",
28-
"Replicas": 1,
29-
"Mode": "",
30-
"Ports": ""
26+
"Replicas": 1
3127
},
3228
{
3329
"Name": "web",
3430
"Image": "nginx:latest",
3531
"Replicas": 1,
36-
"Mode": "",
3732
"Ports": "8082"
3833
}
3934
],
@@ -44,11 +39,9 @@
4439
"Volumes": [
4540
"static"
4641
],
47-
"Secrets": [],
4842
"Parameters": {
4943
"api_host": "example.com",
5044
"static_subdir": "data/static",
5145
"web_port": "8082"
52-
},
53-
"Attachments": []
46+
}
5447
}

internal/commands/inspect.go

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,11 @@ type inspectOptions struct {
1919
func inspectCmd(dockerCli command.Cli) *cobra.Command {
2020
var opts inspectOptions
2121
cmd := &cobra.Command{
22-
Use: "inspect [APP_NAME] [OPTIONS]",
23-
Short: "Shows metadata, parameters and a summary of the Compose file for a given application",
24-
Example: `$ docker app inspect my-app`,
25-
Args: cli.RequiresMaxArgs(1),
22+
Use: "inspect [APP_NAME] [OPTIONS]",
23+
Short: "Shows metadata, parameters and a summary of the Compose file for a given application",
24+
Example: `$ docker app inspect my-installed-app
25+
$docker app inspect my-app:1.0.0`,
26+
Args: cli.RequiresMaxArgs(1),
2627
RunE: func(cmd *cobra.Command, args []string) error {
2728
return runInspect(dockerCli, firstOrEmpty(args), opts)
2829
},
@@ -42,15 +43,16 @@ func runInspect(dockerCli command.Cli, appname string, opts inspectOptions) erro
4243
if err != nil {
4344
return err
4445
}
45-
bundle, ref, err := getLocalBundle(dockerCli, bundleStore, appname, false)
46+
bndl, ref, err := getLocalBundle(dockerCli, bundleStore, appname, false)
47+
4648
if err != nil {
4749
return err
4850
}
4951
installation, err := appstore.NewInstallation("custom-action", ref.String())
5052
if err != nil {
5153
return err
5254
}
53-
installation.Bundle = bundle
55+
installation.Bundle = bndl
5456

5557
driverImpl, errBuf := prepareDriver(dockerCli, bindMount{}, nil)
5658
a := &action.RunCustom{
@@ -62,7 +64,9 @@ func runInspect(dockerCli command.Cli, appname string, opts inspectOptions) erro
6264
if opts.pretty {
6365
format = "pretty"
6466
}
65-
installation.Parameters[internal.ParameterInspectFormatName] = format
67+
68+
installation.SetParameter(internal.ParameterInspectFormatName, format)
69+
6670
if err := a.Run(&installation.Claim, nil, nil); err != nil {
6771
return fmt.Errorf("inspect failed: %s\n%s", err, errBuf)
6872
}

internal/inspect/inspect.go

Lines changed: 42 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -20,72 +20,87 @@ import (
2020
)
2121

2222
type 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

3030
type attachment struct {
31-
Path string
32-
Size int64
31+
Path string `json:",omitempty"`
32+
Size int64 `json:",omitempty"`
3333
}
3434

3535
type 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
4747
func 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

internal/inspect/testdata/inspect-full-json.golden

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,12 @@
1515
"Name": "web1",
1616
"Image": "nginx:latest",
1717
"Replicas": 2,
18-
"Mode": "",
1918
"Ports": "8080-8100"
2019
},
2120
{
2221
"Name": "web2",
2322
"Image": "nginx:latest",
2423
"Replicas": 2,
25-
"Mode": "",
2624
"Ports": "9080-9100"
2725
}
2826
],

internal/inspect/testdata/inspect-no-description-json.golden

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,5 @@
88
"email": "dev@example.com"
99
}
1010
]
11-
},
12-
"Services": [],
13-
"Networks": [],
14-
"Volumes": [],
15-
"Secrets": [],
16-
"Parameters": {},
17-
"Attachments": []
11+
}
1812
}

internal/inspect/testdata/inspect-no-maintainers-json.golden

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,5 @@
22
"Metadata": {
33
"version": "0.1.0",
44
"name": "myapp"
5-
},
6-
"Services": [],
7-
"Networks": [],
8-
"Volumes": [],
9-
"Secrets": [],
10-
"Parameters": {},
11-
"Attachments": []
5+
}
126
}

internal/inspect/testdata/inspect-no-parameters-json.golden

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,5 @@
99
"email": "dev@example.com"
1010
}
1111
]
12-
},
13-
"Services": [],
14-
"Networks": [],
15-
"Volumes": [],
16-
"Secrets": [],
17-
"Parameters": {},
18-
"Attachments": []
12+
}
1913
}

internal/inspect/testdata/inspect-overridden-json.golden

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,10 @@
88
"Name": "web",
99
"Image": "nginx",
1010
"Replicas": 1,
11-
"Mode": "",
1211
"Ports": "80"
1312
}
1413
],
15-
"Networks": [],
16-
"Volumes": [],
17-
"Secrets": [],
1814
"Parameters": {
1915
"web.port": "80"
20-
},
21-
"Attachments": []
16+
}
2217
}

internal/names.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,9 @@ const (
5050
ParameterOrchestratorName = Namespace + "orchestrator"
5151
// ParameterKubernetesNamespaceName is the name of the parameter containing the kubernetes namespace
5252
ParameterKubernetesNamespaceName = Namespace + "kubernetes-namespace"
53-
// ParameterRenderFormatName is the name of the parameter containing the kubernetes namespace
53+
// ParameterRenderFormatName is the name of the parameter containing the render format
5454
ParameterRenderFormatName = Namespace + "render-format"
55-
// ParameterRenderFormatName is the name of the parameter containing the kubernetes namespace
55+
// ParameterInspectFormatName is the name of the parameter containing the inspect format
5656
ParameterInspectFormatName = Namespace + "inspect-format"
5757
// ParameterShareRegistryCredsName is the name of the parameter which indicates if credentials should be shared
5858
ParameterShareRegistryCredsName = Namespace + "share-registry-creds"
@@ -66,7 +66,8 @@ const (
6666
// DockerRenderFormatEnvVar is the environment variable set by the CNAB runtime to select
6767
// the render output format.
6868
DockerRenderFormatEnvVar = "DOCKER_RENDER_FORMAT"
69-
// DockerInspectFormatEnvVar ...
69+
// DockerInspectFormatEnvVar is the environment variable set by the CNAB runtime to select
70+
// the inspect output format.
7071
DockerInspectFormatEnvVar = "DOCKER_INSPECT_FORMAT"
7172
)
7273

0 commit comments

Comments
 (0)