diff --git a/cli/command/cli.go b/cli/command/cli.go index becfcde163..6057bc1dd1 100644 --- a/cli/command/cli.go +++ b/cli/command/cli.go @@ -411,7 +411,7 @@ func getServerHost(hosts []string, tlsOptions *tlsconfig.Options) (string, error var host string switch len(hosts) { case 0: - host = os.Getenv("DOCKER_HOST") + host = os.Getenv(client.EnvOverrideHost) case 1: host = hosts[0] default: @@ -429,7 +429,7 @@ func UserAgent() string { // resolveContextName resolves the current context name with the following rules: // - setting both --context and --host flags is ambiguous // - if --context is set, use this value -// - if --host flag or DOCKER_HOST is set, fallbacks to use the same logic as before context-store was added +// - if --host flag or DOCKER_HOST (client.EnvOverrideHost) is set, fallbacks to use the same logic as before context-store was added // for backward compatibility with existing scripts // - if DOCKER_CONTEXT is set, use this value // - if Config file has a globally set "CurrentContext", use this value @@ -444,7 +444,7 @@ func resolveContextName(opts *cliflags.CommonOptions, config *configfile.ConfigF if len(opts.Hosts) > 0 { return DefaultContextName, nil } - if _, present := os.LookupEnv("DOCKER_HOST"); present { + if _, present := os.LookupEnv(client.EnvOverrideHost); present { return DefaultContextName, nil } if ctxName, ok := os.LookupEnv("DOCKER_CONTEXT"); ok { diff --git a/cli/command/context/list.go b/cli/command/context/list.go index 8e8a71950f..a1b2af53bc 100644 --- a/cli/command/context/list.go +++ b/cli/command/context/list.go @@ -10,6 +10,7 @@ import ( "github.com/docker/cli/cli/command/formatter" "github.com/docker/cli/cli/context/docker" flagsHelper "github.com/docker/cli/cli/flags" + "github.com/docker/docker/client" "github.com/fvbommel/sortorder" "github.com/spf13/cobra" ) @@ -73,9 +74,9 @@ func runList(dockerCli command.Cli, opts *listOptions) error { if err := format(dockerCli, opts, contexts); err != nil { return err } - if os.Getenv("DOCKER_HOST") != "" { - fmt.Fprint(dockerCli.Err(), "Warning: DOCKER_HOST environment variable overrides the active context. "+ - "To use a context, either set the global --context flag, or unset DOCKER_HOST environment variable.\n") + if os.Getenv(client.EnvOverrideHost) != "" { + fmt.Fprintf(dockerCli.Err(), "Warning: %[1]s environment variable overrides the active context. "+ + "To use a context, either set the global --context flag, or unset %[1]s environment variable.\n", client.EnvOverrideHost) } return nil } diff --git a/cli/command/context/use.go b/cli/command/context/use.go index a3998da69d..ab2a6f8d78 100644 --- a/cli/command/context/use.go +++ b/cli/command/context/use.go @@ -6,6 +6,7 @@ import ( "github.com/docker/cli/cli/command" "github.com/docker/cli/cli/context/store" + "github.com/docker/docker/client" "github.com/spf13/cobra" ) @@ -41,9 +42,9 @@ func RunUse(dockerCli command.Cli, name string) error { } fmt.Fprintln(dockerCli.Out(), name) fmt.Fprintf(dockerCli.Err(), "Current context is now %q\n", name) - if os.Getenv("DOCKER_HOST") != "" { - fmt.Fprintf(dockerCli.Err(), "Warning: DOCKER_HOST environment variable overrides the active context. "+ - "To use %q, either set the global --context flag, or unset DOCKER_HOST environment variable.\n", name) + if os.Getenv(client.EnvOverrideHost) != "" { + fmt.Fprintf(dockerCli.Err(), "Warning: %[1]s environment variable overrides the active context. "+ + "To use %[2]q, either set the global --context flag, or unset %[1]s environment variable.\n", client.EnvOverrideHost, name) } return nil } diff --git a/cli/flags/common.go b/cli/flags/common.go index a770333a49..fbe686c5d2 100644 --- a/cli/flags/common.go +++ b/cli/flags/common.go @@ -7,6 +7,7 @@ import ( "github.com/docker/cli/cli/config" "github.com/docker/cli/opts" + "github.com/docker/docker/client" "github.com/docker/go-connections/tlsconfig" "github.com/sirupsen/logrus" "github.com/spf13/pflag" @@ -36,9 +37,10 @@ Refer to https://docs.docker.com/go/formatting/ for more information about forma ) var ( - dockerCertPath = os.Getenv("DOCKER_CERT_PATH") - dockerTLSVerify = os.Getenv("DOCKER_TLS_VERIFY") != "" - dockerTLS = os.Getenv("DOCKER_TLS") != "" + dockerCertPath = os.Getenv(client.EnvOverrideCertPath) + dockerTLSVerify = os.Getenv(client.EnvTLSVerify) != "" + // TODO(thaJeztah) the 'DOCKER_TLS' environment variable is not documented, and does not have a const. + dockerTLS = os.Getenv("DOCKER_TLS") != "" ) // CommonOptions are options common to both the client and the daemon. @@ -84,7 +86,7 @@ func (commonOpts *CommonOptions) InstallFlags(flags *pflag.FlagSet) { hostOpt := opts.NewNamedListOptsRef("hosts", &commonOpts.Hosts, nil) flags.VarP(hostOpt, "host", "H", "Daemon socket(s) to connect to") flags.StringVarP(&commonOpts.Context, "context", "c", "", - `Name of the context to use to connect to the daemon (overrides DOCKER_HOST env var and default context set with "docker context use")`) + `Name of the context to use to connect to the daemon (overrides `+client.EnvOverrideHost+` env var and default context set with "docker context use")`) } // SetDefaultOptions sets default values for options after flag parsing is diff --git a/internal/test/environment/testenv.go b/internal/test/environment/testenv.go index bb80a9b2cc..a719c1b117 100644 --- a/internal/test/environment/testenv.go +++ b/internal/test/environment/testenv.go @@ -7,6 +7,7 @@ import ( "testing" "time" + "github.com/docker/docker/client" "github.com/pkg/errors" "gotest.tools/v3/icmd" "gotest.tools/v3/poll" @@ -19,15 +20,15 @@ func Setup() error { if dockerHost == "" { return errors.New("$TEST_DOCKER_HOST must be set") } - if err := os.Setenv("DOCKER_HOST", dockerHost); err != nil { + if err := os.Setenv(client.EnvOverrideHost, dockerHost); err != nil { return err } if dockerCertPath := os.Getenv("TEST_DOCKER_CERT_PATH"); dockerCertPath != "" { - if err := os.Setenv("DOCKER_CERT_PATH", dockerCertPath); err != nil { + if err := os.Setenv(client.EnvOverrideCertPath, dockerCertPath); err != nil { return err } - if err := os.Setenv("DOCKER_TLS_VERIFY", "1"); err != nil { + if err := os.Setenv(client.EnvTLSVerify, "1"); err != nil { return err } }