2017-03-05 01:10:04 -05:00
|
|
|
package utils
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/docker/docker/api/types"
|
2017-03-05 01:24:17 -05:00
|
|
|
"github.com/docker/docker/cli/config"
|
2017-03-05 01:10:04 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
// GetAuthConfig returns the docker registry AuthConfig.
|
2017-12-11 13:58:05 -05:00
|
|
|
// Optionally takes in the authentication values, otherwise pulls them from the
|
|
|
|
// docker config file.
|
|
|
|
func GetAuthConfig(username, password, registry string) (types.AuthConfig, error) {
|
|
|
|
if username != "" && password != "" && registry != "" {
|
2017-03-05 01:10:04 -05:00
|
|
|
return types.AuthConfig{
|
2017-12-11 13:58:05 -05:00
|
|
|
Username: username,
|
|
|
|
Password: password,
|
|
|
|
ServerAddress: registry,
|
2017-03-05 01:10:04 -05:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2017-03-05 01:24:17 -05:00
|
|
|
dcfg, err := config.Load(config.Dir())
|
2017-03-05 01:10:04 -05:00
|
|
|
if err != nil {
|
|
|
|
return types.AuthConfig{}, fmt.Errorf("Loading config file failed: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// return error early if there are no auths saved
|
|
|
|
if !dcfg.ContainsAuth() {
|
2017-12-11 13:58:05 -05:00
|
|
|
// If we were passed a registry, just use that.
|
|
|
|
if registry != "" {
|
2017-03-05 01:10:04 -05:00
|
|
|
return types.AuthConfig{
|
2017-12-11 13:58:05 -05:00
|
|
|
ServerAddress: registry,
|
2017-03-05 01:10:04 -05:00
|
|
|
}, nil
|
|
|
|
}
|
2017-12-11 13:58:05 -05:00
|
|
|
|
|
|
|
// Otherwise, just use an empty auth config.
|
|
|
|
return types.AuthConfig{}, nil
|
2017-03-05 01:10:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// if they passed a specific registry, return those creds _if_ they exist
|
2017-12-11 13:58:05 -05:00
|
|
|
if registry != "" {
|
2017-07-17 13:00:25 -04:00
|
|
|
// try with the user input
|
2017-12-11 13:58:05 -05:00
|
|
|
if creds, ok := dcfg.AuthConfigs[registry]; ok {
|
2017-03-05 01:10:04 -05:00
|
|
|
return creds, nil
|
|
|
|
}
|
2017-07-17 13:00:25 -04:00
|
|
|
// add https:// to user input and try again
|
|
|
|
// see https://github.com/jessfraz/reg/issues/32
|
2017-12-11 13:58:05 -05:00
|
|
|
if !strings.HasPrefix(registry, "https://") && !strings.HasPrefix(registry, "http://") {
|
|
|
|
if creds, ok := dcfg.AuthConfigs["https://"+registry]; ok {
|
2017-07-17 13:00:25 -04:00
|
|
|
return creds, nil
|
|
|
|
}
|
|
|
|
}
|
2017-12-11 13:58:05 -05:00
|
|
|
|
|
|
|
// Otherwise just use the registry with no auth.
|
2017-08-10 15:15:19 -04:00
|
|
|
return types.AuthConfig{
|
2017-12-11 13:58:05 -05:00
|
|
|
ServerAddress: registry,
|
2017-08-10 15:15:19 -04:00
|
|
|
}, nil
|
2017-03-05 01:10:04 -05:00
|
|
|
}
|
|
|
|
|
2017-12-11 13:58:05 -05:00
|
|
|
// Just set the auth config as the first registryURL, username and password
|
|
|
|
// found in the auth config.
|
2017-03-05 01:10:04 -05:00
|
|
|
for _, creds := range dcfg.AuthConfigs {
|
|
|
|
return creds, nil
|
|
|
|
}
|
|
|
|
|
2017-12-11 13:58:05 -05:00
|
|
|
// Don't use any authentication.
|
|
|
|
return types.AuthConfig{}, nil
|
2017-03-05 01:10:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetRepoAndRef parses the repo name and reference.
|
2017-12-11 13:58:05 -05:00
|
|
|
func GetRepoAndRef(arg string) (repo, ref string, err error) {
|
|
|
|
if arg == "" {
|
2017-03-05 01:10:04 -05:00
|
|
|
return "", "", errors.New("pass the name of the repository")
|
|
|
|
}
|
|
|
|
|
2017-12-11 13:39:02 -05:00
|
|
|
var parts []string
|
2017-03-05 01:10:04 -05:00
|
|
|
if strings.Contains(arg, "@") {
|
2017-12-11 13:58:05 -05:00
|
|
|
parts = strings.Split(arg, "@")
|
2017-03-05 01:10:04 -05:00
|
|
|
} else if strings.Contains(arg, ":") {
|
2017-12-11 13:58:05 -05:00
|
|
|
parts = strings.Split(arg, ":")
|
2017-03-05 01:10:04 -05:00
|
|
|
} else {
|
|
|
|
parts = []string{arg}
|
|
|
|
}
|
|
|
|
|
|
|
|
repo = parts[0]
|
|
|
|
ref = "latest"
|
|
|
|
if len(parts) > 1 {
|
|
|
|
ref = parts[1]
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|