1
0

semverutil.go 777 B

123456789101112131415161718192021222324252627282930313233
  1. package semverutil
  2. import (
  3. "strings"
  4. "github.com/Masterminds/semver/v3"
  5. )
  6. // Compare returns true if the comparison is true for given versions. It returns false if
  7. // comparison is false, or failed to parse one or both versions as Semantic Versions.
  8. //
  9. // See https://github.com/Masterminds/semver#basic-comparisons for supported comparisons.
  10. func Compare(version1, comparison, version2 string) bool {
  11. clean := func(v string) string {
  12. if strings.Count(v, ".") > 2 {
  13. fields := strings.SplitN(v, ".", 4)
  14. v = strings.Join(fields[:3], ".")
  15. }
  16. return v
  17. }
  18. v, err := semver.NewVersion(clean(version1))
  19. if err != nil {
  20. return false
  21. }
  22. c, err := semver.NewConstraint(comparison + " " + clean(version2))
  23. if err != nil {
  24. return false
  25. }
  26. return c.Check(v)
  27. }