(ctx context.Context, dockerCLI command.Cli, opts loginOptions)
| 148 | } |
| 149 | |
| 150 | func runLogin(ctx context.Context, dockerCLI command.Cli, opts loginOptions) error { |
| 151 | if err := verifyLoginOptions(dockerCLI, &opts); err != nil { |
| 152 | return err |
| 153 | } |
| 154 | |
| 155 | maybePrintEnvAuthWarning(dockerCLI) |
| 156 | |
| 157 | var ( |
| 158 | serverAddress string |
| 159 | msg string |
| 160 | ) |
| 161 | if opts.serverAddress != "" && opts.serverAddress != registry.DefaultNamespace { |
| 162 | serverAddress = opts.serverAddress |
| 163 | } else { |
| 164 | serverAddress = registry.IndexServer |
| 165 | } |
| 166 | isDefaultRegistry := serverAddress == registry.IndexServer |
| 167 | |
| 168 | // attempt login with current (stored) credentials |
| 169 | authConfig, err := command.GetDefaultAuthConfig(dockerCLI.ConfigFile(), opts.user == "" && opts.password == "", serverAddress, isDefaultRegistry) |
| 170 | if err == nil && authConfig.Username != "" && authConfig.Password != "" { |
| 171 | msg, err = loginWithStoredCredentials(ctx, dockerCLI, authConfig) |
| 172 | } |
| 173 | |
| 174 | // if we failed to authenticate with stored credentials (or didn't have stored credentials), |
| 175 | // prompt the user for new credentials |
| 176 | if err != nil || authConfig.Username == "" || authConfig.Password == "" { |
| 177 | msg, err = loginUser(ctx, dockerCLI, opts, authConfig.Username, authConfig.ServerAddress) |
| 178 | if err != nil { |
| 179 | return err |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | if msg != "" { |
| 184 | _, _ = fmt.Fprintln(dockerCLI.Out(), msg) |
| 185 | } |
| 186 | return nil |
| 187 | } |
| 188 | |
| 189 | func loginWithStoredCredentials(ctx context.Context, dockerCLI command.Cli, authConfig registrytypes.AuthConfig) (msg string, _ error) { |
| 190 | _, _ = fmt.Fprintf(dockerCLI.Err(), "Authenticating with existing credentials...") |
searching dependent graphs…