aboutsummaryrefslogtreecommitdiffhomepage
path: root/vendor/github.com/hashicorp/terraform/plugin/discovery/version.go
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/github.com/hashicorp/terraform/plugin/discovery/version.go')
-rw-r--r--vendor/github.com/hashicorp/terraform/plugin/discovery/version.go72
1 files changed, 72 insertions, 0 deletions
diff --git a/vendor/github.com/hashicorp/terraform/plugin/discovery/version.go b/vendor/github.com/hashicorp/terraform/plugin/discovery/version.go
new file mode 100644
index 0000000..8fad58d
--- /dev/null
+++ b/vendor/github.com/hashicorp/terraform/plugin/discovery/version.go
@@ -0,0 +1,72 @@
1package discovery
2
3import (
4 "fmt"
5 "sort"
6
7 version "github.com/hashicorp/go-version"
8)
9
10const VersionZero = "0.0.0"
11
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.
15type VersionStr string
16
17// Parse transforms a VersionStr into a Version if it is
18// syntactically valid. If it isn't then an error is returned instead.
19func (s VersionStr) Parse() (Version, error) {
20 raw, err := version.NewVersion(string(s))
21 if err != nil {
22 return Version{}, err
23 }
24 return Version{raw}, nil
25}
26
27// MustParse transforms a VersionStr into a Version if it is
28// syntactically valid. If it isn't then it panics.
29func (s VersionStr) MustParse() Version {
30 ret, err := s.Parse()
31 if err != nil {
32 panic(err)
33 }
34 return ret
35}
36
37// Version represents a version number that has been parsed from
38// a semver string and known to be valid.
39type Version struct {
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.
43 raw *version.Version
44}
45
46func (v Version) String() string {
47 return v.raw.String()
48}
49
50func (v Version) NewerThan(other Version) bool {
51 return v.raw.GreaterThan(other.raw)
52}
53
54func (v Version) Equal(other Version) bool {
55 return v.raw.Equal(other.raw)
56}
57
58// MinorUpgradeConstraintStr returns a ConstraintStr that would permit
59// minor upgrades relative to the receiving version.
60func (v Version) MinorUpgradeConstraintStr() ConstraintStr {
61 segments := v.raw.Segments()
62 return ConstraintStr(fmt.Sprintf("~> %d.%d", segments[0], segments[1]))
63}
64
65type Versions []Version
66
67// Sort sorts version from newest to oldest.
68func (v Versions) Sort() {
69 sort.Slice(v, func(i, j int) bool {
70 return v[i].NewerThan(v[j])
71 })
72}