NewImageReference returns an ImageReference instance based on the given url.
(url string, opts ...name.Option)
| 119 | |
| 120 | // NewImageReference returns an ImageReference instance based on the given url. |
| 121 | func NewImageReference(url string, opts ...name.Option) (*ImageReference, error) { |
| 122 | ref, err := name.ParseReference(url, opts...) |
| 123 | if err != nil { |
| 124 | return nil, err |
| 125 | } |
| 126 | imageRef := &ImageReference{ref: ref} |
| 127 | |
| 128 | // An image reference may contain both a tag and a digest. However, the parsing library |
| 129 | // will drop the tag in favor of the digest. Since we care about the value of the tag, |
| 130 | // parse the url without the digest reference to force the library to retain the tag |
| 131 | // value in all cases where it is present. |
| 132 | tagRef, err := name.NewTag(strings.Split(url, "@")[0], opts...) |
| 133 | if err == nil { |
| 134 | imageRef.Tag = tagRef.TagStr() |
| 135 | imageRef.Repository = tagRef.Context().Name() |
| 136 | } |
| 137 | |
| 138 | digestRef, err := name.NewDigest(url, opts...) |
| 139 | if err == nil { |
| 140 | imageRef.Digest = digestRef.DigestStr() |
| 141 | imageRef.Repository = digestRef.Context().Name() |
| 142 | } |
| 143 | |
| 144 | return imageRef, nil |
| 145 | } |
| 146 | |
| 147 | func (i ImageReference) Ref() name.Reference { |
| 148 | return i.ref |