Merge pull request #2131 from dmcgowan/fix-reference-package

Fix reference package
This commit is contained in:
Aaron Lehmann 2017-01-09 16:06:30 -08:00 committed by GitHub
commit 129ad8ea0c
3 changed files with 31 additions and 29 deletions

View file

@ -5,7 +5,8 @@ import (
"fmt" "fmt"
"strings" "strings"
"github.com/docker/distribution/digest" "github.com/docker/distribution/digestset"
"github.com/opencontainers/go-digest"
) )
var ( var (
@ -80,7 +81,7 @@ func splitDockerDomain(name string) (domain, remainder string) {
// For example, "docker.io/library/redis" will have the familiar // For example, "docker.io/library/redis" will have the familiar
// name "redis" and "docker.io/dmcgowan/myapp" will be "dmcgowan/myapp". // name "redis" and "docker.io/dmcgowan/myapp" will be "dmcgowan/myapp".
// Returns a familiarized named only reference. // Returns a familiarized named only reference.
func familiarizeName(named NamedRepository) repository { func familiarizeName(named namedRepository) repository {
repo := repository{ repo := repository{
domain: named.Domain(), domain: named.Domain(),
path: named.Path(), path: named.Path(),
@ -95,7 +96,7 @@ func familiarizeName(named NamedRepository) repository {
func (r reference) Familiar() Named { func (r reference) Familiar() Named {
return reference{ return reference{
NamedRepository: familiarizeName(r.NamedRepository), namedRepository: familiarizeName(r.namedRepository),
tag: r.tag, tag: r.tag,
digest: r.digest, digest: r.digest,
} }
@ -107,14 +108,14 @@ func (r repository) Familiar() Named {
func (t taggedReference) Familiar() Named { func (t taggedReference) Familiar() Named {
return taggedReference{ return taggedReference{
NamedRepository: familiarizeName(t.NamedRepository), namedRepository: familiarizeName(t.namedRepository),
tag: t.tag, tag: t.tag,
} }
} }
func (c canonicalReference) Familiar() Named { func (c canonicalReference) Familiar() Named {
return canonicalReference{ return canonicalReference{
NamedRepository: familiarizeName(c.NamedRepository), namedRepository: familiarizeName(c.namedRepository),
digest: c.digest, digest: c.digest,
} }
} }
@ -142,7 +143,7 @@ func ParseAnyReference(ref string) (Reference, error) {
if ok := anchoredIdentifierRegexp.MatchString(ref); ok { if ok := anchoredIdentifierRegexp.MatchString(ref); ok {
return digestReference("sha256:" + ref), nil return digestReference("sha256:" + ref), nil
} }
if dgst, err := digest.ParseDigest(ref); err == nil { if dgst, err := digest.Parse(ref); err == nil {
return digestReference(dgst), nil return digestReference(dgst), nil
} }
@ -151,14 +152,14 @@ func ParseAnyReference(ref string) (Reference, error) {
// ParseAnyReferenceWithSet parses a reference string as a possible short // ParseAnyReferenceWithSet parses a reference string as a possible short
// identifier to be matched in a digest set, a full digest, or familiar name. // identifier to be matched in a digest set, a full digest, or familiar name.
func ParseAnyReferenceWithSet(ref string, ds *digest.Set) (Reference, error) { func ParseAnyReferenceWithSet(ref string, ds *digestset.Set) (Reference, error) {
if ok := anchoredShortIdentifierRegexp.MatchString(ref); ok { if ok := anchoredShortIdentifierRegexp.MatchString(ref); ok {
dgst, err := ds.Lookup(ref) dgst, err := ds.Lookup(ref)
if err == nil { if err == nil {
return digestReference(dgst), nil return digestReference(dgst), nil
} }
} else { } else {
if dgst, err := digest.ParseDigest(ref); err == nil { if dgst, err := digest.Parse(ref); err == nil {
return digestReference(dgst), nil return digestReference(dgst), nil
} }
} }

View file

@ -3,7 +3,8 @@ package reference
import ( import (
"testing" "testing"
"github.com/docker/distribution/digest" "github.com/docker/distribution/digestset"
"github.com/opencontainers/go-digest"
) )
func TestValidateReferenceName(t *testing.T) { func TestValidateReferenceName(t *testing.T) {
@ -410,7 +411,7 @@ func TestParseAnyReference(t *testing.T) {
if len(tcase.Digests) == 0 { if len(tcase.Digests) == 0 {
ref, err = ParseAnyReference(tcase.Reference) ref, err = ParseAnyReference(tcase.Reference)
} else { } else {
ds := digest.NewSet() ds := digestset.NewSet()
for _, dgst := range tcase.Digests { for _, dgst := range tcase.Digests {
if err := ds.Add(dgst); err != nil { if err := ds.Add(dgst); err != nil {
t.Fatalf("Error adding digest %s: %v", dgst.String(), err) t.Fatalf("Error adding digest %s: %v", dgst.String(), err)

View file

@ -135,9 +135,9 @@ type Canonical interface {
Digest() digest.Digest Digest() digest.Digest
} }
// NamedRepository is a reference to a repository with a name. // namedRepository is a reference to a repository with a name.
// A NamedRepository has both domain and path components. // A namedRepository has both domain and path components.
type NamedRepository interface { type namedRepository interface {
Named Named
Domain() string Domain() string
Path() string Path() string
@ -145,7 +145,7 @@ type NamedRepository interface {
// Domain returns the domain part of the Named reference // Domain returns the domain part of the Named reference
func Domain(named Named) string { func Domain(named Named) string {
if r, ok := named.(NamedRepository); ok { if r, ok := named.(namedRepository); ok {
return r.Domain() return r.Domain()
} }
domain, _ := splitDomain(named.Name()) domain, _ := splitDomain(named.Name())
@ -154,7 +154,7 @@ func Domain(named Named) string {
// Path returns the name without the domain part of the Named reference // Path returns the name without the domain part of the Named reference
func Path(named Named) (name string) { func Path(named Named) (name string) {
if r, ok := named.(NamedRepository); ok { if r, ok := named.(namedRepository); ok {
return r.Path() return r.Path()
} }
_, path := splitDomain(named.Name()) _, path := splitDomain(named.Name())
@ -175,7 +175,7 @@ func splitDomain(name string) (string, string) {
// is returned as name // is returned as name
// DEPRECATED: Use Domain or Path // DEPRECATED: Use Domain or Path
func SplitHostname(named Named) (string, string) { func SplitHostname(named Named) (string, string) {
if r, ok := named.(NamedRepository); ok { if r, ok := named.(namedRepository); ok {
return r.Domain(), r.Path() return r.Domain(), r.Path()
} }
return splitDomain(named.Name()) return splitDomain(named.Name())
@ -212,7 +212,7 @@ func Parse(s string) (Reference, error) {
} }
ref := reference{ ref := reference{
NamedRepository: repo, namedRepository: repo,
tag: matches[2], tag: matches[2],
} }
if matches[3] != "" { if matches[3] != "" {
@ -272,7 +272,7 @@ func WithTag(name Named, tag string) (NamedTagged, error) {
return nil, ErrTagInvalidFormat return nil, ErrTagInvalidFormat
} }
var repo repository var repo repository
if r, ok := name.(NamedRepository); ok { if r, ok := name.(namedRepository); ok {
repo.domain = r.Domain() repo.domain = r.Domain()
repo.path = r.Path() repo.path = r.Path()
} else { } else {
@ -280,13 +280,13 @@ func WithTag(name Named, tag string) (NamedTagged, error) {
} }
if canonical, ok := name.(Canonical); ok { if canonical, ok := name.(Canonical); ok {
return reference{ return reference{
NamedRepository: repo, namedRepository: repo,
tag: tag, tag: tag,
digest: canonical.Digest(), digest: canonical.Digest(),
}, nil }, nil
} }
return taggedReference{ return taggedReference{
NamedRepository: repo, namedRepository: repo,
tag: tag, tag: tag,
}, nil }, nil
} }
@ -298,7 +298,7 @@ func WithDigest(name Named, digest digest.Digest) (Canonical, error) {
return nil, ErrDigestInvalidFormat return nil, ErrDigestInvalidFormat
} }
var repo repository var repo repository
if r, ok := name.(NamedRepository); ok { if r, ok := name.(namedRepository); ok {
repo.domain = r.Domain() repo.domain = r.Domain()
repo.path = r.Path() repo.path = r.Path()
} else { } else {
@ -306,13 +306,13 @@ func WithDigest(name Named, digest digest.Digest) (Canonical, error) {
} }
if tagged, ok := name.(Tagged); ok { if tagged, ok := name.(Tagged); ok {
return reference{ return reference{
NamedRepository: repo, namedRepository: repo,
tag: tagged.Tag(), tag: tagged.Tag(),
digest: digest, digest: digest,
}, nil }, nil
} }
return canonicalReference{ return canonicalReference{
NamedRepository: repo, namedRepository: repo,
digest: digest, digest: digest,
}, nil }, nil
} }
@ -347,15 +347,15 @@ func getBestReferenceType(ref reference) Reference {
if ref.tag == "" { if ref.tag == "" {
if ref.digest != "" { if ref.digest != "" {
return canonicalReference{ return canonicalReference{
NamedRepository: ref.NamedRepository, namedRepository: ref.namedRepository,
digest: ref.digest, digest: ref.digest,
} }
} }
return ref.NamedRepository return ref.namedRepository
} }
if ref.digest == "" { if ref.digest == "" {
return taggedReference{ return taggedReference{
NamedRepository: ref.NamedRepository, namedRepository: ref.namedRepository,
tag: ref.tag, tag: ref.tag,
} }
} }
@ -364,7 +364,7 @@ func getBestReferenceType(ref reference) Reference {
} }
type reference struct { type reference struct {
NamedRepository namedRepository
tag string tag string
digest digest.Digest digest digest.Digest
} }
@ -416,7 +416,7 @@ func (d digestReference) Digest() digest.Digest {
} }
type taggedReference struct { type taggedReference struct {
NamedRepository namedRepository
tag string tag string
} }
@ -429,7 +429,7 @@ func (t taggedReference) Tag() string {
} }
type canonicalReference struct { type canonicalReference struct {
NamedRepository namedRepository
digest digest.Digest digest digest.Digest
} }