version.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. package version
  2. import (
  3. "strconv"
  4. "strings"
  5. )
  6. // Version is the service current released version.
  7. // Semantic versioning: https://semver.org/
  8. var Version = "0.9.1"
  9. // DevVersion is the service current development version.
  10. var DevVersion = "0.9.1"
  11. func GetCurrentVersion(mode string) string {
  12. if mode == "dev" {
  13. return DevVersion
  14. }
  15. return Version
  16. }
  17. func GetMinorVersion(version string) string {
  18. versionList := strings.Split(version, ".")
  19. if len(versionList) < 3 {
  20. return ""
  21. }
  22. return versionList[0] + "." + versionList[1]
  23. }
  24. func GetSchemaVersion(version string) string {
  25. minorVersion := GetMinorVersion(version)
  26. return minorVersion + ".0"
  27. }
  28. // convSemanticVersionToInt converts version string to int.
  29. func convSemanticVersionToInt(version string) int {
  30. versionList := strings.Split(version, ".")
  31. if len(versionList) < 3 {
  32. return 0
  33. }
  34. major, err := strconv.Atoi(versionList[0])
  35. if err != nil {
  36. return 0
  37. }
  38. minor, err := strconv.Atoi(versionList[1])
  39. if err != nil {
  40. return 0
  41. }
  42. patch, err := strconv.Atoi(versionList[2])
  43. if err != nil {
  44. return 0
  45. }
  46. return major*10000 + minor*100 + patch
  47. }
  48. // IsVersionGreaterThanOrEqualTo returns true if version is greater than or equal to target.
  49. func IsVersionGreaterOrEqualThan(version, target string) bool {
  50. return convSemanticVersionToInt(version) >= convSemanticVersionToInt(target)
  51. }
  52. // IsVersionGreaterThan returns true if version is greater than target.
  53. func IsVersionGreaterThan(version, target string) bool {
  54. return convSemanticVersionToInt(version) > convSemanticVersionToInt(target)
  55. }