From 0b3cadb05615ea2ca9b487231506f4b802bda537 Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Wed, 28 Jun 2023 13:19:39 +0200 Subject: [PATCH] cli/config: add EnvOverrideConfigDir const Add a const for the DOCKER_CONFIG to allow documenting its purpose in code. Signed-off-by: Sebastiaan van Stijn --- cli/config/config.go | 12 ++++++++++-- e2e/context/context_test.go | 17 ++++++----------- e2e/internal/fixtures/fixtures.go | 3 ++- 3 files changed, 18 insertions(+), 14 deletions(-) diff --git a/cli/config/config.go b/cli/config/config.go index cbcdbc1858..5ea2d92693 100644 --- a/cli/config/config.go +++ b/cli/config/config.go @@ -16,7 +16,15 @@ import ( ) const ( - // ConfigFileName is the name of config file + // EnvOverrideConfigDir is the name of the environment variable that can be + // used to override the location of the client configuration files (~/.docker). + // + // It takes priority over the default, but can be overridden by the "--config" + // command line option. + EnvOverrideConfigDir = "DOCKER_CONFIG" + + // ConfigFileName is the name of the client configuration file inside the + // config-directory. ConfigFileName = "config.json" configFileDir = ".docker" contextsDir = "contexts" @@ -30,7 +38,7 @@ var ( // Dir returns the directory the configuration file is stored in func Dir() string { initConfigDir.Do(func() { - configDir = os.Getenv("DOCKER_CONFIG") + configDir = os.Getenv(EnvOverrideConfigDir) if configDir == "" { configDir = filepath.Join(homedir.Get(), configFileDir) } diff --git a/e2e/context/context_test.go b/e2e/context/context_test.go index d286ff2a05..9fba58326e 100644 --- a/e2e/context/context_test.go +++ b/e2e/context/context_test.go @@ -4,6 +4,7 @@ import ( "os" "testing" + "github.com/docker/cli/cli/config" "gotest.tools/v3/assert" "gotest.tools/v3/golden" "gotest.tools/v3/icmd" @@ -11,7 +12,7 @@ import ( func TestContextList(t *testing.T) { cmd := icmd.Command("docker", "context", "ls") - cmd.Env = append(cmd.Env, "DOCKER_CONFIG=testdata/test-dockerconfig") + cmd.Env = append(cmd.Env, config.EnvOverrideConfigDir+"=testdata/test-dockerconfig") result := icmd.RunCmd(cmd).Assert(t, icmd.Expected{ Err: icmd.None, ExitCode: 0, @@ -22,13 +23,11 @@ func TestContextList(t *testing.T) { func TestContextImportNoTLS(t *testing.T) { d := t.TempDir() cmd := icmd.Command("docker", "context", "import", "remote", "./testdata/test-dockerconfig.tar") - cmd.Env = append(cmd.Env, - "DOCKER_CONFIG="+d, - ) + cmd.Env = append(cmd.Env, config.EnvOverrideConfigDir+"="+d) icmd.RunCmd(cmd).Assert(t, icmd.Success) cmd = icmd.Command("docker", "context", "ls") - cmd.Env = append(cmd.Env, "DOCKER_CONFIG="+d) + cmd.Env = append(cmd.Env, config.EnvOverrideConfigDir+"="+d) result := icmd.RunCmd(cmd).Assert(t, icmd.Success) golden.Assert(t, result.Stdout(), "context-ls.golden") } @@ -36,15 +35,11 @@ func TestContextImportNoTLS(t *testing.T) { func TestContextImportTLS(t *testing.T) { d := t.TempDir() cmd := icmd.Command("docker", "context", "import", "test", "./testdata/test-dockerconfig-tls.tar") - cmd.Env = append(cmd.Env, - "DOCKER_CONFIG="+d, - ) + cmd.Env = append(cmd.Env, config.EnvOverrideConfigDir+"="+d) icmd.RunCmd(cmd).Assert(t, icmd.Success) cmd = icmd.Command("docker", "context", "ls") - cmd.Env = append(cmd.Env, - "DOCKER_CONFIG="+d, - ) + cmd.Env = append(cmd.Env, config.EnvOverrideConfigDir+"="+d) result := icmd.RunCmd(cmd).Assert(t, icmd.Success) golden.Assert(t, result.Stdout(), "context-ls-tls.golden") diff --git a/e2e/internal/fixtures/fixtures.go b/e2e/internal/fixtures/fixtures.go index 8fcc6248ec..ff291c992f 100644 --- a/e2e/internal/fixtures/fixtures.go +++ b/e2e/internal/fixtures/fixtures.go @@ -5,6 +5,7 @@ import ( "os" "testing" + "github.com/docker/cli/cli/config" "gotest.tools/v3/fs" "gotest.tools/v3/icmd" ) @@ -53,7 +54,7 @@ func SetupConfigWithNotaryURL(t *testing.T, path, notaryURL string) fs.Dir { // WithConfig sets an environment variable for the docker config location func WithConfig(dir string) func(cmd *icmd.Cmd) { return func(cmd *icmd.Cmd) { - addEnvs(cmd, "DOCKER_CONFIG="+dir) + addEnvs(cmd, config.EnvOverrideConfigDir+"="+dir) } }