add fix for missing locator

Signed-off-by: Josh Dolitsky <jdolitsky@gmail.com>
pull/5279/head
Josh Dolitsky 7 years ago
parent 24eeca741e
commit f34a876dd4

@ -56,39 +56,48 @@ func ParseReference(s string) (*Reference, error) {
return &ref, nil return &ref, nil
} }
// fix modifies references that were potentially not parsed properly // fix modifies and augments a ref that may not have been parsed properly
func (spec *Reference) fix() { func (ref *Reference) fix() {
spec.fixNoTag() ref.fixNoTag()
ref.fixNoLocator()
} }
// fixNoTag is a fix for ref strings such as "mychart:1.0.0", which result in missing tag (object) // fixNoTag is a fix for ref strings such as "mychart:1.0.0", which result in missing tag
func (spec *Reference) fixNoTag() { func (ref *Reference) fixNoTag() {
if spec.Object == "" { if ref.Object == "" {
parts := strings.Split(spec.Locator, ":") parts := strings.Split(ref.Locator, ":")
numParts := len(parts) numParts := len(parts)
if 0 < numParts { if 0 < numParts {
lastIndex := numParts - 1 lastIndex := numParts - 1
lastPart := parts[lastIndex] lastPart := parts[lastIndex]
if !strings.Contains(lastPart, "/") { if !strings.Contains(lastPart, "/") {
spec.Locator = strings.Join(parts[:lastIndex], ":") ref.Locator = strings.Join(parts[:lastIndex], ":")
spec.Object = lastPart ref.Object = lastPart
} }
} }
} }
} }
// fixNoLocator is a fix for ref strings such as "mychart", which have the locator swapped with tag
func (ref *Reference) fixNoLocator() {
if ref.Locator == "" {
ref.Locator = ref.Object
ref.Object = ""
}
}
// validate makes sure the ref meets our criteria // validate makes sure the ref meets our criteria
func (spec *Reference) validate() error { func (ref *Reference) validate() error {
return spec.validateColons() return ref.validateColons()
} }
// validateColons verifies the ref only contains one colon max // validateColons verifies the ref only contains one colon max
// (or two, there might be a port number specified i.e. :5000) // (or two, there might be a port number specified i.e. :5000)
func (spec *Reference) validateColons() error { func (ref *Reference) validateColons() error {
if strings.Contains(spec.Object, ":") { if strings.Contains(ref.Object, ":") {
return tooManyColonsError return tooManyColonsError
} }
locParts := strings.Split(spec.Locator, ":") locParts := strings.Split(ref.Locator, ":")
locLastIndex := len(locParts) - 1 locLastIndex := len(locParts) - 1
if 1 < locLastIndex { if 1 < locLastIndex {
return tooManyColonsError return tooManyColonsError

@ -39,12 +39,24 @@ func TestReference(t *testing.T) {
is.Error(err, "ref contains too many colons (3)") is.Error(err, "ref contains too many colons (3)")
// good refs // good refs
s = "mychart:1.5.0" s = "mychart"
ref, err := ParseReference(s) ref, err := ParseReference(s)
is.NoError(err) is.NoError(err)
is.Equal("mychart", ref.Locator) is.Equal("mychart", ref.Locator)
is.Equal("", ref.Object)
s = "mychart:1.5.0"
ref, err = ParseReference(s)
is.NoError(err)
is.Equal("mychart", ref.Locator)
is.Equal("1.5.0", ref.Object) is.Equal("1.5.0", ref.Object)
s = "myrepo/mychart"
ref, err = ParseReference(s)
is.NoError(err)
is.Equal("myrepo/mychart", ref.Locator)
is.Equal("", ref.Object)
s = "myrepo/mychart:1.5.0" s = "myrepo/mychart:1.5.0"
ref, err = ParseReference(s) ref, err = ParseReference(s)
is.NoError(err) is.NoError(err)

Loading…
Cancel
Save