Skip to content

Commit 18962d0

Browse files
committed
load authConfig only when needed and fix useless WARNING
1 parent 92a2b63 commit 18962d0

2 files changed

Lines changed: 22 additions & 11 deletions

File tree

auth/auth.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ func LoadConfig(rootPath string) (*ConfigFile, error) {
7676
configFile := ConfigFile{Configs: make(map[string]AuthConfig), rootPath: rootPath}
7777
confFile := path.Join(rootPath, CONFIGFILE)
7878
if _, err := os.Stat(confFile); err != nil {
79-
return &configFile, ErrConfigFileMissing
79+
return &configFile, nil //missing file is not an error
8080
}
8181
b, err := ioutil.ReadFile(confFile)
8282
if err != nil {
@@ -86,13 +86,13 @@ func LoadConfig(rootPath string) (*ConfigFile, error) {
8686
if err := json.Unmarshal(b, &configFile.Configs); err != nil {
8787
arr := strings.Split(string(b), "\n")
8888
if len(arr) < 2 {
89-
return nil, fmt.Errorf("The Auth config file is empty")
89+
return &configFile, fmt.Errorf("The Auth config file is empty")
9090
}
9191
authConfig := AuthConfig{}
9292
origAuth := strings.Split(arr[0], " = ")
9393
authConfig.Username, authConfig.Password, err = decodeAuth(origAuth[1])
9494
if err != nil {
95-
return nil, err
95+
return &configFile, err
9696
}
9797
origEmail := strings.Split(arr[1], " = ")
9898
authConfig.Email = origEmail[1]
@@ -101,7 +101,7 @@ func LoadConfig(rootPath string) (*ConfigFile, error) {
101101
for k, authConfig := range configFile.Configs {
102102
authConfig.Username, authConfig.Password, err = decodeAuth(authConfig.Auth)
103103
if err != nil {
104-
return nil, err
104+
return &configFile, err
105105
}
106106
authConfig.Auth = ""
107107
configFile.Configs[k] = authConfig

commands.go

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,8 @@ func (cli *DockerCli) CmdLogin(args ...string) error {
303303
return nil
304304
}
305305

306+
cli.LoadConfigFile()
307+
306308
var oldState *term.State
307309
if *flUsername == "" || *flPassword == "" || *flEmail == "" {
308310
oldState, err = term.SetRawTerminal(cli.terminalFd)
@@ -498,6 +500,7 @@ func (cli *DockerCli) CmdInfo(args ...string) error {
498500
}
499501

500502
if len(out.IndexServerAddress) != 0 {
503+
cli.LoadConfigFile()
501504
u := cli.configFile.Configs[out.IndexServerAddress].Username
502505
if len(u) > 0 {
503506
fmt.Fprintf(cli.out, "Username: %v\n", u)
@@ -838,12 +841,18 @@ func (cli *DockerCli) CmdPush(args ...string) error {
838841
return nil
839842
}
840843

844+
cli.LoadConfigFile()
845+
841846
// If we're not using a custom registry, we know the restrictions
842847
// applied to repository names and can warn the user in advance.
843848
// Custom repositories can have different rules, and we must also
844849
// allow pushing by image ID.
845850
if len(strings.SplitN(name, "/", 2)) == 1 {
846-
return fmt.Errorf("Impossible to push a \"root\" repository. Please rename your repository in <user>/<repo> (ex: %s/%s)", cli.configFile.Configs[auth.IndexServerAddress()].Username, name)
851+
username := cli.configFile.Configs[auth.IndexServerAddress()].Username
852+
if username == "" {
853+
username = "<user>"
854+
}
855+
return fmt.Errorf("Impossible to push a \"root\" repository. Please rename your repository in <user>/<repo> (ex: %s/%s)", username, name)
847856
}
848857

849858
v := url.Values{}
@@ -1745,6 +1754,14 @@ func Subcmd(name, signature, description string) *flag.FlagSet {
17451754
return flags
17461755
}
17471756

1757+
func (cli *DockerCli) LoadConfigFile() (err error) {
1758+
cli.configFile, err = auth.LoadConfig(os.Getenv("HOME"))
1759+
if err != nil {
1760+
fmt.Fprintf(cli.err, "WARNING: %s\n", err)
1761+
}
1762+
return err
1763+
}
1764+
17481765
func NewDockerCli(in io.ReadCloser, out, err io.Writer, proto, addr string) *DockerCli {
17491766
var (
17501767
isTerminal = false
@@ -1761,15 +1778,9 @@ func NewDockerCli(in io.ReadCloser, out, err io.Writer, proto, addr string) *Doc
17611778
if err == nil {
17621779
err = out
17631780
}
1764-
1765-
configFile, e := auth.LoadConfig(os.Getenv("HOME"))
1766-
if e != nil {
1767-
fmt.Fprintf(err, "WARNING: %s\n", e)
1768-
}
17691781
return &DockerCli{
17701782
proto: proto,
17711783
addr: addr,
1772-
configFile: configFile,
17731784
in: in,
17741785
out: out,
17751786
err: err,

0 commit comments

Comments
 (0)