Skip to content

Commit 8325214

Browse files
committed
cli/command: fix completion for secret create, config create
These commands accept two arguments; the first is a custom name, the second is either a filename or "-" to create from STDIN. With this patch: # does not provide completion docker secret create <tab> # starts providing completion once a non-empty name is provided docker secret create somename<tab> file.txt other-file.txt Signed-off-by: Sebastiaan van Stijn <[email protected]>
1 parent 924dd47 commit 8325214

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
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/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()

0 commit comments

Comments
 (0)