inspect: add consts / enum for object-types

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn 2025-05-06 14:05:32 +02:00
parent 25a168106a
commit f61e2bb6f1
No known key found for this signature in database
GPG Key ID: 76698F39D527CE8C

View File

@ -22,11 +22,26 @@ import (
"github.com/spf13/cobra"
)
type objectType = string
const (
typeConfig objectType = "config"
typeContainer objectType = "container"
typeImage objectType = "image"
typeNetwork objectType = "network"
typeNode objectType = "node"
typePlugin objectType = "plugin"
typeSecret objectType = "secret"
typeService objectType = "service"
typeTask objectType = "task"
typeVolume objectType = "volume"
)
type inspectOptions struct {
format string
inspectType string
size bool
ids []string
format string
objectType objectType
size bool
ids []string
}
// NewInspectCommand creates a new cobra.Command for `docker inspect`
@ -45,7 +60,7 @@ func NewInspectCommand(dockerCli command.Cli) *cobra.Command {
flags := cmd.Flags()
flags.StringVarP(&opts.format, "format", "f", "", flagsHelper.InspectFormatHelp)
flags.StringVar(&opts.inspectType, "type", "", "Return JSON for specified type")
flags.StringVar(&opts.objectType, "type", "", "Return JSON for specified type")
flags.BoolVarP(&opts.size, "size", "s", false, "Display total file sizes if the type is container")
return cmd
@ -53,11 +68,12 @@ func NewInspectCommand(dockerCli command.Cli) *cobra.Command {
func runInspect(ctx context.Context, dockerCli command.Cli, opts inspectOptions) error {
var elementSearcher inspect.GetRefFunc
switch opts.inspectType {
case "", "config", "container", "image", "network", "node", "plugin", "secret", "service", "task", "volume":
elementSearcher = inspectAll(ctx, dockerCli, opts.size, opts.inspectType)
switch opts.objectType {
case "", typeConfig, typeContainer, typeImage, typeNetwork, typeNode,
typePlugin, typeSecret, typeService, typeTask, typeVolume:
elementSearcher = inspectAll(ctx, dockerCli, opts.size, opts.objectType)
default:
return errors.Errorf("%q is not a valid value for --type", opts.inspectType)
return errors.Errorf("%q is not a valid value for --type", opts.objectType)
}
return inspect.Inspect(dockerCli.Out(), opts.ids, opts.format, elementSearcher)
}
@ -128,56 +144,56 @@ func inspectConfig(ctx context.Context, dockerCLI command.Cli) inspect.GetRefFun
}
}
func inspectAll(ctx context.Context, dockerCLI command.Cli, getSize bool, typeConstraint string) inspect.GetRefFunc {
func inspectAll(ctx context.Context, dockerCLI command.Cli, getSize bool, typeConstraint objectType) inspect.GetRefFunc {
inspectAutodetect := []struct {
objectType string
objectType objectType
isSizeSupported bool
isSwarmObject bool
objectInspector func(string) (any, []byte, error)
}{
{
objectType: "container",
objectType: typeContainer,
isSizeSupported: true,
objectInspector: inspectContainers(ctx, dockerCLI, getSize),
},
{
objectType: "image",
objectType: typeImage,
objectInspector: inspectImages(ctx, dockerCLI),
},
{
objectType: "network",
objectType: typeNetwork,
objectInspector: inspectNetwork(ctx, dockerCLI),
},
{
objectType: "volume",
objectType: typeVolume,
objectInspector: inspectVolume(ctx, dockerCLI),
},
{
objectType: "service",
objectType: typeService,
isSwarmObject: true,
objectInspector: inspectService(ctx, dockerCLI),
},
{
objectType: "task",
objectType: typeTask,
isSwarmObject: true,
objectInspector: inspectTasks(ctx, dockerCLI),
},
{
objectType: "node",
objectType: typeNode,
isSwarmObject: true,
objectInspector: inspectNode(ctx, dockerCLI),
},
{
objectType: "plugin",
objectType: typePlugin,
objectInspector: inspectPlugin(ctx, dockerCLI),
},
{
objectType: "secret",
objectType: typeSecret,
isSwarmObject: true,
objectInspector: inspectSecret(ctx, dockerCLI),
},
{
objectType: "config",
objectType: typeConfig,
isSwarmObject: true,
objectInspector: inspectConfig(ctx, dockerCLI),
},