mirror of
https://github.com/genuinetools/reg.git
synced 2024-09-28 11:46:20 -04:00
update getauthconfig to be more reusable
Signed-off-by: Jess Frazelle <acidburn@microsoft.com>
This commit is contained in:
parent
9bb4c0ee3e
commit
54adaea027
3 changed files with 34 additions and 27 deletions
10
main.go
10
main.go
|
@ -39,7 +39,7 @@ func preload(c *cli.Context) (err error) {
|
||||||
|
|
||||||
if len(c.Args()) > 0 {
|
if len(c.Args()) > 0 {
|
||||||
if c.Args()[0] != "help" {
|
if c.Args()[0] != "help" {
|
||||||
auth, err = utils.GetAuthConfig(c)
|
auth, err = utils.GetAuthConfig(c.GlobalString("username"), c.GlobalString("password"), c.GlobalString("registry"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -107,7 +107,7 @@ func main() {
|
||||||
Aliases: []string{"rm"},
|
Aliases: []string{"rm"},
|
||||||
Usage: "delete a specific reference of a repository",
|
Usage: "delete a specific reference of a repository",
|
||||||
Action: func(c *cli.Context) error {
|
Action: func(c *cli.Context) error {
|
||||||
repo, ref, err := utils.GetRepoAndRef(c)
|
repo, ref, err := utils.GetRepoAndRef(c.Args()[0])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -169,7 +169,7 @@ func main() {
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
Action: func(c *cli.Context) error {
|
Action: func(c *cli.Context) error {
|
||||||
repo, ref, err := utils.GetRepoAndRef(c)
|
repo, ref, err := utils.GetRepoAndRef(c.Args()[0])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -227,7 +227,7 @@ func main() {
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
Action: func(c *cli.Context) error {
|
Action: func(c *cli.Context) error {
|
||||||
repo, ref, err := utils.GetRepoAndRef(c)
|
repo, ref, err := utils.GetRepoAndRef(c.Args()[0])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -266,7 +266,7 @@ func main() {
|
||||||
return errors.New("clair url cannot be empty, pass --clair")
|
return errors.New("clair url cannot be empty, pass --clair")
|
||||||
}
|
}
|
||||||
|
|
||||||
repo, ref, err := utils.GetRepoAndRef(c)
|
repo, ref, err := utils.GetRepoAndRef(c.Args()[0])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
|
@ -98,7 +98,7 @@ func main() {
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
app.Action = func(c *cli.Context) error {
|
app.Action = func(c *cli.Context) error {
|
||||||
auth, err := utils.GetAuthConfig(c)
|
auth, err := utils.GetAuthConfig(c.GlobalString("username"), c.GlobalString("password"), c.GlobalString("registry"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logrus.Fatal(err)
|
logrus.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,16 +7,17 @@ import (
|
||||||
|
|
||||||
"github.com/docker/docker/api/types"
|
"github.com/docker/docker/api/types"
|
||||||
"github.com/docker/docker/cli/config"
|
"github.com/docker/docker/cli/config"
|
||||||
"github.com/urfave/cli"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// GetAuthConfig returns the docker registry AuthConfig.
|
// GetAuthConfig returns the docker registry AuthConfig.
|
||||||
func GetAuthConfig(c *cli.Context) (types.AuthConfig, error) {
|
// Optionally takes in the authentication values, otherwise pulls them from the
|
||||||
if c.GlobalString("username") != "" && c.GlobalString("password") != "" && c.GlobalString("registry") != "" {
|
// docker config file.
|
||||||
|
func GetAuthConfig(username, password, registry string) (types.AuthConfig, error) {
|
||||||
|
if username != "" && password != "" && registry != "" {
|
||||||
return types.AuthConfig{
|
return types.AuthConfig{
|
||||||
Username: c.GlobalString("username"),
|
Username: username,
|
||||||
Password: c.GlobalString("password"),
|
Password: password,
|
||||||
ServerAddress: c.GlobalString("registry"),
|
ServerAddress: registry,
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -27,52 +28,58 @@ func GetAuthConfig(c *cli.Context) (types.AuthConfig, error) {
|
||||||
|
|
||||||
// return error early if there are no auths saved
|
// return error early if there are no auths saved
|
||||||
if !dcfg.ContainsAuth() {
|
if !dcfg.ContainsAuth() {
|
||||||
if c.GlobalString("registry") != "" {
|
// If we were passed a registry, just use that.
|
||||||
|
if registry != "" {
|
||||||
return types.AuthConfig{
|
return types.AuthConfig{
|
||||||
ServerAddress: c.GlobalString("registry"),
|
ServerAddress: registry,
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
return types.AuthConfig{}, fmt.Errorf("No auth was present in %s, please pass a registry, username, and password", config.Dir())
|
|
||||||
|
// Otherwise, just use an empty auth config.
|
||||||
|
return types.AuthConfig{}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// if they passed a specific registry, return those creds _if_ they exist
|
// if they passed a specific registry, return those creds _if_ they exist
|
||||||
if c.GlobalString("registry") != "" {
|
if registry != "" {
|
||||||
// try with the user input
|
// try with the user input
|
||||||
if creds, ok := dcfg.AuthConfigs[c.GlobalString("registry")]; ok {
|
if creds, ok := dcfg.AuthConfigs[registry]; ok {
|
||||||
return creds, nil
|
return creds, nil
|
||||||
}
|
}
|
||||||
// add https:// to user input and try again
|
// add https:// to user input and try again
|
||||||
// see https://github.com/jessfraz/reg/issues/32
|
// see https://github.com/jessfraz/reg/issues/32
|
||||||
if !strings.HasPrefix(c.GlobalString("registry"), "https://") && !strings.HasPrefix(c.GlobalString("registry"), "http://") {
|
if !strings.HasPrefix(registry, "https://") && !strings.HasPrefix(registry, "http://") {
|
||||||
if creds, ok := dcfg.AuthConfigs["https://"+c.GlobalString("registry")]; ok {
|
if creds, ok := dcfg.AuthConfigs["https://"+registry]; ok {
|
||||||
return creds, nil
|
return creds, nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Otherwise just use the registry with no auth.
|
||||||
return types.AuthConfig{
|
return types.AuthConfig{
|
||||||
ServerAddress: c.GlobalString("registry"),
|
ServerAddress: registry,
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// set the auth config as the registryURL, username and Password
|
// Just set the auth config as the first registryURL, username and password
|
||||||
|
// found in the auth config.
|
||||||
for _, creds := range dcfg.AuthConfigs {
|
for _, creds := range dcfg.AuthConfigs {
|
||||||
return creds, nil
|
return creds, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
return types.AuthConfig{}, fmt.Errorf("Could not find any authentication credentials")
|
// Don't use any authentication.
|
||||||
|
return types.AuthConfig{}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetRepoAndRef parses the repo name and reference.
|
// GetRepoAndRef parses the repo name and reference.
|
||||||
func GetRepoAndRef(c *cli.Context) (repo, ref string, err error) {
|
func GetRepoAndRef(arg string) (repo, ref string, err error) {
|
||||||
if len(c.Args()) < 1 {
|
if arg == "" {
|
||||||
return "", "", errors.New("pass the name of the repository")
|
return "", "", errors.New("pass the name of the repository")
|
||||||
}
|
}
|
||||||
|
|
||||||
arg := c.Args()[0]
|
|
||||||
var parts []string
|
var parts []string
|
||||||
if strings.Contains(arg, "@") {
|
if strings.Contains(arg, "@") {
|
||||||
parts = strings.Split(c.Args()[0], "@")
|
parts = strings.Split(arg, "@")
|
||||||
} else if strings.Contains(arg, ":") {
|
} else if strings.Contains(arg, ":") {
|
||||||
parts = strings.Split(c.Args()[0], ":")
|
parts = strings.Split(arg, ":")
|
||||||
} else {
|
} else {
|
||||||
parts = []string{arg}
|
parts = []string{arg}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue