-
Notifications
You must be signed in to change notification settings - Fork 555
/
semver.go
61 lines (50 loc) · 2.2 KB
/
semver.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package semverutil
import (
"strings"
"golang.org/x/mod/semver"
)
// FeatureReleases contains the combination of versions the feature was released in.
type FeatureReleases struct {
// MajorVersion is the major version in the format including the v. E.g. "v6"
MajorVersion string
// MinorVersions contains a slice of versions including the v and excluding the patch version. E.g. v2.5
MinorVersions []string
}
// IsSupported returns whether the version contains the feature.
// This is true if the version is greater than or equal to the major version it was released in
// or is greater than or equal to the list of minor releases it was included in.
func (fr FeatureReleases) IsSupported(versionStr string) bool {
// in our compatibility tests, our images are in the format of "release-v1.0.x". We want to actually look at
// the "1.0.x" part but we also need this to be a valid version. We can change it to "1.0.0"
// TODO: change the way we provide the ibc-go version. This should be done in a more flexible way such
// as docker labels/metadata instead of the tag, as this will only work for our versioning scheme.
const releasePrefix = "release-"
if strings.HasPrefix(versionStr, releasePrefix) {
versionStr = versionStr[len(releasePrefix):]
// replace x with 999 so the release version is always larger than the others in the release line.
versionStr = strings.ReplaceAll(versionStr, "x", "999")
}
// assume any non-semantic version formatted version supports the feature
// this will be useful during development of the e2e test with the new feature
if !semver.IsValid(versionStr) {
return true
}
if fr.MajorVersion != "" && GTE(versionStr, fr.MajorVersion) {
return true
}
for _, mv := range fr.MinorVersions {
mvMajor, versionStrMajor := semver.Major(mv), semver.Major(versionStr)
if semverEqual(mvMajor, versionStrMajor) {
return GTE(versionStr, mv)
}
}
return false
}
// GTE returns true if versionA is greater than or equal to versionB.
func GTE(versionA, versionB string) bool {
return semver.Compare(versionA, versionB) >= 0
}
// semverEqual returns true if versionA is equal to versionB.
func semverEqual(versionA, versionB string) bool {
return semver.Compare(versionA, versionB) == 0
}