Implement reusable GetTagMatchingVersionOrConstraint

Largely borrowed from (IndexFile).Get. However there is not currently a nice
way to make this code also usable to the repo package, as IndexFile depends on
a list of index Entries containing a nexted version.

We could refactor this later to somehow use the same shared function, but for
now keeping separate.

Signed-off-by: Scott Rigby <scott@r6by.com>
pull/10527/head
Scott Rigby 3 years ago
parent 4f62d3dc1b
commit b6bf3905f3
No known key found for this signature in database
GPG Key ID: C7C6FBB5B91C1155

@ -23,6 +23,8 @@ import (
"io"
"strings"
"github.com/Masterminds/semver"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
orascontext "oras.land/oras-go/pkg/context"
"oras.land/oras-go/pkg/registry"
@ -46,6 +48,35 @@ func ContainsTag(tags []string, tag string) bool {
return false
}
func GetTagMatchingVersionOrConstraint(tags []string, versionString string) (string, error) {
var constraint *semver.Constraints
if versionString == "" {
// If string is empty, set wildcard constraint
constraint, _ = semver.NewConstraint("*")
} else {
// Otherwise set constraint to the string given
var err error
constraint, err = semver.NewConstraint(versionString)
if err != nil {
return "", err
}
}
// Otherwise try to find the first available version matching the string,
// in case it is a constraint
for _, v := range tags {
test, err := semver.NewVersion(v)
if err != nil {
continue
}
if constraint.Check(test) {
return v, nil
}
}
return "", errors.Errorf("Could not locate a version matching provided version string %s", versionString)
}
// extractChartMeta is used to extract a chart metadata from a byte array
func extractChartMeta(chartData []byte) (*chart.Metadata, error) {
ch, err := loader.LoadArchive(bytes.NewReader(chartData))

@ -30,7 +30,7 @@ type OCIGetter struct {
opts options
}
//Get performs a Get from repo.Getter and returns the body.
// Get performs a Get from repo.Getter and returns the body.
func (g *OCIGetter) Get(href string, options ...Option) (*bytes.Buffer, error) {
for _, opt := range options {
opt(&g.opts)
@ -57,23 +57,22 @@ func (g *OCIGetter) get(href string) (*bytes.Buffer, error) {
if err != nil {
return nil, err
}
if len(tags) == 0 {
return nil, errors.Errorf("Unable to locate any tags in provided repository: %s", ref)
}
//Determine if version provided. If not
// Determine if version provided
// If empty, try to get the highest available tag
// If exact version, try to find it
// If semver constraint string, try to find a match
providedVersion := g.opts.version
if g.opts.version == "" {
if len(tags) > 0 {
providedVersion = tags[0]
} else {
return nil, errors.Errorf("Unable to locate any tags in provided repository: %s", ref)
}
} else {
if !registry.ContainsTag(tags, providedVersion) {
return nil, errors.Errorf("Could not located provided version %s in repository %s", providedVersion, ref)
}
tag, err := registry.GetTagMatchingVersionOrConstraint(tags, providedVersion)
if err != nil {
return nil, err
}
ref = fmt.Sprintf("%s:%s", ref, providedVersion)
ref = fmt.Sprintf("%s:%s", ref, tag)
result, err := client.Pull(ref, pullOpts...)
if err != nil {

Loading…
Cancel
Save