Skip to content

Commit d18af47

Browse files
committed
cli/command/completion: change EnvVarNames to return a cobra.CompletionFunc
It's adding a slight indirection by constructing a function when called, but makes the completion functions more consistent, the signature easier to read, and making the return type a [cobra.CompletionFunc] makes it more transparent what it's intended for, and helps discovery of functions that provide completion. [cobra.CompletionFunc]: https://pkg.go.dev/github.com/spf13/cobra#CompletionFunc Signed-off-by: Sebastiaan van Stijn <[email protected]>
1 parent c5467b5 commit d18af47

File tree

6 files changed

+14
-12
lines changed

6 files changed

+14
-12
lines changed

cli/command/completion/functions.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -116,14 +116,16 @@ func NetworkNames(dockerCLI APIClientProvider) cobra.CompletionFunc {
116116
// export MY_VAR=hello
117117
// docker run --rm --env MY_VAR alpine printenv MY_VAR
118118
// hello
119-
func EnvVarNames(_ *cobra.Command, _ []string, _ string) (names []string, _ cobra.ShellCompDirective) {
120-
envs := os.Environ()
121-
names = make([]string, 0, len(envs))
122-
for _, env := range envs {
123-
name, _, _ := strings.Cut(env, "=")
124-
names = append(names, name)
119+
func EnvVarNames() cobra.CompletionFunc {
120+
return func(_ *cobra.Command, _ []string, _ string) (names []string, _ cobra.ShellCompDirective) {
121+
envs := os.Environ()
122+
names = make([]string, 0, len(envs))
123+
for _, env := range envs {
124+
name, _, _ := strings.Cut(env, "=")
125+
names = append(names, name)
126+
}
127+
return names, cobra.ShellCompDirectiveNoFileComp
125128
}
126-
return names, cobra.ShellCompDirectiveNoFileComp
127129
}
128130

129131
// FromList offers completion for the given list of options.

cli/command/completion/functions_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ func TestCompleteEnvVarNames(t *testing.T) {
176176
"ENV_A": "hello-a",
177177
"ENV_B": "hello-b",
178178
})
179-
values, directives := EnvVarNames(nil, nil, "")
179+
values, directives := EnvVarNames()(nil, nil, "")
180180
assert.Check(t, is.Equal(directives&cobra.ShellCompDirectiveNoFileComp, cobra.ShellCompDirectiveNoFileComp), "Should not perform file completion")
181181

182182
sort.Strings(values)

cli/command/container/completion.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ func addCompletions(cmd *cobra.Command, dockerCLI completion.APIClientProvider)
122122
_ = cmd.RegisterFlagCompletionFunc("cap-add", completeLinuxCapabilityNames)
123123
_ = cmd.RegisterFlagCompletionFunc("cap-drop", completeLinuxCapabilityNames)
124124
_ = cmd.RegisterFlagCompletionFunc("cgroupns", completeCgroupns())
125-
_ = cmd.RegisterFlagCompletionFunc("env", completion.EnvVarNames)
125+
_ = cmd.RegisterFlagCompletionFunc("env", completion.EnvVarNames())
126126
_ = cmd.RegisterFlagCompletionFunc("env-file", completion.FileNames)
127127
_ = cmd.RegisterFlagCompletionFunc("ipc", completeIpc(dockerCLI))
128128
_ = cmd.RegisterFlagCompletionFunc("link", completeLink(dockerCLI))

cli/command/container/exec.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ func newExecCommand(dockerCLI command.Cli) *cobra.Command {
7878
flags.StringVarP(&options.Workdir, "workdir", "w", "", "Working directory inside the container")
7979
flags.SetAnnotation("workdir", "version", []string{"1.35"})
8080

81-
_ = cmd.RegisterFlagCompletionFunc("env", completion.EnvVarNames)
81+
_ = cmd.RegisterFlagCompletionFunc("env", completion.EnvVarNames())
8282
_ = cmd.RegisterFlagCompletionFunc("env-file", completion.FileNames)
8383

8484
return cmd

cli/command/service/create.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ func newCreateCommand(dockerCLI command.Cli) *cobra.Command {
8282
// _ = cmd.RegisterFlagCompletionFunc(flagStopSignal, completeSignals)
8383

8484
_ = cmd.RegisterFlagCompletionFunc(flagMode, completion.FromList("replicated", "global", "replicated-job", "global-job"))
85-
_ = cmd.RegisterFlagCompletionFunc(flagEnv, completion.EnvVarNames) // TODO(thaJeztah): flagEnvRemove (needs to read current env-vars on the service)
85+
_ = cmd.RegisterFlagCompletionFunc(flagEnv, completion.EnvVarNames()) // TODO(thaJeztah): flagEnvRemove (needs to read current env-vars on the service)
8686
_ = cmd.RegisterFlagCompletionFunc(flagEnvFile, completion.FileNames)
8787
_ = cmd.RegisterFlagCompletionFunc(flagNetwork, completion.NetworkNames(dockerCLI))
8888
_ = cmd.RegisterFlagCompletionFunc(flagRestartCondition, completion.FromList("none", "on-failure", "any"))

cli/command/service/update.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ func newUpdateCommand(dockerCLI command.Cli) *cobra.Command {
121121
// _ = cmd.RegisterFlagCompletionFunc(flagCapDrop, completeLinuxCapabilityNames)
122122
// _ = cmd.RegisterFlagCompletionFunc(flagStopSignal, completeSignals)
123123

124-
_ = cmd.RegisterFlagCompletionFunc(flagEnvAdd, completion.EnvVarNames)
124+
_ = cmd.RegisterFlagCompletionFunc(flagEnvAdd, completion.EnvVarNames())
125125
// TODO(thaJeztah): flagEnvRemove (needs to read current env-vars on the service)
126126
_ = cmd.RegisterFlagCompletionFunc("image", completion.ImageNames(dockerCLI, -1))
127127
_ = cmd.RegisterFlagCompletionFunc(flagNetworkAdd, completion.NetworkNames(dockerCLI))

0 commit comments

Comments
 (0)