@ -26,6 +26,7 @@ import (
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
emptyRepoError = errors . New ( "parsed repo was empty" )
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
}
// setExtraFields adds the R p eo and Tag fields to a Reference
// setExtraFields adds the R ep o and Tag fields to a Reference
func ( ref * Reference ) setExtraFields ( ) {
ref . Tag = ref . Object
ref . Repo = ref . Locator
@ -92,22 +93,34 @@ func (ref *Reference) fixNoRepo() {
// validate makes sure the ref meets our criteria
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
// (or two, there might be a port number specified i.e. :5000)
func ( ref * Reference ) validateColons ( ) error {
// validate NumColon ensures the ref only contains a single colon character (:)
// (or potentially two, there might be a port number specified i.e. :5000)
func ( ref * Reference ) validate Num Colons( ) error {
if strings . Contains ( ref . Tag , ":" ) {
return tooManyColonsError
}
locParts := strings . Split ( ref . Repo , ":" )
locLastIndex := len ( locParts ) - 1
if 1 < locLastIndex {
p arts := strings . Split ( ref . Repo , ":" )
l astIndex := len ( p arts) - 1
if 1 < l astIndex {
return tooManyColonsError
}
if 0 < l ocL astIndex {
port := strings . Split ( locParts[ locL astIndex] , "/" ) [ 0 ]
if 0 < l astIndex {
port := strings . Split ( parts[ l astIndex] , "/" ) [ 0 ]
if ! isValidPort ( port ) {
return tooManyColonsError
}