7 version "github.com/hashicorp/go-version"
10 const VersionZero = "0.0.0"
12 // A VersionStr is a string containing a possibly-invalid representation
13 // of a semver version number. Call Parse on it to obtain a real Version
14 // object, or discover that it is invalid.
15 type VersionStr string
17 // Parse transforms a VersionStr into a Version if it is
18 // syntactically valid. If it isn't then an error is returned instead.
19 func (s VersionStr) Parse() (Version, error) {
20 raw, err := version.NewVersion(string(s))
24 return Version{raw}, nil
27 // MustParse transforms a VersionStr into a Version if it is
28 // syntactically valid. If it isn't then it panics.
29 func (s VersionStr) MustParse() Version {
37 // Version represents a version number that has been parsed from
38 // a semver string and known to be valid.
40 // We wrap this here just because it avoids a proliferation of
41 // direct go-version imports all over the place, and keeps the
42 // version-processing details within this package.
46 func (v Version) String() string {
50 func (v Version) NewerThan(other Version) bool {
51 return v.raw.GreaterThan(other.raw)
54 func (v Version) Equal(other Version) bool {
55 return v.raw.Equal(other.raw)
58 // MinorUpgradeConstraintStr returns a ConstraintStr that would permit
59 // minor upgrades relative to the receiving version.
60 func (v Version) MinorUpgradeConstraintStr() ConstraintStr {
61 segments := v.raw.Segments()
62 return ConstraintStr(fmt.Sprintf("~> %d.%d", segments[0], segments[1]))
65 type Versions []Version
67 // Sort sorts version from newest to oldest.
68 func (v Versions) Sort() {
69 sort.Slice(v, func(i, j int) bool {
70 return v[i].NewerThan(v[j])