inspect: add consts / enum for object-types
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
parent
25a168106a
commit
f61e2bb6f1
@ -22,9 +22,24 @@ import (
|
|||||||
"github.com/spf13/cobra"
|
"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 {
|
type inspectOptions struct {
|
||||||
format string
|
format string
|
||||||
inspectType string
|
objectType objectType
|
||||||
size bool
|
size bool
|
||||||
ids []string
|
ids []string
|
||||||
}
|
}
|
||||||
@ -45,7 +60,7 @@ func NewInspectCommand(dockerCli command.Cli) *cobra.Command {
|
|||||||
|
|
||||||
flags := cmd.Flags()
|
flags := cmd.Flags()
|
||||||
flags.StringVarP(&opts.format, "format", "f", "", flagsHelper.InspectFormatHelp)
|
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")
|
flags.BoolVarP(&opts.size, "size", "s", false, "Display total file sizes if the type is container")
|
||||||
|
|
||||||
return cmd
|
return cmd
|
||||||
@ -53,11 +68,12 @@ func NewInspectCommand(dockerCli command.Cli) *cobra.Command {
|
|||||||
|
|
||||||
func runInspect(ctx context.Context, dockerCli command.Cli, opts inspectOptions) error {
|
func runInspect(ctx context.Context, dockerCli command.Cli, opts inspectOptions) error {
|
||||||
var elementSearcher inspect.GetRefFunc
|
var elementSearcher inspect.GetRefFunc
|
||||||
switch opts.inspectType {
|
switch opts.objectType {
|
||||||
case "", "config", "container", "image", "network", "node", "plugin", "secret", "service", "task", "volume":
|
case "", typeConfig, typeContainer, typeImage, typeNetwork, typeNode,
|
||||||
elementSearcher = inspectAll(ctx, dockerCli, opts.size, opts.inspectType)
|
typePlugin, typeSecret, typeService, typeTask, typeVolume:
|
||||||
|
elementSearcher = inspectAll(ctx, dockerCli, opts.size, opts.objectType)
|
||||||
default:
|
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)
|
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 {
|
inspectAutodetect := []struct {
|
||||||
objectType string
|
objectType objectType
|
||||||
isSizeSupported bool
|
isSizeSupported bool
|
||||||
isSwarmObject bool
|
isSwarmObject bool
|
||||||
objectInspector func(string) (any, []byte, error)
|
objectInspector func(string) (any, []byte, error)
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
objectType: "container",
|
objectType: typeContainer,
|
||||||
isSizeSupported: true,
|
isSizeSupported: true,
|
||||||
objectInspector: inspectContainers(ctx, dockerCLI, getSize),
|
objectInspector: inspectContainers(ctx, dockerCLI, getSize),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
objectType: "image",
|
objectType: typeImage,
|
||||||
objectInspector: inspectImages(ctx, dockerCLI),
|
objectInspector: inspectImages(ctx, dockerCLI),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
objectType: "network",
|
objectType: typeNetwork,
|
||||||
objectInspector: inspectNetwork(ctx, dockerCLI),
|
objectInspector: inspectNetwork(ctx, dockerCLI),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
objectType: "volume",
|
objectType: typeVolume,
|
||||||
objectInspector: inspectVolume(ctx, dockerCLI),
|
objectInspector: inspectVolume(ctx, dockerCLI),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
objectType: "service",
|
objectType: typeService,
|
||||||
isSwarmObject: true,
|
isSwarmObject: true,
|
||||||
objectInspector: inspectService(ctx, dockerCLI),
|
objectInspector: inspectService(ctx, dockerCLI),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
objectType: "task",
|
objectType: typeTask,
|
||||||
isSwarmObject: true,
|
isSwarmObject: true,
|
||||||
objectInspector: inspectTasks(ctx, dockerCLI),
|
objectInspector: inspectTasks(ctx, dockerCLI),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
objectType: "node",
|
objectType: typeNode,
|
||||||
isSwarmObject: true,
|
isSwarmObject: true,
|
||||||
objectInspector: inspectNode(ctx, dockerCLI),
|
objectInspector: inspectNode(ctx, dockerCLI),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
objectType: "plugin",
|
objectType: typePlugin,
|
||||||
objectInspector: inspectPlugin(ctx, dockerCLI),
|
objectInspector: inspectPlugin(ctx, dockerCLI),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
objectType: "secret",
|
objectType: typeSecret,
|
||||||
isSwarmObject: true,
|
isSwarmObject: true,
|
||||||
objectInspector: inspectSecret(ctx, dockerCLI),
|
objectInspector: inspectSecret(ctx, dockerCLI),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
objectType: "config",
|
objectType: typeConfig,
|
||||||
isSwarmObject: true,
|
isSwarmObject: true,
|
||||||
objectInspector: inspectConfig(ctx, dockerCLI),
|
objectInspector: inspectConfig(ctx, dockerCLI),
|
||||||
},
|
},
|
||||||
|
Loading…
x
Reference in New Issue
Block a user