inspect: improve (flag) validation

Produce an error if the `--type` flag was set, but an empty value
was passed.

Before this patch:

    docker inspect --type "" foo
    # json output

    docker inspect --type unknown foo
    "unknown" is not a valid value for --type

With this patch:

    docker inspect --type "" foo
    type is empty: must be one of "config", "container", "image", "network", "node", "plugin", "secret", "service", "task", "volume"

    docker inspect --type unknown foo
    unknown type: "unknown": must be one of "config", "container", "image", "network", "node", "plugin", "secret", "service", "task", "volume"

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn 2025-05-06 15:05:06 +02:00
parent 8c5aaff57f
commit 52752f3aa2
No known key found for this signature in database
GPG Key ID: 76698F39D527CE8C
2 changed files with 52 additions and 1 deletions

View File

@ -69,6 +69,9 @@ func NewInspectCommand(dockerCli command.Cli) *cobra.Command {
Args: cli.RequiresMinArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
opts.ids = args
if cmd.Flags().Changed("type") && opts.objectType == "" {
return fmt.Errorf(`type is empty: must be one of "%s"`, strings.Join(allTypes, `", "`))
}
return runInspect(cmd.Context(), dockerCli, opts)
},
// TODO(thaJeztah): should we consider adding completion for common object-types? (images, containers?)
@ -97,7 +100,7 @@ func runInspect(ctx context.Context, dockerCli command.Cli, opts inspectOptions)
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.objectType)
return errors.Errorf(`unknown type: %q: must be one of "%s"`, opts.objectType, strings.Join(allTypes, `", "`))
}
return inspect.Inspect(dockerCli.Out(), opts.ids, opts.format, elementSearcher)
}

View File

@ -0,0 +1,48 @@
package system
import (
"io"
"testing"
"github.com/docker/cli/internal/test"
"gotest.tools/v3/assert"
is "gotest.tools/v3/assert/cmp"
)
func TestInspectValidateFlagsAndArgs(t *testing.T) {
for _, tc := range []struct {
name string
args []string
expectedErr string
}{
{
name: "empty type",
args: []string{"--type", "", "something"},
expectedErr: `type is empty: must be one of "config", "container", "image", "network", "node", "plugin", "secret", "service", "task", "volume"`,
},
{
name: "unknown type",
args: []string{"--type", "unknown", "something"},
expectedErr: `unknown type: "unknown": must be one of "config", "container", "image", "network", "node", "plugin", "secret", "service", "task", "volume"`,
},
{
name: "no arg",
args: []string{},
expectedErr: `inspect: 'inspect' requires at least 1 argument`,
},
} {
t.Run(tc.name, func(t *testing.T) {
cmd := NewInspectCommand(test.NewFakeCli(&fakeClient{}))
cmd.SetOut(io.Discard)
cmd.SetErr(io.Discard)
cmd.SetArgs(tc.args)
err := cmd.Execute()
if tc.expectedErr != "" {
assert.Check(t, is.ErrorContains(err, tc.expectedErr))
} else {
assert.Check(t, is.Nil(err))
}
})
}
}