diff --git a/auth.go b/auth.go index a6e2fa94..9730ad21 100644 --- a/auth.go +++ b/auth.go @@ -104,17 +104,20 @@ func NewAuthConfigurationsFromFile(path string) (*AuthConfigurations, error) { } func cfgPaths(dockerConfigEnv string, homeEnv string) []string { - var paths []string if dockerConfigEnv != "" { - paths = append(paths, path.Join(dockerConfigEnv, "plaintext-passwords.json")) - paths = append(paths, path.Join(dockerConfigEnv, "config.json")) + return []string{ + path.Join(dockerConfigEnv, "plaintext-passwords.json"), + path.Join(dockerConfigEnv, "config.json"), + } } if homeEnv != "" { - paths = append(paths, path.Join(homeEnv, ".docker", "plaintext-passwords.json")) - paths = append(paths, path.Join(homeEnv, ".docker", "config.json")) - paths = append(paths, path.Join(homeEnv, ".dockercfg")) + return []string{ + path.Join(homeEnv, ".docker", "plaintext-passwords.json"), + path.Join(homeEnv, ".docker", "config.json"), + path.Join(homeEnv, ".dockercfg"), + } } - return paths + return nil } // NewAuthConfigurationsFromDockerCfg returns AuthConfigurations from @@ -128,6 +131,7 @@ func NewAuthConfigurationsFromDockerCfg() (*AuthConfigurations, error) { return nil, errors.New("no docker configuration found") } + var result *AuthConfigurations var auths *AuthConfigurations var err error for _, path := range pathsToTry { @@ -136,14 +140,17 @@ func NewAuthConfigurationsFromDockerCfg() (*AuthConfigurations, error) { continue } - if !auths.isEmpty() { - return auths, nil - } - if err == nil { - return auths, nil + if result == nil { + result = auths + } else { + result.merge(*auths) } } - return auths, err + + if result != nil { + return result, nil + } + return result, err } // NewAuthConfigurations returns AuthConfigurations from a JSON encoded string in the diff --git a/auth_test.go b/auth_test.go index bd1c4144..7176df0a 100644 --- a/auth_test.go +++ b/auth_test.go @@ -26,7 +26,7 @@ func TestAuthConfigurationSearchPath(t *testing.T) { {"", "", []string{}}, {"", "home", []string{path.Join("home", ".docker", "plaintext-passwords.json"), path.Join("home", ".docker", "config.json"), path.Join("home", ".dockercfg")}}, {"docker_config", "", []string{path.Join("docker_config", "plaintext-passwords.json"), path.Join("docker_config", "config.json")}}, - {"a", "b", []string{path.Join("a", "plaintext-passwords.json"), path.Join("a", "config.json"), path.Join("b", ".docker", "plaintext-passwords.json"), path.Join("b", ".docker", "config.json"), path.Join("b", ".dockercfg")}}, + {"a", "b", []string{path.Join("a", "plaintext-passwords.json"), path.Join("a", "config.json")}}, } for _, tt := range testData { tt := tt