-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
<!-- markdownlint-disable MD041 --> #### What this PR does / why we need it Because the new OCI references now always contain the digest in addition to the tag, the artifactset creation is not able anymore to provide appropriate meta data. This is fixed by a new ArtVersion type able to handle the combination of digest and tag. #### Which issue(s) this PR fixes <!-- Usage: `Fixes #<issue number>`, or `Fixes (paste link of issue)`. --> --------- Co-authored-by: Jakob Möller <[email protected]>
- Loading branch information
1 parent
9e27f16
commit d6a5994
Showing
13 changed files
with
214 additions
and
77 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
package ociutils | ||
|
||
import ( | ||
"strings" | ||
|
||
"github.com/mandelsoft/goutils/generics" | ||
"github.com/opencontainers/go-digest" | ||
) | ||
|
||
// ParseVersion parses the version part of an OCI reference consisting | ||
// of an optional tag and/or digest. | ||
func ParseVersion(vers string) (*ArtVersion, error) { | ||
if strings.HasPrefix(vers, "@") { | ||
dig, err := digest.Parse(vers[1:]) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return &ArtVersion{ | ||
Digest: &dig, | ||
}, nil | ||
} | ||
|
||
i := strings.Index(vers, "@") | ||
if i > 0 { | ||
dig, err := digest.Parse(vers[i+1:]) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return &ArtVersion{ | ||
Tag: generics.Pointer(vers[:i]), | ||
Digest: &dig, | ||
}, nil | ||
} | ||
return &ArtVersion{ | ||
Tag: &vers, | ||
}, nil | ||
} | ||
|
||
// ArtVersion is the version part of an OCI reference consisting of an | ||
// optional tag and/or digest. Both parts may be nil, if a reference | ||
// does not include a version part. | ||
// Such objects are sub objects of (oci.)ArtSpec, which has be moved | ||
// to separate package to avoid package cycles. The methods are | ||
// derived from ArtSpec. | ||
type ArtVersion struct { | ||
// +optional | ||
Tag *string `json:"tag,omitempty"` | ||
// +optional | ||
Digest *digest.Digest `json:"digest,omitempty"` | ||
} | ||
|
||
func (v *ArtVersion) VersionSpec() string { | ||
if v != nil { | ||
return "" | ||
} | ||
|
||
vers := "" | ||
if v.Tag != nil { | ||
vers = *v.Tag | ||
} | ||
|
||
if v.Digest != nil { | ||
vers += "@" + string(*v.Digest) | ||
} | ||
if vers == "" { | ||
return "latest" | ||
} | ||
return vers | ||
} | ||
|
||
// IsVersion returns true, if the object ref is given | ||
// and describes a dedicated version, either by tag or digest. | ||
// As part of the ArtSpec type in oci, it might describe | ||
// no version part. THis method indicates, whether a version part | ||
// is present. | ||
func (v *ArtVersion) IsVersion() bool { | ||
if v == nil { | ||
return false | ||
} | ||
return v.Tag != nil || v.Digest != nil | ||
} | ||
|
||
func (v *ArtVersion) IsTagged() bool { | ||
return v != nil && v.Tag != nil | ||
} | ||
|
||
func (v *ArtVersion) IsDigested() bool { | ||
return v != nil && v.Digest != nil | ||
} | ||
|
||
func (v *ArtVersion) GetTag() string { | ||
if v != nil && | ||
v.Tag != nil { | ||
return *v.Tag | ||
} | ||
return "" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.