Restrict completion for some commands with a limit paramter for ImageNames
Previously, multiple suggestions were provided when completing commands like `run`, `history` and `push`. This change limits completion to a single suggestion for the above and 2 suggestions for `tag` Signed-off-by: Mohammed Aminu Futa <mohammedfuta2000@gmail.com>
This commit is contained in:
parent
516e822d4c
commit
a656dfd409
@ -27,8 +27,11 @@ type APIClientProvider interface {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// ImageNames offers completion for images present within the local store
|
// ImageNames offers completion for images present within the local store
|
||||||
func ImageNames(dockerCLI APIClientProvider) ValidArgsFn {
|
func ImageNames(dockerCLI APIClientProvider, limit int) ValidArgsFn {
|
||||||
return func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
|
return func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
|
||||||
|
if limit > 0 && len(args) >= limit {
|
||||||
|
return nil, cobra.ShellCompDirectiveNoFileComp
|
||||||
|
}
|
||||||
list, err := dockerCLI.Client().ImageList(cmd.Context(), image.ListOptions{})
|
list, err := dockerCLI.Client().ImageList(cmd.Context(), image.ListOptions{})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, cobra.ShellCompDirectiveError
|
return nil, cobra.ShellCompDirectiveError
|
||||||
|
@ -234,7 +234,7 @@ func TestCompleteImageNames(t *testing.T) {
|
|||||||
}
|
}
|
||||||
return tc.images, nil
|
return tc.images, nil
|
||||||
},
|
},
|
||||||
}})
|
}}, -1)
|
||||||
|
|
||||||
volumes, directives := comp(&cobra.Command{}, nil, "")
|
volumes, directives := comp(&cobra.Command{}, nil, "")
|
||||||
assert.Check(t, is.Equal(directives&tc.expDirective, tc.expDirective))
|
assert.Check(t, is.Equal(directives&tc.expDirective, tc.expDirective))
|
||||||
|
@ -60,7 +60,7 @@ func NewCreateCommand(dockerCli command.Cli) *cobra.Command {
|
|||||||
Annotations: map[string]string{
|
Annotations: map[string]string{
|
||||||
"aliases": "docker container create, docker create",
|
"aliases": "docker container create, docker create",
|
||||||
},
|
},
|
||||||
ValidArgsFunction: completion.ImageNames(dockerCli),
|
ValidArgsFunction: completion.ImageNames(dockerCli, -1),
|
||||||
}
|
}
|
||||||
|
|
||||||
flags := cmd.Flags()
|
flags := cmd.Flags()
|
||||||
|
@ -43,7 +43,7 @@ func NewRunCommand(dockerCli command.Cli) *cobra.Command {
|
|||||||
}
|
}
|
||||||
return runRun(cmd.Context(), dockerCli, cmd.Flags(), &options, copts)
|
return runRun(cmd.Context(), dockerCli, cmd.Flags(), &options, copts)
|
||||||
},
|
},
|
||||||
ValidArgsFunction: completion.ImageNames(dockerCli),
|
ValidArgsFunction: completion.ImageNames(dockerCli, 1),
|
||||||
Annotations: map[string]string{
|
Annotations: map[string]string{
|
||||||
"category-top": "1",
|
"category-top": "1",
|
||||||
"aliases": "docker container run, docker run",
|
"aliases": "docker container run, docker run",
|
||||||
|
@ -36,7 +36,7 @@ func NewHistoryCommand(dockerCli command.Cli) *cobra.Command {
|
|||||||
opts.image = args[0]
|
opts.image = args[0]
|
||||||
return runHistory(cmd.Context(), dockerCli, opts)
|
return runHistory(cmd.Context(), dockerCli, opts)
|
||||||
},
|
},
|
||||||
ValidArgsFunction: completion.ImageNames(dockerCli),
|
ValidArgsFunction: completion.ImageNames(dockerCli, 1),
|
||||||
Annotations: map[string]string{
|
Annotations: map[string]string{
|
||||||
"aliases": "docker image history, docker history",
|
"aliases": "docker image history, docker history",
|
||||||
},
|
},
|
||||||
|
@ -34,7 +34,7 @@ func newInspectCommand(dockerCli command.Cli) *cobra.Command {
|
|||||||
opts.refs = args
|
opts.refs = args
|
||||||
return runInspect(cmd.Context(), dockerCli, opts)
|
return runInspect(cmd.Context(), dockerCli, opts)
|
||||||
},
|
},
|
||||||
ValidArgsFunction: completion.ImageNames(dockerCli),
|
ValidArgsFunction: completion.ImageNames(dockerCli, -1),
|
||||||
}
|
}
|
||||||
|
|
||||||
flags := cmd.Flags()
|
flags := cmd.Flags()
|
||||||
|
@ -51,7 +51,7 @@ func NewPushCommand(dockerCli command.Cli) *cobra.Command {
|
|||||||
"category-top": "6",
|
"category-top": "6",
|
||||||
"aliases": "docker image push, docker push",
|
"aliases": "docker image push, docker push",
|
||||||
},
|
},
|
||||||
ValidArgsFunction: completion.ImageNames(dockerCli),
|
ValidArgsFunction: completion.ImageNames(dockerCli, 1),
|
||||||
}
|
}
|
||||||
|
|
||||||
flags := cmd.Flags()
|
flags := cmd.Flags()
|
||||||
|
@ -29,7 +29,7 @@ func NewRemoveCommand(dockerCli command.Cli) *cobra.Command {
|
|||||||
RunE: func(cmd *cobra.Command, args []string) error {
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
return runRemove(cmd.Context(), dockerCli, opts, args)
|
return runRemove(cmd.Context(), dockerCli, opts, args)
|
||||||
},
|
},
|
||||||
ValidArgsFunction: completion.ImageNames(dockerCli),
|
ValidArgsFunction: completion.ImageNames(dockerCli, -1),
|
||||||
Annotations: map[string]string{
|
Annotations: map[string]string{
|
||||||
"aliases": "docker image rm, docker image remove, docker rmi",
|
"aliases": "docker image rm, docker image remove, docker rmi",
|
||||||
},
|
},
|
||||||
|
@ -34,7 +34,7 @@ func NewSaveCommand(dockerCli command.Cli) *cobra.Command {
|
|||||||
Annotations: map[string]string{
|
Annotations: map[string]string{
|
||||||
"aliases": "docker image save, docker save",
|
"aliases": "docker image save, docker save",
|
||||||
},
|
},
|
||||||
ValidArgsFunction: completion.ImageNames(dockerCli),
|
ValidArgsFunction: completion.ImageNames(dockerCli, -1),
|
||||||
}
|
}
|
||||||
|
|
||||||
flags := cmd.Flags()
|
flags := cmd.Flags()
|
||||||
|
@ -30,7 +30,7 @@ func NewTagCommand(dockerCli command.Cli) *cobra.Command {
|
|||||||
Annotations: map[string]string{
|
Annotations: map[string]string{
|
||||||
"aliases": "docker image tag, docker tag",
|
"aliases": "docker image tag, docker tag",
|
||||||
},
|
},
|
||||||
ValidArgsFunction: completion.ImageNames(dockerCli),
|
ValidArgsFunction: completion.ImageNames(dockerCli, 2),
|
||||||
}
|
}
|
||||||
|
|
||||||
flags := cmd.Flags()
|
flags := cmd.Flags()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user