forked from TrueCloudLab/distribution
ddfd2335c7
There seems to be a need for a type that represents a way of pointing to an image, irrespective of the implementation. This patch defines a Reference interface and provides 3 implementations: - TagReference: when only a tag is provided - DigestReference: when a digest (according to the digest package) is provided, can include optional tag as well Validation of references are purely syntactic. There is also a strong type for tags, analogous to digests, as well as a strong type for Repository from which clients can access the hostname alone, or the repository name without the hostname, or both together via the String() method. For Repository, the files names.go and names_test.go were moved from the v2 package. Update regexp to support repeated dash and double underscore Add field type for serialization Since reference itself may be represented by multiple types which implement the reference inteface, serialization can lead to ambiguous type which cannot be deserialized. Field wraps the reference object to ensure that the correct type is always deserialized, requiring an extra unwrap of the reference after deserialization. Signed-off-by: Tibor Vass <tibor@docker.com> Signed-off-by: Derek McGowan <derek@mcgstyle.net> (github: dmcgowan)
42 lines
2.3 KiB
Go
42 lines
2.3 KiB
Go
package reference
|
|
|
|
import "regexp"
|
|
|
|
var (
|
|
// nameSubComponentRegexp defines the part of the name which must be
|
|
// begin and end with an alphanumeric character. These characters can
|
|
// be separated by any number of dashes.
|
|
nameSubComponentRegexp = regexp.MustCompile(`[a-z0-9]+(?:[-]+[a-z0-9]+)*`)
|
|
|
|
// nameComponentRegexp restricts registry path component names to
|
|
// start with at least one letter or number, with following parts able to
|
|
// be separated by one period, underscore or double underscore.
|
|
nameComponentRegexp = regexp.MustCompile(nameSubComponentRegexp.String() + `(?:(?:[._]|__)` + nameSubComponentRegexp.String() + `)*`)
|
|
|
|
nameRegexp = regexp.MustCompile(`(?:` + nameComponentRegexp.String() + `/)*` + nameComponentRegexp.String())
|
|
|
|
hostnameComponentRegexp = regexp.MustCompile(`(?:[a-z0-9]|[a-z0-9][a-z0-9-]*[a-z0-9])`)
|
|
|
|
// hostnameComponentRegexp restricts the registry hostname component of a repository name to
|
|
// start with a component as defined by hostnameRegexp and followed by an optional port.
|
|
hostnameRegexp = regexp.MustCompile(`(?:` + hostnameComponentRegexp.String() + `\.)*` + hostnameComponentRegexp.String() + `(?::[0-9]+)?`)
|
|
|
|
// TagRegexp matches valid tag names. From docker/docker:graph/tags.go.
|
|
TagRegexp = regexp.MustCompile(`[\w][\w.-]{0,127}`)
|
|
|
|
// anchoredTagRegexp matches valid tag names, anchored at the start and
|
|
// end of the matched string.
|
|
anchoredTagRegexp = regexp.MustCompile(`^` + TagRegexp.String() + `$`)
|
|
|
|
// NameRegexp is the format for the name component of references. The
|
|
// regexp has capturing groups for the hostname and name part omitting
|
|
// the seperating forward slash from either.
|
|
NameRegexp = regexp.MustCompile(`(?:` + hostnameRegexp.String() + `/)?` + nameRegexp.String())
|
|
|
|
// ReferenceRegexp is the full supported format of a reference. The
|
|
// regexp has capturing groups for name, tag, and digest components.
|
|
ReferenceRegexp = regexp.MustCompile(`^((?:` + hostnameRegexp.String() + `/)?` + nameRegexp.String() + `)(?:[:](` + TagRegexp.String() + `))?(?:[@]([A-Za-z][A-Za-z0-9]*(?:[-_+.][A-Za-z][A-Za-z0-9]*)*[:][[:xdigit:]]{32,}))?$`)
|
|
|
|
// anchoredNameRegexp is used to parse a name value, capturing hostname
|
|
anchoredNameRegexp = regexp.MustCompile(`^(?:(` + hostnameRegexp.String() + `)/)?(` + nameRegexp.String() + `)$`)
|
|
)
|