Skip to content

Commit f61e2bb

Browse files
committed
inspect: add consts / enum for object-types
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
1 parent 25a1681 commit f61e2bb

1 file changed

Lines changed: 37 additions & 21 deletions

File tree

cli/command/system/inspect.go

Lines changed: 37 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,26 @@ import (
2222
"github.com/spf13/cobra"
2323
)
2424

25+
type objectType = string
26+
27+
const (
28+
typeConfig objectType = "config"
29+
typeContainer objectType = "container"
30+
typeImage objectType = "image"
31+
typeNetwork objectType = "network"
32+
typeNode objectType = "node"
33+
typePlugin objectType = "plugin"
34+
typeSecret objectType = "secret"
35+
typeService objectType = "service"
36+
typeTask objectType = "task"
37+
typeVolume objectType = "volume"
38+
)
39+
2540
type inspectOptions struct {
26-
format string
27-
inspectType string
28-
size bool
29-
ids []string
41+
format string
42+
objectType objectType
43+
size bool
44+
ids []string
3045
}
3146

3247
// NewInspectCommand creates a new cobra.Command for `docker inspect`
@@ -45,19 +60,20 @@ func NewInspectCommand(dockerCli command.Cli) *cobra.Command {
4560

4661
flags := cmd.Flags()
4762
flags.StringVarP(&opts.format, "format", "f", "", flagsHelper.InspectFormatHelp)
48-
flags.StringVar(&opts.inspectType, "type", "", "Return JSON for specified type")
63+
flags.StringVar(&opts.objectType, "type", "", "Return JSON for specified type")
4964
flags.BoolVarP(&opts.size, "size", "s", false, "Display total file sizes if the type is container")
5065

5166
return cmd
5267
}
5368

5469
func runInspect(ctx context.Context, dockerCli command.Cli, opts inspectOptions) error {
5570
var elementSearcher inspect.GetRefFunc
56-
switch opts.inspectType {
57-
case "", "config", "container", "image", "network", "node", "plugin", "secret", "service", "task", "volume":
58-
elementSearcher = inspectAll(ctx, dockerCli, opts.size, opts.inspectType)
71+
switch opts.objectType {
72+
case "", typeConfig, typeContainer, typeImage, typeNetwork, typeNode,
73+
typePlugin, typeSecret, typeService, typeTask, typeVolume:
74+
elementSearcher = inspectAll(ctx, dockerCli, opts.size, opts.objectType)
5975
default:
60-
return errors.Errorf("%q is not a valid value for --type", opts.inspectType)
76+
return errors.Errorf("%q is not a valid value for --type", opts.objectType)
6177
}
6278
return inspect.Inspect(dockerCli.Out(), opts.ids, opts.format, elementSearcher)
6379
}
@@ -128,56 +144,56 @@ func inspectConfig(ctx context.Context, dockerCLI command.Cli) inspect.GetRefFun
128144
}
129145
}
130146

131-
func inspectAll(ctx context.Context, dockerCLI command.Cli, getSize bool, typeConstraint string) inspect.GetRefFunc {
147+
func inspectAll(ctx context.Context, dockerCLI command.Cli, getSize bool, typeConstraint objectType) inspect.GetRefFunc {
132148
inspectAutodetect := []struct {
133-
objectType string
149+
objectType objectType
134150
isSizeSupported bool
135151
isSwarmObject bool
136152
objectInspector func(string) (any, []byte, error)
137153
}{
138154
{
139-
objectType: "container",
155+
objectType: typeContainer,
140156
isSizeSupported: true,
141157
objectInspector: inspectContainers(ctx, dockerCLI, getSize),
142158
},
143159
{
144-
objectType: "image",
160+
objectType: typeImage,
145161
objectInspector: inspectImages(ctx, dockerCLI),
146162
},
147163
{
148-
objectType: "network",
164+
objectType: typeNetwork,
149165
objectInspector: inspectNetwork(ctx, dockerCLI),
150166
},
151167
{
152-
objectType: "volume",
168+
objectType: typeVolume,
153169
objectInspector: inspectVolume(ctx, dockerCLI),
154170
},
155171
{
156-
objectType: "service",
172+
objectType: typeService,
157173
isSwarmObject: true,
158174
objectInspector: inspectService(ctx, dockerCLI),
159175
},
160176
{
161-
objectType: "task",
177+
objectType: typeTask,
162178
isSwarmObject: true,
163179
objectInspector: inspectTasks(ctx, dockerCLI),
164180
},
165181
{
166-
objectType: "node",
182+
objectType: typeNode,
167183
isSwarmObject: true,
168184
objectInspector: inspectNode(ctx, dockerCLI),
169185
},
170186
{
171-
objectType: "plugin",
187+
objectType: typePlugin,
172188
objectInspector: inspectPlugin(ctx, dockerCLI),
173189
},
174190
{
175-
objectType: "secret",
191+
objectType: typeSecret,
176192
isSwarmObject: true,
177193
objectInspector: inspectSecret(ctx, dockerCLI),
178194
},
179195
{
180-
objectType: "config",
196+
objectType: typeConfig,
181197
isSwarmObject: true,
182198
objectInspector: inspectConfig(ctx, dockerCLI),
183199
},

0 commit comments

Comments
 (0)