|
| 1 | +package util |
| 2 | + |
| 3 | +import ( |
| 4 | + "log" |
| 5 | + "strings" |
| 6 | + |
| 7 | + "golang.org/x/mod/semver" |
| 8 | +) |
| 9 | + |
| 10 | +// A type used to represent values known to be valid semantic versions. |
| 11 | +type SemVer interface { |
| 12 | + String() string |
| 13 | + // Compares this semantic version against the `other`. Returns the following values: |
| 14 | + // |
| 15 | + // 0 if both versions are equal. |
| 16 | + // |
| 17 | + // -1 if this version is older than the `other`. |
| 18 | + // |
| 19 | + // 1 if this version is newer than the `other`. |
| 20 | + Compare(other SemVer) int |
| 21 | + // Returns true if this version is newer than the `other`, or false otherwise. |
| 22 | + IsNewerThan(other SemVer) bool |
| 23 | + // Returns true if this version is equal to `other` or newer, or false otherwise. |
| 24 | + IsAtLeast(other SemVer) bool |
| 25 | + // Returns true if this version is older than the `other`, or false otherwise. |
| 26 | + IsOlderThan(other SemVer) bool |
| 27 | + // Returns true if this version is equal to `other` or older, or false otherwise. |
| 28 | + IsAtMost(other SemVer) bool |
| 29 | + // Returns the `major.minor` version prefix of the semantic version. For example, "v1.2.3" becomes "v1.2". |
| 30 | + MajorMinor() SemVer |
| 31 | +} |
| 32 | + |
| 33 | +// The internal representation used for values known to be valid semantic versions. |
| 34 | +// |
| 35 | +// NOTE: Not exported to prevent invalid values from being constructed. |
| 36 | +type semVer string |
| 37 | + |
| 38 | +// Converts the semantic version to a string representation. |
| 39 | +func (ver semVer) String() string { |
| 40 | + return string(ver) |
| 41 | +} |
| 42 | + |
| 43 | +// Represents `v0.0.0`. |
| 44 | +func Zero() SemVer { |
| 45 | + return semVer("v0.0.0") |
| 46 | +} |
| 47 | + |
| 48 | +// Constructs a [SemVer] from the given `version` string. The input can be any valid version string |
| 49 | +// that we commonly deal with. This includes ordinary version strings such as "1.2.3", ones with |
| 50 | +// the "go" prefix, and ones with the "v" prefix. Go's non-semver-compliant release candidate |
| 51 | +// versions are also automatically corrected from e.g. "go1.20rc1" to "v1.20-rc1". If given |
| 52 | +// the empty string, this function return `nil`. Otherwise, for invalid version strings, the function |
| 53 | +// prints a message to the log and exits the process. |
| 54 | +func NewSemVer(version string) SemVer { |
| 55 | + // If the input is the empty string, return nil f |
| 56 | + if version == "" { |
| 57 | + return nil |
| 58 | + } |
| 59 | + |
| 60 | + // Drop a "go" prefix, if there is one. |
| 61 | + version = strings.TrimPrefix(version, "go") |
| 62 | + |
| 63 | + // Go versions don't follow the SemVer format, but the only exception we normally care about |
| 64 | + // is release candidates; so this is a horrible hack to convert e.g. `1.22rc1` into `1.22-rc1` |
| 65 | + // which is compatible with the SemVer specification. |
| 66 | + rcIndex := strings.Index(version, "rc") |
| 67 | + if rcIndex != -1 { |
| 68 | + version = semver.Canonical("v"+version[:rcIndex]) + "-" + version[rcIndex:] |
| 69 | + } |
| 70 | + |
| 71 | + // Add the "v" prefix that is required by the `semver` package. |
| 72 | + if !strings.HasPrefix(version, "v") { |
| 73 | + version = "v" + version |
| 74 | + } |
| 75 | + |
| 76 | + // Convert the remaining version string to a canonical semantic version, |
| 77 | + // and check that this was successful. |
| 78 | + canonical := semver.Canonical(version) |
| 79 | + |
| 80 | + if canonical == "" { |
| 81 | + log.Fatalf("%s is not a valid version string\n", version) |
| 82 | + } |
| 83 | + |
| 84 | + return semVer(canonical) |
| 85 | +} |
| 86 | + |
| 87 | +func (ver semVer) Compare(other SemVer) int { |
| 88 | + return semver.Compare(string(ver), string(other.String())) |
| 89 | +} |
| 90 | + |
| 91 | +func (ver semVer) IsNewerThan(other SemVer) bool { |
| 92 | + return ver.Compare(other) > 0 |
| 93 | +} |
| 94 | + |
| 95 | +func (ver semVer) IsAtLeast(other SemVer) bool { |
| 96 | + return ver.Compare(other) >= 0 |
| 97 | +} |
| 98 | + |
| 99 | +func (ver semVer) IsOlderThan(other SemVer) bool { |
| 100 | + return ver.Compare(other) < 0 |
| 101 | +} |
| 102 | + |
| 103 | +func (ver semVer) IsAtMost(other SemVer) bool { |
| 104 | + return ver.Compare(other) <= 0 |
| 105 | +} |
| 106 | + |
| 107 | +func (ver semVer) MajorMinor() SemVer { |
| 108 | + return semVer(semver.MajorMinor(string(ver))) |
| 109 | +} |
0 commit comments