use client consts for environment variable names

It's slightly more verbose, but helps finding the purpose of each
of the environment variables. In tests, I kept the fixed strings.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn 2022-03-25 00:15:14 +01:00
parent 68cad50f61
commit 1db2da57c8
No known key found for this signature in database
GPG Key ID: 76698F39D527CE8C
5 changed files with 21 additions and 16 deletions

View File

@ -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 {

View File

@ -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
}

View File

@ -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
}

View File

@ -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

View File

@ -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
}
}