Skip to content

Commit 8b4d29a

Browse files
authored
Merge pull request #4385 from thaJeztah/config_envvar_const
cli/config: add EnvOverrideConfigDir const
2 parents a26e601 + 0b3cadb commit 8b4d29a

3 files changed

Lines changed: 18 additions & 14 deletions

File tree

cli/config/config.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,15 @@ import (
1616
)
1717

1818
const (
19-
// ConfigFileName is the name of config file
19+
// EnvOverrideConfigDir is the name of the environment variable that can be
20+
// used to override the location of the client configuration files (~/.docker).
21+
//
22+
// It takes priority over the default, but can be overridden by the "--config"
23+
// command line option.
24+
EnvOverrideConfigDir = "DOCKER_CONFIG"
25+
26+
// ConfigFileName is the name of the client configuration file inside the
27+
// config-directory.
2028
ConfigFileName = "config.json"
2129
configFileDir = ".docker"
2230
contextsDir = "contexts"
@@ -30,7 +38,7 @@ var (
3038
// Dir returns the directory the configuration file is stored in
3139
func Dir() string {
3240
initConfigDir.Do(func() {
33-
configDir = os.Getenv("DOCKER_CONFIG")
41+
configDir = os.Getenv(EnvOverrideConfigDir)
3442
if configDir == "" {
3543
configDir = filepath.Join(homedir.Get(), configFileDir)
3644
}

e2e/context/context_test.go

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,15 @@ import (
44
"os"
55
"testing"
66

7+
"github.com/docker/cli/cli/config"
78
"gotest.tools/v3/assert"
89
"gotest.tools/v3/golden"
910
"gotest.tools/v3/icmd"
1011
)
1112

1213
func TestContextList(t *testing.T) {
1314
cmd := icmd.Command("docker", "context", "ls")
14-
cmd.Env = append(cmd.Env, "DOCKER_CONFIG=testdata/test-dockerconfig")
15+
cmd.Env = append(cmd.Env, config.EnvOverrideConfigDir+"=testdata/test-dockerconfig")
1516
result := icmd.RunCmd(cmd).Assert(t, icmd.Expected{
1617
Err: icmd.None,
1718
ExitCode: 0,
@@ -22,29 +23,23 @@ func TestContextList(t *testing.T) {
2223
func TestContextImportNoTLS(t *testing.T) {
2324
d := t.TempDir()
2425
cmd := icmd.Command("docker", "context", "import", "remote", "./testdata/test-dockerconfig.tar")
25-
cmd.Env = append(cmd.Env,
26-
"DOCKER_CONFIG="+d,
27-
)
26+
cmd.Env = append(cmd.Env, config.EnvOverrideConfigDir+"="+d)
2827
icmd.RunCmd(cmd).Assert(t, icmd.Success)
2928

3029
cmd = icmd.Command("docker", "context", "ls")
31-
cmd.Env = append(cmd.Env, "DOCKER_CONFIG="+d)
30+
cmd.Env = append(cmd.Env, config.EnvOverrideConfigDir+"="+d)
3231
result := icmd.RunCmd(cmd).Assert(t, icmd.Success)
3332
golden.Assert(t, result.Stdout(), "context-ls.golden")
3433
}
3534

3635
func TestContextImportTLS(t *testing.T) {
3736
d := t.TempDir()
3837
cmd := icmd.Command("docker", "context", "import", "test", "./testdata/test-dockerconfig-tls.tar")
39-
cmd.Env = append(cmd.Env,
40-
"DOCKER_CONFIG="+d,
41-
)
38+
cmd.Env = append(cmd.Env, config.EnvOverrideConfigDir+"="+d)
4239
icmd.RunCmd(cmd).Assert(t, icmd.Success)
4340

4441
cmd = icmd.Command("docker", "context", "ls")
45-
cmd.Env = append(cmd.Env,
46-
"DOCKER_CONFIG="+d,
47-
)
42+
cmd.Env = append(cmd.Env, config.EnvOverrideConfigDir+"="+d)
4843
result := icmd.RunCmd(cmd).Assert(t, icmd.Success)
4944
golden.Assert(t, result.Stdout(), "context-ls-tls.golden")
5045

e2e/internal/fixtures/fixtures.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"os"
66
"testing"
77

8+
"github.com/docker/cli/cli/config"
89
"gotest.tools/v3/fs"
910
"gotest.tools/v3/icmd"
1011
)
@@ -53,7 +54,7 @@ func SetupConfigWithNotaryURL(t *testing.T, path, notaryURL string) fs.Dir {
5354
// WithConfig sets an environment variable for the docker config location
5455
func WithConfig(dir string) func(cmd *icmd.Cmd) {
5556
return func(cmd *icmd.Cmd) {
56-
addEnvs(cmd, "DOCKER_CONFIG="+dir)
57+
addEnvs(cmd, config.EnvOverrideConfigDir+"="+dir)
5758
}
5859
}
5960

0 commit comments

Comments
 (0)