mirror of
https://github.com/opentofu/opentofu.git
synced 2024-12-28 18:01:01 -06:00
a1e29ae290
The semver library we were using doesn't have support for a "pessimistic constraint" where e.g. the user wants to accept only minor or patch version upgrades. This is important for providers since users will generally want to pin their dependencies to not inadvertantly accept breaking changes. So here we switch to hashicorp's home-grown go-version library, which has the ~> constraint operator for this sort of constraint. Given how much the old version object was already intruding into the interface and creating dependency noise in callers, this also now wraps the "raw" go-version objects in package-local structs, thus keeping the details encapsulated and allowing callers to deal just with this package's own types.
38 lines
1.0 KiB
Go
38 lines
1.0 KiB
Go
package discovery
|
|
|
|
import (
|
|
version "github.com/hashicorp/go-version"
|
|
)
|
|
|
|
// A VersionStr is a string containing a possibly-invalid representation
|
|
// of a semver version number. Call Parse on it to obtain a real Version
|
|
// object, or discover that it is invalid.
|
|
type VersionStr string
|
|
|
|
// Parse transforms a VersionStr into a Version if it is
|
|
// syntactically valid. If it isn't then an error is returned instead.
|
|
func (s VersionStr) Parse() (Version, error) {
|
|
raw, err := version.NewVersion(string(s))
|
|
if err != nil {
|
|
return Version{}, err
|
|
}
|
|
return Version{raw}, nil
|
|
}
|
|
|
|
// Version represents a version number that has been parsed from
|
|
// a semver string and known to be valid.
|
|
type Version struct {
|
|
// We wrap this here just because it avoids a proliferation of
|
|
// direct go-version imports all over the place, and keeps the
|
|
// version-processing details within this package.
|
|
raw *version.Version
|
|
}
|
|
|
|
func (v Version) String() string {
|
|
return v.raw.String()
|
|
}
|
|
|
|
func (v Version) newerThan(other Version) bool {
|
|
return v.raw.GreaterThan(other.raw)
|
|
}
|