mirror of https://github.com/docker/cli.git
cli/command/completion: add EnvVarNames utility
EnvVarNames offers completion for environment-variable names. This completion can be used for "--env" and "--build-arg" flags, which allow obtaining the value of the given environment-variable if present in the local environment, so we only should complete the names of the environment variables, and not their value. This also prevents the completion script from printing values of environment variables containing sensitive values. For example; export MY_VAR=hello docker run --rm --env MY_VAR alpine printenv MY_VAR hello Before this patch: docker run --env GO GO111MODULE=auto GOLANG_VERSION=1.21.12 GOPATH=/go GOTOOLCHAIN=local With this patch: docker run --env GO<tab> GO111MODULE GOLANG_VERSION GOPATH GOTOOLCHAIN Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
parent
9207ff1046
commit
e3427f341b
|
@ -2,6 +2,7 @@ package completion
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"os"
|
"os"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/docker/cli/cli/command/formatter"
|
"github.com/docker/cli/cli/command/formatter"
|
||||||
"github.com/docker/docker/api/types/container"
|
"github.com/docker/docker/api/types/container"
|
||||||
|
@ -105,6 +106,29 @@ func NetworkNames(dockerCLI APIClientProvider) ValidArgsFn {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// EnvVarNames offers completion for environment-variable names. This
|
||||||
|
// completion can be used for "--env" and "--build-arg" flags, which
|
||||||
|
// allow obtaining the value of the given environment-variable if present
|
||||||
|
// in the local environment, so we only should complete the names of the
|
||||||
|
// environment variables, and not their value. This also prevents the
|
||||||
|
// completion script from printing values of environment variables
|
||||||
|
// containing sensitive values.
|
||||||
|
//
|
||||||
|
// For example;
|
||||||
|
//
|
||||||
|
// export MY_VAR=hello
|
||||||
|
// docker run --rm --env MY_VAR alpine printenv MY_VAR
|
||||||
|
// hello
|
||||||
|
func EnvVarNames(_ *cobra.Command, _ []string, _ string) (names []string, _ cobra.ShellCompDirective) {
|
||||||
|
envs := os.Environ()
|
||||||
|
names = make([]string, 0, len(envs))
|
||||||
|
for _, env := range envs {
|
||||||
|
name, _, _ := strings.Cut(env, "=")
|
||||||
|
names = append(names, name)
|
||||||
|
}
|
||||||
|
return names, cobra.ShellCompDirectiveNoFileComp
|
||||||
|
}
|
||||||
|
|
||||||
// FileNames is a convenience function to use [cobra.ShellCompDirectiveDefault],
|
// FileNames is a convenience function to use [cobra.ShellCompDirectiveDefault],
|
||||||
// which indicates to let the shell perform its default behavior after
|
// which indicates to let the shell perform its default behavior after
|
||||||
// completions have been provided.
|
// completions have been provided.
|
||||||
|
|
|
@ -4,7 +4,6 @@ import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"os"
|
|
||||||
|
|
||||||
"github.com/docker/cli/cli"
|
"github.com/docker/cli/cli"
|
||||||
"github.com/docker/cli/cli/command"
|
"github.com/docker/cli/cli/command"
|
||||||
|
@ -78,9 +77,7 @@ func NewExecCommand(dockerCli command.Cli) *cobra.Command {
|
||||||
flags.StringVarP(&options.Workdir, "workdir", "w", "", "Working directory inside the container")
|
flags.StringVarP(&options.Workdir, "workdir", "w", "", "Working directory inside the container")
|
||||||
flags.SetAnnotation("workdir", "version", []string{"1.35"})
|
flags.SetAnnotation("workdir", "version", []string{"1.35"})
|
||||||
|
|
||||||
_ = cmd.RegisterFlagCompletionFunc("env", func(*cobra.Command, []string, string) ([]string, cobra.ShellCompDirective) {
|
_ = cmd.RegisterFlagCompletionFunc("env", completion.EnvVarNames)
|
||||||
return os.Environ(), cobra.ShellCompDirectiveNoFileComp
|
|
||||||
})
|
|
||||||
_ = cmd.RegisterFlagCompletionFunc("env-file", completion.FileNames)
|
_ = cmd.RegisterFlagCompletionFunc("env-file", completion.FileNames)
|
||||||
|
|
||||||
return cmd
|
return cmd
|
||||||
|
|
|
@ -4,7 +4,6 @@ import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"os"
|
|
||||||
"strings"
|
"strings"
|
||||||
"syscall"
|
"syscall"
|
||||||
|
|
||||||
|
@ -70,9 +69,7 @@ func NewRunCommand(dockerCli command.Cli) *cobra.Command {
|
||||||
command.AddTrustVerificationFlags(flags, &options.untrusted, dockerCli.ContentTrustEnabled())
|
command.AddTrustVerificationFlags(flags, &options.untrusted, dockerCli.ContentTrustEnabled())
|
||||||
copts = addFlags(flags)
|
copts = addFlags(flags)
|
||||||
|
|
||||||
_ = cmd.RegisterFlagCompletionFunc("env", func(*cobra.Command, []string, string) ([]string, cobra.ShellCompDirective) {
|
_ = cmd.RegisterFlagCompletionFunc("env", completion.EnvVarNames)
|
||||||
return os.Environ(), cobra.ShellCompDirectiveNoFileComp
|
|
||||||
})
|
|
||||||
_ = cmd.RegisterFlagCompletionFunc("env-file", completion.FileNames)
|
_ = cmd.RegisterFlagCompletionFunc("env-file", completion.FileNames)
|
||||||
_ = cmd.RegisterFlagCompletionFunc("network", completion.NetworkNames(dockerCli))
|
_ = cmd.RegisterFlagCompletionFunc("network", completion.NetworkNames(dockerCli))
|
||||||
return cmd
|
return cmd
|
||||||
|
|
Loading…
Reference in New Issue