version.go 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. package version
  2. import (
  3. "fmt"
  4. "strings"
  5. "golang.org/x/mod/semver"
  6. )
  7. // Version is the service current released version.
  8. // Semantic versioning: https://semver.org/
  9. var Version = "0.15.1"
  10. // DevVersion is the service current development version.
  11. var DevVersion = "0.15.1"
  12. func GetCurrentVersion(mode string) string {
  13. if mode == "dev" || mode == "demo" {
  14. return DevVersion
  15. }
  16. return Version
  17. }
  18. func GetMinorVersion(version string) string {
  19. versionList := strings.Split(version, ".")
  20. if len(versionList) < 3 {
  21. return ""
  22. }
  23. return versionList[0] + "." + versionList[1]
  24. }
  25. func GetSchemaVersion(version string) string {
  26. minorVersion := GetMinorVersion(version)
  27. return minorVersion + ".0"
  28. }
  29. // IsVersionGreaterOrEqualThan returns true if version is greater than or equal to target.
  30. func IsVersionGreaterOrEqualThan(version, target string) bool {
  31. return semver.Compare(fmt.Sprintf("v%s", version), fmt.Sprintf("v%s", target)) > -1
  32. }
  33. // IsVersionGreaterThan returns true if version is greater than target.
  34. func IsVersionGreaterThan(version, target string) bool {
  35. return semver.Compare(fmt.Sprintf("v%s", version), fmt.Sprintf("v%s", target)) > 0
  36. }
  37. type SortVersion []string
  38. func (s SortVersion) Len() int {
  39. return len(s)
  40. }
  41. func (s SortVersion) Swap(i, j int) {
  42. s[i], s[j] = s[j], s[i]
  43. }
  44. func (s SortVersion) Less(i, j int) bool {
  45. v1 := fmt.Sprintf("v%s", s[i])
  46. v2 := fmt.Sprintf("v%s", s[j])
  47. return semver.Compare(v1, v2) == -1
  48. }