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
Родитель a26e6011f0
Коммит 0b3cadb056
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 76698F39D527CE8C
3 изменённых файлов: 18 добавлений и 14 удалений

Просмотреть файл

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

Просмотреть файл

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

Просмотреть файл

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