Skip to content

Commit 2f6abcf

Browse files
authored
Merge pull request #6446 from thaJeztah/fix_completions
Improve shell completion for `docker secret` and `docker config` subcommands
2 parents c5467b5 + 8325214 commit 2f6abcf

File tree

12 files changed

+44
-29
lines changed

12 files changed

+44
-29
lines changed

cli/command/config/create.go

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,23 @@ func newConfigCreateCommand(dockerCLI command.Cli) *cobra.Command {
3636
createOpts.file = args[1]
3737
return runCreate(cmd.Context(), dockerCLI, createOpts)
3838
},
39-
ValidArgsFunction: cobra.NoFileCompletions,
39+
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
40+
switch len(args) {
41+
case 0:
42+
// No completion for the first argument, which is the name for
43+
// the new config, but if a non-empty name is given, we return
44+
// it as completion to allow "tab"-ing to the next completion.
45+
return []string{toComplete}, cobra.ShellCompDirectiveNoFileComp
46+
case 1:
47+
// Second argument is either "-" or a file to load.
48+
//
49+
// TODO(thaJeztah): provide completion for "-".
50+
return nil, cobra.ShellCompDirectiveNoSpace | cobra.ShellCompDirectiveDefault
51+
default:
52+
// Command only accepts two arguments.
53+
return nil, cobra.ShellCompDirectiveNoSpace | cobra.ShellCompDirectiveNoFileComp
54+
}
55+
},
4056
DisableFlagsInUseLine: true,
4157
}
4258
flags := cmd.Flags()

cli/command/config/inspect.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,7 @@ func newConfigInspectCommand(dockerCLI command.Cli) *cobra.Command {
3232
opts.names = args
3333
return runInspect(cmd.Context(), dockerCLI, opts)
3434
},
35-
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
36-
return completeNames(dockerCLI)(cmd, args, toComplete)
37-
},
35+
ValidArgsFunction: completeNames(dockerCLI),
3836
DisableFlagsInUseLine: true,
3937
}
4038

cli/command/config/remove.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,7 @@ func newConfigRemoveCommand(dockerCLI command.Cli) *cobra.Command {
1919
RunE: func(cmd *cobra.Command, args []string) error {
2020
return runRemove(cmd.Context(), dockerCLI, args)
2121
},
22-
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
23-
return completeNames(dockerCLI)(cmd, args, toComplete)
24-
},
22+
ValidArgsFunction: completeNames(dockerCLI),
2523
DisableFlagsInUseLine: true,
2624
}
2725
}

cli/command/secret/cmd.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ func completeNames(dockerCLI completion.APIClientProvider) cobra.CompletionFunc
4444
}
4545
var names []string
4646
for _, secret := range list {
47-
names = append(names, secret.ID)
47+
names = append(names, secret.Spec.Name)
4848
}
4949
return names, cobra.ShellCompDirectiveNoFileComp
5050
}

cli/command/secret/create.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,23 @@ func newSecretCreateCommand(dockerCLI command.Cli) *cobra.Command {
3838
}
3939
return runSecretCreate(cmd.Context(), dockerCLI, options)
4040
},
41+
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
42+
switch len(args) {
43+
case 0:
44+
// No completion for the first argument, which is the name for
45+
// the new secret, but if a non-empty name is given, we return
46+
// it as completion to allow "tab"-ing to the next completion.
47+
return []string{toComplete}, cobra.ShellCompDirectiveNoFileComp
48+
case 1:
49+
// Second argument is either "-" or a file to load.
50+
//
51+
// TODO(thaJeztah): provide completion for "-".
52+
return nil, cobra.ShellCompDirectiveNoSpace | cobra.ShellCompDirectiveDefault
53+
default:
54+
// Command only accepts two arguments.
55+
return nil, cobra.ShellCompDirectiveNoSpace | cobra.ShellCompDirectiveNoFileComp
56+
}
57+
},
4158
DisableFlagsInUseLine: true,
4259
}
4360
flags := cmd.Flags()

cli/command/secret/inspect.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,7 @@ func newSecretInspectCommand(dockerCLI command.Cli) *cobra.Command {
3131
opts.names = args
3232
return runSecretInspect(cmd.Context(), dockerCLI, opts)
3333
},
34-
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
35-
return completeNames(dockerCLI)(cmd, args, toComplete)
36-
},
34+
ValidArgsFunction: completeNames(dockerCLI),
3735
DisableFlagsInUseLine: true,
3836
}
3937

cli/command/secret/ls.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,7 @@ func newSecretListCommand(dockerCLI command.Cli) *cobra.Command {
3131
RunE: func(cmd *cobra.Command, args []string) error {
3232
return runSecretList(cmd.Context(), dockerCLI, options)
3333
},
34-
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
35-
return completeNames(dockerCLI)(cmd, args, toComplete)
36-
},
34+
ValidArgsFunction: cobra.NoFileCompletions,
3735
DisableFlagsInUseLine: true,
3836
}
3937

cli/command/secret/remove.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,7 @@ func newSecretRemoveCommand(dockerCLI command.Cli) *cobra.Command {
2626
}
2727
return runRemove(cmd.Context(), dockerCLI, opts)
2828
},
29-
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
30-
return completeNames(dockerCLI)(cmd, args, toComplete)
31-
},
29+
ValidArgsFunction: completeNames(dockerCLI),
3230
DisableFlagsInUseLine: true,
3331
}
3432
}

cli/command/stack/deploy.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,7 @@ func newDeployCommand(dockerCLI command.Cli) *cobra.Command {
4545
}
4646
return runDeploy(cmd.Context(), dockerCLI, cmd.Flags(), &opts, config)
4747
},
48-
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
49-
return completeNames(dockerCLI)(cmd, args, toComplete)
50-
},
48+
ValidArgsFunction: completeNames(dockerCLI),
5149
DisableFlagsInUseLine: true,
5250
}
5351

cli/command/stack/ps.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,7 @@ func newPsCommand(dockerCLI command.Cli) *cobra.Command {
3838
}
3939
return runPS(cmd.Context(), dockerCLI, opts)
4040
},
41-
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
42-
return completeNames(dockerCLI)(cmd, args, toComplete)
43-
},
41+
ValidArgsFunction: completeNames(dockerCLI),
4442
DisableFlagsInUseLine: true,
4543
}
4644
flags := cmd.Flags()

0 commit comments

Comments
 (0)