2017-04-20 02:04:09 +02:00
|
|
|
package discovery
|
|
|
|
|
|
|
|
import (
|
2017-06-02 02:57:43 +02:00
|
|
|
"fmt"
|
2017-05-31 22:08:56 +02:00
|
|
|
"sort"
|
|
|
|
|
2017-04-20 02:04:09 +02:00
|
|
|
version "github.com/hashicorp/go-version"
|
|
|
|
)
|
|
|
|
|
2017-06-16 22:19:13 +02:00
|
|
|
const VersionZero = "0.0.0"
|
|
|
|
|
2017-04-20 02:04:09 +02:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2017-05-25 00:03:36 +02:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2017-04-20 02:04:09 +02:00
|
|
|
// 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()
|
|
|
|
}
|
|
|
|
|
2017-05-03 23:54:47 +02:00
|
|
|
func (v Version) NewerThan(other Version) bool {
|
2017-04-20 02:04:09 +02:00
|
|
|
return v.raw.GreaterThan(other.raw)
|
|
|
|
}
|
2017-05-31 22:08:56 +02:00
|
|
|
|
2017-06-13 03:22:47 +02:00
|
|
|
func (v Version) Equal(other Version) bool {
|
|
|
|
return v.raw.Equal(other.raw)
|
|
|
|
}
|
|
|
|
|
2017-06-02 02:57:43 +02:00
|
|
|
// 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]))
|
|
|
|
}
|
|
|
|
|
2017-05-31 22:08:56 +02:00
|
|
|
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])
|
|
|
|
})
|
|
|
|
}
|