load authConfig only when needed and fix useless WARNING

This commit is contained in:
Victor Vieux 2013-08-16 14:29:40 +00:00
parent 92a2b635a3
commit 18962d0ff3
2 changed files with 22 additions and 11 deletions

View File

@ -76,7 +76,7 @@ func LoadConfig(rootPath string) (*ConfigFile, error) {
configFile := ConfigFile{Configs: make(map[string]AuthConfig), rootPath: rootPath} configFile := ConfigFile{Configs: make(map[string]AuthConfig), rootPath: rootPath}
confFile := path.Join(rootPath, CONFIGFILE) confFile := path.Join(rootPath, CONFIGFILE)
if _, err := os.Stat(confFile); err != nil { if _, err := os.Stat(confFile); err != nil {
return &configFile, ErrConfigFileMissing return &configFile, nil //missing file is not an error
} }
b, err := ioutil.ReadFile(confFile) b, err := ioutil.ReadFile(confFile)
if err != nil { if err != nil {
@ -86,13 +86,13 @@ func LoadConfig(rootPath string) (*ConfigFile, error) {
if err := json.Unmarshal(b, &configFile.Configs); err != nil { if err := json.Unmarshal(b, &configFile.Configs); err != nil {
arr := strings.Split(string(b), "\n") arr := strings.Split(string(b), "\n")
if len(arr) < 2 { if len(arr) < 2 {
return nil, fmt.Errorf("The Auth config file is empty") return &configFile, fmt.Errorf("The Auth config file is empty")
} }
authConfig := AuthConfig{} authConfig := AuthConfig{}
origAuth := strings.Split(arr[0], " = ") origAuth := strings.Split(arr[0], " = ")
authConfig.Username, authConfig.Password, err = decodeAuth(origAuth[1]) authConfig.Username, authConfig.Password, err = decodeAuth(origAuth[1])
if err != nil { if err != nil {
return nil, err return &configFile, err
} }
origEmail := strings.Split(arr[1], " = ") origEmail := strings.Split(arr[1], " = ")
authConfig.Email = origEmail[1] authConfig.Email = origEmail[1]
@ -101,7 +101,7 @@ func LoadConfig(rootPath string) (*ConfigFile, error) {
for k, authConfig := range configFile.Configs { for k, authConfig := range configFile.Configs {
authConfig.Username, authConfig.Password, err = decodeAuth(authConfig.Auth) authConfig.Username, authConfig.Password, err = decodeAuth(authConfig.Auth)
if err != nil { if err != nil {
return nil, err return &configFile, err
} }
authConfig.Auth = "" authConfig.Auth = ""
configFile.Configs[k] = authConfig configFile.Configs[k] = authConfig

View File

@ -303,6 +303,8 @@ func (cli *DockerCli) CmdLogin(args ...string) error {
return nil return nil
} }
cli.LoadConfigFile()
var oldState *term.State var oldState *term.State
if *flUsername == "" || *flPassword == "" || *flEmail == "" { if *flUsername == "" || *flPassword == "" || *flEmail == "" {
oldState, err = term.SetRawTerminal(cli.terminalFd) oldState, err = term.SetRawTerminal(cli.terminalFd)
@ -498,6 +500,7 @@ func (cli *DockerCli) CmdInfo(args ...string) error {
} }
if len(out.IndexServerAddress) != 0 { if len(out.IndexServerAddress) != 0 {
cli.LoadConfigFile()
u := cli.configFile.Configs[out.IndexServerAddress].Username u := cli.configFile.Configs[out.IndexServerAddress].Username
if len(u) > 0 { if len(u) > 0 {
fmt.Fprintf(cli.out, "Username: %v\n", u) fmt.Fprintf(cli.out, "Username: %v\n", u)
@ -838,12 +841,18 @@ func (cli *DockerCli) CmdPush(args ...string) error {
return nil return nil
} }
cli.LoadConfigFile()
// If we're not using a custom registry, we know the restrictions // If we're not using a custom registry, we know the restrictions
// applied to repository names and can warn the user in advance. // applied to repository names and can warn the user in advance.
// Custom repositories can have different rules, and we must also // Custom repositories can have different rules, and we must also
// allow pushing by image ID. // allow pushing by image ID.
if len(strings.SplitN(name, "/", 2)) == 1 { if len(strings.SplitN(name, "/", 2)) == 1 {
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) username := cli.configFile.Configs[auth.IndexServerAddress()].Username
if username == "" {
username = "<user>"
}
return fmt.Errorf("Impossible to push a \"root\" repository. Please rename your repository in <user>/<repo> (ex: %s/%s)", username, name)
} }
v := url.Values{} v := url.Values{}
@ -1745,6 +1754,14 @@ func Subcmd(name, signature, description string) *flag.FlagSet {
return flags return flags
} }
func (cli *DockerCli) LoadConfigFile() (err error) {
cli.configFile, err = auth.LoadConfig(os.Getenv("HOME"))
if err != nil {
fmt.Fprintf(cli.err, "WARNING: %s\n", err)
}
return err
}
func NewDockerCli(in io.ReadCloser, out, err io.Writer, proto, addr string) *DockerCli { func NewDockerCli(in io.ReadCloser, out, err io.Writer, proto, addr string) *DockerCli {
var ( var (
isTerminal = false isTerminal = false
@ -1761,15 +1778,9 @@ func NewDockerCli(in io.ReadCloser, out, err io.Writer, proto, addr string) *Doc
if err == nil { if err == nil {
err = out err = out
} }
configFile, e := auth.LoadConfig(os.Getenv("HOME"))
if e != nil {
fmt.Fprintf(err, "WARNING: %s\n", e)
}
return &DockerCli{ return &DockerCli{
proto: proto, proto: proto,
addr: addr, addr: addr,
configFile: configFile,
in: in, in: in,
out: out, out: out,
err: err, err: err,