small refector

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

@ -26,6 +26,7 @@ import (
var ( var (
validPortRegEx = regexp.MustCompile("^([1-9]\\d{0,3}|0|[1-5][0-9]{4}|6[0-4][0-9]{3}|65[0-4][0-9]{2}|655[0-2][0-9]|6553[0-5])$") // adapted from https://stackoverflow.com/a/12968117 validPortRegEx = regexp.MustCompile("^([1-9]\\d{0,3}|0|[1-5][0-9]{4}|6[0-4][0-9]{3}|65[0-4][0-9]{2}|655[0-2][0-9]|6553[0-5])$") // adapted from https://stackoverflow.com/a/12968117
emptyRepoError = errors.New("parsed repo was empty")
tooManyColonsError = errors.New("ref may only contain a single colon character (:) unless specifying a port number") tooManyColonsError = errors.New("ref may only contain a single colon character (:) unless specifying a port number")
) )
@ -58,7 +59,7 @@ func ParseReference(s string) (*Reference, error) {
return &ref, nil return &ref, nil
} }
// setExtraFields adds the Rpeo and Tag fields to a Reference // setExtraFields adds the Repo and Tag fields to a Reference
func (ref *Reference) setExtraFields() { func (ref *Reference) setExtraFields() {
ref.Tag = ref.Object ref.Tag = ref.Object
ref.Repo = ref.Locator ref.Repo = ref.Locator
@ -92,22 +93,34 @@ func (ref *Reference) fixNoRepo() {
// validate makes sure the ref meets our criteria // validate makes sure the ref meets our criteria
func (ref *Reference) validate() error { func (ref *Reference) validate() error {
return ref.validateColons() err := ref.validateRepo()
if err != nil {
return err
}
return ref.validateNumColons()
}
// validateRepo checks that the Repo field is non-empty
func (ref *Reference) validateRepo() error {
if ref.Repo == "" {
return emptyRepoError
}
return nil
} }
// validateColons verifies the ref only contains one colon max // validateNumColon ensures the ref only contains a single colon character (:)
// (or two, there might be a port number specified i.e. :5000) // (or potentially two, there might be a port number specified i.e. :5000)
func (ref *Reference) validateColons() error { func (ref *Reference) validateNumColons() error {
if strings.Contains(ref.Tag, ":") { if strings.Contains(ref.Tag, ":") {
return tooManyColonsError return tooManyColonsError
} }
locParts := strings.Split(ref.Repo, ":") parts := strings.Split(ref.Repo, ":")
locLastIndex := len(locParts) - 1 lastIndex := len(parts) - 1
if 1 < locLastIndex { if 1 < lastIndex {
return tooManyColonsError return tooManyColonsError
} }
if 0 < locLastIndex { if 0 < lastIndex {
port := strings.Split(locParts[locLastIndex], "/")[0] port := strings.Split(parts[lastIndex], "/")[0]
if !isValidPort(port) { if !isValidPort(port) {
return tooManyColonsError return tooManyColonsError
} }

Loading…
Cancel
Save