Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support more types of image names #119

Merged
merged 12 commits into from
Oct 8, 2020
31 changes: 19 additions & 12 deletions core/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,27 +240,34 @@ func buildFindLocalImageOptions(image string) docker.ListImagesOptions {
}

func buildPullOptions(image string) (docker.PullImageOptions, docker.AuthConfiguration) {
tag := "latest"
registry := ""
repository, tag := docker.ParseRepositoryTag(image)

parts := strings.Split(image, ":")
if len(parts) == 2 {
tag = parts[1]
}
registry := parseRegistry(repository)

name := parts[0]
parts = strings.Split(name, "/")
if len(parts) > 2 {
registry = parts[0]
if tag == "" {
tag = "latest"
}

return docker.PullImageOptions{
Repository: name,
Repository: repository,
Registry: registry,
Tag: tag,
Tag: tag,
}, buildAuthConfiguration(registry)
}

func parseRegistry(repository string) string {
parts := strings.Split(repository, "/")
if len(parts) < 2 {
return ""
}

if strings.ContainsAny(parts[0], ".:") || len(parts) > 2 {
return parts[0]
}

return ""
}

func buildAuthConfiguration(registry string) docker.AuthConfiguration {
var auth docker.AuthConfiguration
if dockercfg == nil {
Expand Down
7 changes: 7 additions & 0 deletions core/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -316,3 +316,10 @@ func (*TestLogger) Debugf(format string, args ...interface{}) {}
func (*TestLogger) Errorf(format string, args ...interface{}) {}
func (*TestLogger) Noticef(format string, args ...interface{}) {}
func (*TestLogger) Warningf(format string, args ...interface{}) {}

func (s *SuiteCommon) TestParseRegistry(c *C) {
c.Assert(parseRegistry("example.com:port/dir/image"), Equals, "example.com:port")
c.Assert(parseRegistry("example.com:port/image"), Equals, "example.com:port")
c.Assert(parseRegistry("dir/image"), Equals, "")
c.Assert(parseRegistry("image"), Equals, "")
}