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 <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn 2023-06-28 13:19:39 +02:00
parent a26e6011f0
commit 0b3cadb056
No known key found for this signature in database
GPG Key ID: 76698F39D527CE8C
3 changed files with 18 additions and 14 deletions

View File

@ -16,7 +16,15 @@ import (
) )
const ( 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" ConfigFileName = "config.json"
configFileDir = ".docker" configFileDir = ".docker"
contextsDir = "contexts" contextsDir = "contexts"
@ -30,7 +38,7 @@ var (
// Dir returns the directory the configuration file is stored in // Dir returns the directory the configuration file is stored in
func Dir() string { func Dir() string {
initConfigDir.Do(func() { initConfigDir.Do(func() {
configDir = os.Getenv("DOCKER_CONFIG") configDir = os.Getenv(EnvOverrideConfigDir)
if configDir == "" { if configDir == "" {
configDir = filepath.Join(homedir.Get(), configFileDir) configDir = filepath.Join(homedir.Get(), configFileDir)
} }

View File

@ -4,6 +4,7 @@ import (
"os" "os"
"testing" "testing"
"github.com/docker/cli/cli/config"
"gotest.tools/v3/assert" "gotest.tools/v3/assert"
"gotest.tools/v3/golden" "gotest.tools/v3/golden"
"gotest.tools/v3/icmd" "gotest.tools/v3/icmd"
@ -11,7 +12,7 @@ import (
func TestContextList(t *testing.T) { func TestContextList(t *testing.T) {
cmd := icmd.Command("docker", "context", "ls") 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{ result := icmd.RunCmd(cmd).Assert(t, icmd.Expected{
Err: icmd.None, Err: icmd.None,
ExitCode: 0, ExitCode: 0,
@ -22,13 +23,11 @@ func TestContextList(t *testing.T) {
func TestContextImportNoTLS(t *testing.T) { func TestContextImportNoTLS(t *testing.T) {
d := t.TempDir() d := t.TempDir()
cmd := icmd.Command("docker", "context", "import", "remote", "./testdata/test-dockerconfig.tar") cmd := icmd.Command("docker", "context", "import", "remote", "./testdata/test-dockerconfig.tar")
cmd.Env = append(cmd.Env, cmd.Env = append(cmd.Env, config.EnvOverrideConfigDir+"="+d)
"DOCKER_CONFIG="+d,
)
icmd.RunCmd(cmd).Assert(t, icmd.Success) icmd.RunCmd(cmd).Assert(t, icmd.Success)
cmd = icmd.Command("docker", "context", "ls") 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) result := icmd.RunCmd(cmd).Assert(t, icmd.Success)
golden.Assert(t, result.Stdout(), "context-ls.golden") golden.Assert(t, result.Stdout(), "context-ls.golden")
} }
@ -36,15 +35,11 @@ func TestContextImportNoTLS(t *testing.T) {
func TestContextImportTLS(t *testing.T) { func TestContextImportTLS(t *testing.T) {
d := t.TempDir() d := t.TempDir()
cmd := icmd.Command("docker", "context", "import", "test", "./testdata/test-dockerconfig-tls.tar") cmd := icmd.Command("docker", "context", "import", "test", "./testdata/test-dockerconfig-tls.tar")
cmd.Env = append(cmd.Env, cmd.Env = append(cmd.Env, config.EnvOverrideConfigDir+"="+d)
"DOCKER_CONFIG="+d,
)
icmd.RunCmd(cmd).Assert(t, icmd.Success) icmd.RunCmd(cmd).Assert(t, icmd.Success)
cmd = icmd.Command("docker", "context", "ls") cmd = icmd.Command("docker", "context", "ls")
cmd.Env = append(cmd.Env, cmd.Env = append(cmd.Env, config.EnvOverrideConfigDir+"="+d)
"DOCKER_CONFIG="+d,
)
result := icmd.RunCmd(cmd).Assert(t, icmd.Success) result := icmd.RunCmd(cmd).Assert(t, icmd.Success)
golden.Assert(t, result.Stdout(), "context-ls-tls.golden") golden.Assert(t, result.Stdout(), "context-ls-tls.golden")

View File

@ -5,6 +5,7 @@ import (
"os" "os"
"testing" "testing"
"github.com/docker/cli/cli/config"
"gotest.tools/v3/fs" "gotest.tools/v3/fs"
"gotest.tools/v3/icmd" "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 // WithConfig sets an environment variable for the docker config location
func WithConfig(dir string) func(cmd *icmd.Cmd) { func WithConfig(dir string) func(cmd *icmd.Cmd) {
return func(cmd *icmd.Cmd) { return func(cmd *icmd.Cmd) {
addEnvs(cmd, "DOCKER_CONFIG="+dir) addEnvs(cmd, config.EnvOverrideConfigDir+"="+dir)
} }
} }