github.com/kobeld/docker@v1.12.0-rc1/registry/reference.go (about)

     1  package registry
     2  
     3  import (
     4  	"strings"
     5  
     6  	"github.com/docker/distribution/digest"
     7  )
     8  
     9  // Reference represents a tag or digest within a repository
    10  type Reference interface {
    11  	// HasDigest returns whether the reference has a verifiable
    12  	// content addressable reference which may be considered secure.
    13  	HasDigest() bool
    14  
    15  	// ImageName returns an image name for the given repository
    16  	ImageName(string) string
    17  
    18  	// Returns a string representation of the reference
    19  	String() string
    20  }
    21  
    22  type tagReference struct {
    23  	tag string
    24  }
    25  
    26  func (tr tagReference) HasDigest() bool {
    27  	return false
    28  }
    29  
    30  func (tr tagReference) ImageName(repo string) string {
    31  	return repo + ":" + tr.tag
    32  }
    33  
    34  func (tr tagReference) String() string {
    35  	return tr.tag
    36  }
    37  
    38  type digestReference struct {
    39  	digest digest.Digest
    40  }
    41  
    42  func (dr digestReference) HasDigest() bool {
    43  	return true
    44  }
    45  
    46  func (dr digestReference) ImageName(repo string) string {
    47  	return repo + "@" + dr.String()
    48  }
    49  
    50  func (dr digestReference) String() string {
    51  	return dr.digest.String()
    52  }
    53  
    54  // ParseReference parses a reference into either a digest or tag reference
    55  func ParseReference(ref string) Reference {
    56  	if strings.Contains(ref, ":") {
    57  		dgst, err := digest.ParseDigest(ref)
    58  		if err == nil {
    59  			return digestReference{digest: dgst}
    60  		}
    61  	}
    62  	return tagReference{tag: ref}
    63  }
    64  
    65  // DigestReference creates a digest reference using a digest
    66  func DigestReference(dgst digest.Digest) Reference {
    67  	return digestReference{digest: dgst}
    68  }