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

WIP: - Parse Host, Port, Image and Tag with regular expressions #64

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 27 additions & 56 deletions docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,67 +104,38 @@ func NewImage(qname, user, password string, insecureTLS, insecureRegistry bool)
}
registry := dockerHub
tag := "latest"
var nameParts, tagParts []string
var name, port string
state := stateInitial
start := 0
for i, c := range qname {
if c == ':' || c == '/' || c == '@' || i == len(qname)-1 {
if i == len(qname)-1 {
// ignore a separator, include the last symbol
i += 1
}
part := qname[start:i]
start = i + 1

switch state {
case stateInitial:
if part == "localhost" || strings.Contains(part, ".") || (string(qname[i]) == ":" && strings.Contains(qname, "/")) {
var name string

// it's registry, let's check what's next =port of image name
registry = part
if c == ':' {
state = statePort
} else {
state = stateName
}
} else {
// it's an image name, if separator is /
// next part is also part of the name
// othrewise it's an offcial image
if c == '/' {
// we got just a part of name, till next time
start = 0
state = stateName
} else {
state = stateTag
name = fmt.Sprintf("library/%s", part)
}
}
case stateTag:
tag = ""
tagParts = append(tagParts, part)
case statePort:
state = stateName
port = part
case stateName:
if c == ':' || c == '@' {
state = stateTag
}
nameParts = append(nameParts, part)
if !utils.ImageOnlyRegexp.MatchString(string(qname)) || strings.HasPrefix(qname, "localhost/") {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The type of qname already is string, why still need to convert it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ups, I thought it was a byte. There is no point in convert string into string XD

registry = utils.DomainRegexp.FindString(string(qname))
imageAndTag := utils.ImageRegexp.FindStringSubmatch(string(qname))
result := make(map[string]string)
for i, name := range utils.ImageRegexp.SubexpNames() {
if i != 0 {
result[name] = imageAndTag[i]
}
}
name = result["name"]
if len(result["tag"]) > 0 {
tag = result["tag"]
}
} else {
imageAndTag := utils.ImageNoRegistryRegexp.FindStringSubmatch(string(qname))
result := make(map[string]string)
for i, name := range utils.ImageRegexp.SubexpNames() {
if i != 0 {
result[name] = imageAndTag[i]
}
}
name = result["name"]
if !strings.Contains(name, "/") {
name = "library/" + name
}
if len(result["tag"]) > 0 {
tag = result["tag"]
}
}

if port != "" {
registry = fmt.Sprintf("%s:%s", registry, port)
}
if name == "" {
name = strings.Join(nameParts, "/")
}
if tag == "" {
tag = strings.Join(tagParts, ":")
}
if insecureRegistry {
registry = fmt.Sprintf("http://%s/v2", registry)
} else {
Expand Down
18 changes: 18 additions & 0 deletions docker/docker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,12 @@ func TestNewImage(t *testing.T) {
name: "skynetservices/skydns",
tag: "2.3",
},
"no_registry_no_tag": {
image: "skynetservices/skydns",
registry: "https://registry-1.docker.io/v2",
name: "skynetservices/skydns",
tag: "latest",
},
"no_registry_root": {
image: "postgres:9.5.1",
registry: "https://registry-1.docker.io/v2",
Expand All @@ -70,6 +76,18 @@ func TestNewImage(t *testing.T) {
name: "library/postgres",
tag: "sha256:f6a2b81d981ace74aeafb2ed2982d52984d82958bfe836b82cbe4bf1ba440999",
},
"digest_with_registry": {
image: "registry:8000/postgres@sha256:f6a2b81d981ace74aeafb2ed2982d52984d82958bfe836b82cbe4bf1ba440999",
registry: "https://registry:8000/v2",
name: "postgres",
tag: "sha256:f6a2b81d981ace74aeafb2ed2982d52984d82958bfe836b82cbe4bf1ba440999",
},
"digest_with_localhost_registry": {
image: "localhost/postgres@sha256:f6a2b81d981ace74aeafb2ed2982d52984d82958bfe836b82cbe4bf1ba440999",
registry: "https://localhost/v2",
name: "postgres",
tag: "sha256:f6a2b81d981ace74aeafb2ed2982d52984d82958bfe836b82cbe4bf1ba440999",
},
"localhost_no_tag": {
image: "localhost/nginx",
registry: "https://localhost/v2",
Expand Down
71 changes: 71 additions & 0 deletions utils/regex.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package utils

import "regexp"

var (
domainComponentRegexp = match(`[a-zA-Z0-9][a-zA-Z0-9-]*[a-zA-Z0-9]`)

DomainRegexp = expression(
domainComponentRegexp,
optional(repeated(literal(`.`), domainComponentRegexp)),
optional(literal(`:`), match(`[0-9]+`)))

ImageRegexp = match(`(?:[/])(?P<name>[a-zA-Z0-9/]*)[:@]?(?P<tag>.*)?`)

ImageNoRegistryRegexp = match(`(?P<name>[a-zA-Z0-9/]*)[:@]?(?P<tag>.*)?`)

ImageOnlyRegexp = match(`^([a-zA-Z0-9]*)([/@][^/].*$|[:][a-zA-Z0-9\.]*$)`)
)

// match compiles the string to a regular expression.
var match = regexp.MustCompile

// literal compiles s into a literal regular expression, escaping any regexp
// reserved characters.
func literal(s string) *regexp.Regexp {
re := match(regexp.QuoteMeta(s))

if _, complete := re.LiteralPrefix(); !complete {
panic("must be a literal")
}

return re
}

// expression defines a full expression, where each regular expression must
// follow the previous.
func expression(res ...*regexp.Regexp) *regexp.Regexp {
var s string
for _, re := range res {
s += re.String()
}

return match(s)
}

// optional wraps the expression in a non-capturing group and makes the
// production optional.
func optional(res ...*regexp.Regexp) *regexp.Regexp {
return match(group(expression(res...)).String() + `?`)
}

// repeated wraps the regexp in a non-capturing group to get one or more
// matches.
func repeated(res ...*regexp.Regexp) *regexp.Regexp {
return match(group(expression(res...)).String() + `+`)
}

// group wraps the regexp in a non-capturing group.
func group(res ...*regexp.Regexp) *regexp.Regexp {
return match(`(?:` + expression(res...).String() + `)`)
}

// capture wraps the expression in a capturing group.
func capture(res ...*regexp.Regexp) *regexp.Regexp {
return match(`(` + expression(res...).String() + `)`)
}

// anchored anchors the regular expression by adding start and end delimiters.
func anchored(res ...*regexp.Regexp) *regexp.Regexp {
return match(`^` + expression(res...).String() + `$`)
}