forked from TrueCloudLab/distribution
f8c09b6a7d
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. Signed-off-by: Tibor Vass <tibor@docker.com>
38 lines
983 B
Go
38 lines
983 B
Go
package reference
|
|
|
|
import (
|
|
"fmt"
|
|
"regexp"
|
|
)
|
|
|
|
var (
|
|
// TagRegexp matches valid tag names. From docker/docker:graph/tags.go.
|
|
TagRegexp = regexp.MustCompile(`[\w][\w.-]{0,127}`)
|
|
|
|
// TagAnchoredRegexp matches valid tag names, anchored at the start and
|
|
// end of the matched string.
|
|
TagAnchoredRegexp = regexp.MustCompile(`^` + TagRegexp.String() + `$`)
|
|
|
|
// ErrTagInvalid is returned when a tag does not match TagAnchoredRegexp.
|
|
ErrTagInvalid = fmt.Errorf("tag name must match %q", TagRegexp.String())
|
|
)
|
|
|
|
// Tag represents an image's tag name.
|
|
type Tag string
|
|
|
|
// NewTag returns a valid Tag from an input string s.
|
|
// If the validation fails, an error is returned.
|
|
func NewTag(s string) (Tag, error) {
|
|
tag := Tag(s)
|
|
return tag, tag.Validate()
|
|
}
|
|
|
|
// Validate returns ErrTagInvalid if tag does not match TagAnchoredRegexp.
|
|
//
|
|
// tag := [\w][\w.-]{0,127}
|
|
func (tag Tag) Validate() error {
|
|
if !TagAnchoredRegexp.MatchString(string(tag)) {
|
|
return ErrTagInvalid
|
|
}
|
|
return nil
|
|
}
|