Updated to support sem version comparison

This commit is contained in:
mitch 2022-04-27 07:35:49 -04:00
parent f355b83fe5
commit ae2805a729
4 changed files with 155 additions and 4 deletions

View File

@ -18,5 +18,3 @@ All arguments are passed through to terraform
`versionedTerraform version` will display the terraform version executed in a folder `versionedTerraform version` will display the terraform version executed in a folder
## Known Issues ## Known Issues
Currently, does not support semantic versioning between values<br>
i.e. `required_version = "~> 0.14", "< 0.14.3"`

View File

@ -10,6 +10,8 @@ const (
//todo add comparison i.e. >= 0.11.10, < 0.12.0 //todo add comparison i.e. >= 0.11.10, < 0.12.0
latestRelease = ">=" latestRelease = ">="
latestPatch = "~>" latestPatch = "~>"
versionLessOrEqual = "<="
versionLessThan = "<"
) )
type SemVersion struct { type SemVersion struct {

View File

@ -9,6 +9,7 @@ import (
"io/ioutil" "io/ioutil"
"net/http" "net/http"
"os" "os"
"regexp"
"strings" "strings"
) )
@ -45,6 +46,29 @@ func (v *Version) getLatestMajorRelease() {
} }
} }
//getGreatestRelease() returns less than release
func (v *Version) getOneLessRelease() {
var vSlice []Version
for _, release := range v.availableVersions {
_v := Version{
Version: release,
availableVersions: v.availableVersions,
installedVersions: v.installedVersions,
}
if isVersionGreater(*v, _v) {
vSlice = append(vSlice, _v)
}
}
for i, ver := range vSlice {
if isVersionGreater(ver, *v) || i == 0 {
*v = ver
}
}
}
//getLatestRelease returns the latest release from Version //getLatestRelease returns the latest release from Version
func (v *Version) getLatestRelease() { func (v *Version) getLatestRelease() {
//todo clean up //todo clean up
@ -130,6 +154,39 @@ func NewVersion(_version string, _vList []string) *Version {
v.availableVersions = append(v.availableVersions, *NewSemVersion(release)) v.availableVersions = append(v.availableVersions, *NewSemVersion(release))
} }
hasMultiVersions, _ := regexp.MatchString("\\d+,", v.Version.ToString())
if hasMultiVersions {
releases := strings.Split(v.Version.ToString(), ",")
for iteration, _release := range releases {
_v := new(Version)
_v.availableVersions = v.availableVersions
switch {
case strings.Contains(_release, latestRelease):
release := strings.Split(_release, latestRelease)[1]
_v.Version = *NewSemVersion(release)
_v.getLatestRelease()
case strings.Contains(_release, latestPatch):
release := strings.Split(_release, latestPatch)[1]
_v.Version = *NewSemVersion(release)
_v.getLatestMajorRelease()
case strings.Contains(_release, versionLessOrEqual):
release := strings.Split(_release, versionLessOrEqual)[1]
_v.Version = *NewSemVersion(release)
case strings.Contains(_release, versionLessThan):
release := strings.Split(_release, versionLessThan)[1]
_v.Version = *NewSemVersion(release)
_v.getOneLessRelease()
}
if isVersionGreater(*_v, *v) || iteration == 0 {
v = _v
}
}
return v
}
switch { switch {
case strings.Contains(v.Version.ToString(), latestRelease): case strings.Contains(v.Version.ToString(), latestRelease):
release := strings.Split(v.Version.ToString(), latestRelease)[1] release := strings.Split(v.Version.ToString(), latestRelease)[1]
@ -139,6 +196,13 @@ func NewVersion(_version string, _vList []string) *Version {
release := strings.Split(v.Version.ToString(), latestPatch)[1] release := strings.Split(v.Version.ToString(), latestPatch)[1]
v.Version = *NewSemVersion(release) v.Version = *NewSemVersion(release)
v.getLatestMajorRelease() v.getLatestMajorRelease()
case strings.Contains(v.Version.ToString(), versionLessOrEqual):
release := strings.Split(v.Version.ToString(), versionLessOrEqual)[1]
v.Version = *NewSemVersion(release)
case strings.Contains(v.Version.ToString(), versionLessThan):
release := strings.Split(v.Version.ToString(), versionLessThan)[1]
v.Version = *NewSemVersion(release)
v.getOneLessRelease()
} }
return v return v
@ -191,3 +255,29 @@ func removeSpacesVersion(v string) string {
func (v *Version) VersionToString() string { func (v *Version) VersionToString() string {
return v.Version.ToString() return v.Version.ToString()
} }
//versionCompare returns true if v1 is greater than v2
func isVersionGreater(v1 Version, v2 Version) bool {
if v1.Version.majorVersion != v2.Version.majorVersion {
if v1.Version.majorVersion > v2.Version.majorVersion {
return true
}
return false
}
if v1.Version.minorVersion != v2.Version.minorVersion {
if v1.Version.minorVersion > v2.Version.minorVersion {
return true
}
return false
}
if v1.Version.patchVersion != v2.Version.patchVersion {
if v1.Version.patchVersion > v2.Version.patchVersion {
return true
}
return false
}
return false
}

View File

@ -19,6 +19,9 @@ func testVersionList() []string {
"1.1.1", "1.1.1",
"1.0.12", "1.0.12",
"1.0.1", "1.0.1",
"0.14.0",
"0.13.1",
"0.13.0",
"0.12.31", "0.12.31",
"0.12.30", "0.12.30",
"0.11.15", "0.11.15",
@ -39,6 +42,11 @@ func TestGetVersion(t *testing.T) {
{testVersionList(), ">= 0.11.15", "1.1.11"}, {testVersionList(), ">= 0.11.15", "1.1.11"},
{testVersionList(), ">= 0.12.0", "1.1.11"}, {testVersionList(), ">= 0.12.0", "1.1.11"},
{testVersionList(), "~> 0.12", "0.12.31"}, {testVersionList(), "~> 0.12", "0.12.31"},
{testVersionList(), "< 0.12", "0.11.15"},
{testVersionList(), "<= 0.12.31", "0.12.31"},
{testVersionList(), "~> 0.12.0, < 0.13", "0.12.31"},
{testVersionList(), "~> 0.12.0, < 0.14", "0.13.1"},
{testVersionList(), "~> 0.12.0, <= 0.14.0", "0.14.0"},
} }
for _, c := range cases { for _, c := range cases {
@ -74,6 +82,59 @@ func TestRemoveSpacesVersion(t *testing.T) {
} }
} }
func TestIsVersionGreater(t *testing.T) {
cases := []struct {
testName string
testValueOne, testValueTwo Version
want bool
}{
{"equal versions",
*NewVersion("0.12.10", testVersionList()),
*NewVersion("0.12.10", testVersionList()),
false,
},
{"major greater versions",
*NewVersion("1.12.10", testVersionList()),
*NewVersion("0.12.10", testVersionList()),
true,
},
{"major less versions",
*NewVersion("0.12.10", testVersionList()),
*NewVersion("1.12.10", testVersionList()),
false,
},
{"minor greater versions",
*NewVersion("0.13.10", testVersionList()),
*NewVersion("0.12.10", testVersionList()),
true,
},
{"minor less versions",
*NewVersion("0.12.10", testVersionList()),
*NewVersion("0.13.10", testVersionList()),
false,
},
{"patch greater versions",
*NewVersion("0.12.11", testVersionList()),
*NewVersion("0.12.10", testVersionList()),
true,
},
{"patch less versions",
*NewVersion("0.12.10", testVersionList()),
*NewVersion("0.12.11", testVersionList()),
false,
},
}
for _, c := range cases {
t.Run("test isVersionGreater: "+c.testName, func(t *testing.T) {
got := isVersionGreater(c.testValueOne, c.testValueTwo)
if got != c.want {
t.Errorf("Got %t, want %t", got, c.want)
}
})
}
}
func TestGetVersionList(t *testing.T) { func TestGetVersionList(t *testing.T) {
//todo write test for this //todo write test for this
//response, _ := getVersionList() //response, _ := getVersionList()