go-zero/tools/goctl/internal/version/version.go
Kevin Wan 7868bdf660
Chore/goctl version (#1962)
* chore: update version to v1.3.7

* docs: update migrate versions

* chore: remove debug prints

* chore: remove debug prints
2022-06-03 20:46:21 +08:00

56 lines
1.4 KiB
Go

package version
import (
"encoding/json"
"strings"
)
// BuildVersion is the version of goctl.
const BuildVersion = "1.3.7"
var tag = map[string]int{"pre-alpha": 0, "alpha": 1, "pre-bata": 2, "beta": 3, "released": 4, "": 5}
// GetGoctlVersion returns BuildVersion
func GetGoctlVersion() string {
return BuildVersion
}
// IsVersionGreaterThan compares whether the current goctl version
// is greater than the target version
func IsVersionGreaterThan(version, target string) bool {
versionNumber, versionTag := convertVersion(version)
targetVersionNumber, targetTag := convertVersion(target)
if versionNumber > targetVersionNumber {
return true
} else if versionNumber < targetVersionNumber {
return false
} else {
// unchecked case, in normal, the goctl version does not contain suffix in release.
return tag[versionTag] > tag[targetTag]
}
}
// version format: number[.number]*(-tag)
func convertVersion(version string) (versionNumber float64, tag string) {
splits := strings.Split(version, "-")
tag = strings.Join(splits[1:], "")
var flag bool
numberStr := strings.Map(func(r rune) rune {
if r >= '0' && r <= '9' {
return r
}
if r == '.' {
if flag {
return '_'
}
flag = true
return r
}
return '_'
}, splits[0])
numberStr = strings.Replace(numberStr, "_", "", -1)
versionNumber, _ = json.Number(numberStr).Float64()
return
}