version.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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.23.0"
  10. // DevVersion is the service current development version.
  11. var DevVersion = "0.23.0"
  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. // IsVersionGreaterOrEqualThan returns true if version is greater than or equal to target.
  26. func IsVersionGreaterOrEqualThan(version, target string) bool {
  27. return semver.Compare(fmt.Sprintf("v%s", version), fmt.Sprintf("v%s", target)) > -1
  28. }
  29. // IsVersionGreaterThan returns true if version is greater than target.
  30. func IsVersionGreaterThan(version, target string) bool {
  31. return semver.Compare(fmt.Sprintf("v%s", version), fmt.Sprintf("v%s", target)) > 0
  32. }
  33. type SortVersion []string
  34. func (s SortVersion) Len() int {
  35. return len(s)
  36. }
  37. func (s SortVersion) Swap(i, j int) {
  38. s[i], s[j] = s[j], s[i]
  39. }
  40. func (s SortVersion) Less(i, j int) bool {
  41. v1 := fmt.Sprintf("v%s", s[i])
  42. v2 := fmt.Sprintf("v%s", s[j])
  43. return semver.Compare(v1, v2) == -1
  44. }