mirror of
https://github.com/opentofu/opentofu.git
synced 2024-12-30 10:47:14 -06:00
4ba20f9c1c
When running "terraform init" with providers that are unconstrained, we will now produce information to help the user update configuration to constrain for the particular providers that were chosen, to prevent inadvertently drifting onto a newer major release that might contain breaking changes. A ~> constraint is used here because pinning to a single specific version is expected to create dependency hell when using child modules. By using this constraint mode, which allows minor version upgrades, we avoid the need for users to constantly adjust version constraints across many modules, but make major version upgrades still be opt-in. Any constraint at all in the configuration will prevent the display of these suggestions, so users are free to use stronger or weaker constraints if desired, ignoring the recommendation.
67 lines
1.7 KiB
Go
67 lines
1.7 KiB
Go
package discovery
|
|
|
|
import (
|
|
"fmt"
|
|
"sort"
|
|
|
|
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
|
|
}
|
|
|
|
// MustParse transforms a VersionStr into a Version if it is
|
|
// syntactically valid. If it isn't then it panics.
|
|
func (s VersionStr) MustParse() Version {
|
|
ret, err := s.Parse()
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return ret
|
|
}
|
|
|
|
// 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)
|
|
}
|
|
|
|
// MinorUpgradeConstraintStr returns a ConstraintStr that would permit
|
|
// minor upgrades relative to the receiving version.
|
|
func (v Version) MinorUpgradeConstraintStr() ConstraintStr {
|
|
segments := v.raw.Segments()
|
|
return ConstraintStr(fmt.Sprintf("~> %d.%d", segments[0], segments[1]))
|
|
}
|
|
|
|
type Versions []Version
|
|
|
|
// Sort sorts version from newest to oldest.
|
|
func (v Versions) Sort() {
|
|
sort.Slice(v, func(i, j int) bool {
|
|
return v[i].NewerThan(v[j])
|
|
})
|
|
}
|