feat!: add metadata to Tag object and update Parse(), Equals() and String() #7

Merged
decentral1se merged 2 commits from knoflook/tagcmp:main into main 2021-10-07 13:03:38 +00:00

View File

@ -14,8 +14,9 @@ type Tag struct {
MissingMinor bool // whether or not the minor semver part was left out
Patch string `json:",omitempty"` // patch semver part
MissingPatch bool // whether or not he patch semver part was left out
Suffix string // tag suffix (e.g. "-alpine")
Suffix string // tag suffix (e.g. "-alpine") [would be release candidate in semver]
UsesV bool // whether or not the tag uses the "v" prefix
Metadata string // metadata: what's after + and after the first "-"
}
// ByTagAsc sorts tags in ascending order where the last element is the latest tag.
@ -96,6 +97,10 @@ func (t Tag) Equals(tag Tag) bool {
return false
}
if t.Metadata != tag.Metadata {
return false
}
// ignore errors since Parse already handled
mj1, _ := strconv.Atoi(t.Major)
mj2, _ := strconv.Atoi(tag.Major)
@ -136,6 +141,10 @@ func (t Tag) String() string {
repr += fmt.Sprintf("-%s", t.Suffix)
}
if t.Metadata != "" {
repr += fmt.Sprintf("+%s", t.Metadata)
}
return repr
}
@ -280,6 +289,7 @@ func patternMatches(tag string) error {
// patternCounts determines if tags match unsupported patterns by counting occurences of matches
func patternCounts(tag string) error {
v := regexp.MustCompile(DotPattern)
tag = strings.Split(tag, "+")[0]
if m := v.FindAllStringIndex(tag, -1); len(m) > 1 {
return fmt.Errorf("'%s' is not supported (%s)", tag, DotPattern)
}
@ -317,8 +327,15 @@ func Parse(tag string) (Tag, error) {
usesV = true
}
var metadata string
splits := strings.Split(tag, "+")
if len(splits) > 1 {
tag = splits[0]
metadata = splits[1]
}
var suffix string
splits := strings.SplitN(tag, "-", 2)
splits = strings.SplitN(tag, "-", 2)
if len(splits) > 1 {
tag = splits[0]
suffix = splits[1]
@ -373,6 +390,7 @@ func Parse(tag string) (Tag, error) {
MissingPatch: missingPatch,
UsesV: usesV,
Suffix: suffix,
Metadata: metadata,
}
return parsedTag, nil