Merge pull request #4385 from thaJeztah/config_envvar_const

cli/config: add EnvOverrideConfigDir const
This commit is contained in:
Sebastiaan van Stijn 2023-06-28 14:07:48 +02:00 committed by GitHub
commit 8b4d29a453
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 18 additions and 14 deletions

View File

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

View File

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

View File

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