github.com/zhuohuang-hust/src-cbuild@v0.0.0-20230105071821-c7aab3e7c840/api/types/reference/image_reference.go (about)

     1  package reference
     2  
     3  import (
     4  	distreference "github.com/docker/distribution/reference"
     5  )
     6  
     7  // Parse parses the given references and returns the repository and
     8  // tag (if present) from it. If there is an error during parsing, it will
     9  // return an error.
    10  func Parse(ref string) (string, string, error) {
    11  	distributionRef, err := distreference.ParseNamed(ref)
    12  	if err != nil {
    13  		return "", "", err
    14  	}
    15  
    16  	tag := GetTagFromNamedRef(distributionRef)
    17  	return distributionRef.Name(), tag, nil
    18  }
    19  
    20  // GetTagFromNamedRef returns a tag from the specified reference.
    21  // This function is necessary as long as the docker "server" api makes the distinction between repository
    22  // and tags.
    23  func GetTagFromNamedRef(ref distreference.Named) string {
    24  	var tag string
    25  	switch x := ref.(type) {
    26  	case distreference.Digested:
    27  		tag = x.Digest().String()
    28  	case distreference.NamedTagged:
    29  		tag = x.Tag()
    30  	default:
    31  		tag = "latest"
    32  	}
    33  	return tag
    34  }