Skip to content

Commit 52752f3

Browse files
committed
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>
1 parent 8c5aaff commit 52752f3

2 files changed

Lines changed: 52 additions & 1 deletion

File tree

cli/command/system/inspect.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,9 @@ func NewInspectCommand(dockerCli command.Cli) *cobra.Command {
6969
Args: cli.RequiresMinArgs(1),
7070
RunE: func(cmd *cobra.Command, args []string) error {
7171
opts.ids = args
72+
if cmd.Flags().Changed("type") && opts.objectType == "" {
73+
return fmt.Errorf(`type is empty: must be one of "%s"`, strings.Join(allTypes, `", "`))
74+
}
7275
return runInspect(cmd.Context(), dockerCli, opts)
7376
},
7477
// 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)
97100
typePlugin, typeSecret, typeService, typeTask, typeVolume:
98101
elementSearcher = inspectAll(ctx, dockerCli, opts.size, opts.objectType)
99102
default:
100-
return errors.Errorf("%q is not a valid value for --type", opts.objectType)
103+
return errors.Errorf(`unknown type: %q: must be one of "%s"`, opts.objectType, strings.Join(allTypes, `", "`))
101104
}
102105
return inspect.Inspect(dockerCli.Out(), opts.ids, opts.format, elementSearcher)
103106
}

cli/command/system/inspect_test.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package system
2+
3+
import (
4+
"io"
5+
"testing"
6+
7+
"github.com/docker/cli/internal/test"
8+
"gotest.tools/v3/assert"
9+
is "gotest.tools/v3/assert/cmp"
10+
)
11+
12+
func TestInspectValidateFlagsAndArgs(t *testing.T) {
13+
for _, tc := range []struct {
14+
name string
15+
args []string
16+
expectedErr string
17+
}{
18+
{
19+
name: "empty type",
20+
args: []string{"--type", "", "something"},
21+
expectedErr: `type is empty: must be one of "config", "container", "image", "network", "node", "plugin", "secret", "service", "task", "volume"`,
22+
},
23+
{
24+
name: "unknown type",
25+
args: []string{"--type", "unknown", "something"},
26+
expectedErr: `unknown type: "unknown": must be one of "config", "container", "image", "network", "node", "plugin", "secret", "service", "task", "volume"`,
27+
},
28+
{
29+
name: "no arg",
30+
args: []string{},
31+
expectedErr: `inspect: 'inspect' requires at least 1 argument`,
32+
},
33+
} {
34+
t.Run(tc.name, func(t *testing.T) {
35+
cmd := NewInspectCommand(test.NewFakeCli(&fakeClient{}))
36+
cmd.SetOut(io.Discard)
37+
cmd.SetErr(io.Discard)
38+
cmd.SetArgs(tc.args)
39+
40+
err := cmd.Execute()
41+
if tc.expectedErr != "" {
42+
assert.Check(t, is.ErrorContains(err, tc.expectedErr))
43+
} else {
44+
assert.Check(t, is.Nil(err))
45+
}
46+
})
47+
}
48+
}

0 commit comments

Comments
 (0)