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