1
0
mirror of https://github.com/rancher/os.git synced 2025-09-25 20:52:58 +00:00

hashicorp version pkg uses alpha sort on -rc11 metadata - so switching to Josh's catalog pkg

Signed-off-by: Sven Dowideit <SvenDowideit@home.org.au>
This commit is contained in:
Sven Dowideit
2017-02-10 00:37:08 +00:00
parent 3929481b5b
commit 9a75d2d5b4
29 changed files with 523 additions and 976 deletions

View File

@@ -0,0 +1,84 @@
package version
import (
"regexp"
"strconv"
"strings"
)
var (
numberRe = regexp.MustCompile("[0-9]+")
wordRe = regexp.MustCompile("[a-z]+")
)
func GreaterThan(a, b string) bool {
a = stripMetadata(a)
b = stripMetadata(b)
a = strings.TrimLeft(a, "v")
b = strings.TrimLeft(b, "v")
aSplit := periodDashSplit(a)
bSplit := periodDashSplit(b)
if len(bSplit) > len(aSplit) {
return !GreaterThan(b, a) && a != b
}
for i := 0; i < len(aSplit); i++ {
if i == len(bSplit) {
if _, err := strconv.Atoi(aSplit[i]); err == nil {
return true
}
return false
}
aWord := wordRe.FindString(aSplit[i])
bWord := wordRe.FindString(bSplit[i])
if aWord != "" && bWord != "" {
if strings.Compare(aWord, bWord) > 0 {
return true
}
if strings.Compare(bWord, aWord) > 0 {
return false
}
}
aMatch := numberRe.FindString(aSplit[i])
bMatch := numberRe.FindString(bSplit[i])
if aMatch == "" || bMatch == "" {
if strings.Compare(aSplit[i], bSplit[i]) > 0 {
return true
}
if strings.Compare(bSplit[i], aSplit[i]) > 0 {
return false
}
}
aNum, _ := strconv.Atoi(aMatch)
bNum, _ := strconv.Atoi(bMatch)
if aNum > bNum {
return true
}
if bNum > aNum {
return false
}
}
return false
}
func stripMetadata(v string) string {
split := strings.Split(v, "+")
if len(split) > 1 {
return split[0]
}
return v
}
func periodDashSplit(s string) []string {
return strings.FieldsFunc(s, func(r rune) bool {
switch r {
case '.', '-':
return true
}
return false
})
}