diff --git a/README.md b/README.md index 9b8e048..713c20e 100644 --- a/README.md +++ b/README.md @@ -18,5 +18,3 @@ All arguments are passed through to terraform `versionedTerraform version` will display the terraform version executed in a folder ## Known Issues -Currently, does not support semantic versioning between values
-i.e. `required_version = "~> 0.14", "< 0.14.3"` diff --git a/SemVersion.go b/SemVersion.go index fd84e49..b322872 100644 --- a/SemVersion.go +++ b/SemVersion.go @@ -8,8 +8,10 @@ import ( const ( //todo include others if needed //todo add comparison i.e. >= 0.11.10, < 0.12.0 - latestRelease = ">=" - latestPatch = "~>" + latestRelease = ">=" + latestPatch = "~>" + versionLessOrEqual = "<=" + versionLessThan = "<" ) type SemVersion struct { diff --git a/versionedTerraform.go b/versionedTerraform.go index 49b987a..56075f9 100644 --- a/versionedTerraform.go +++ b/versionedTerraform.go @@ -9,6 +9,7 @@ import ( "io/ioutil" "net/http" "os" + "regexp" "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 func (v *Version) getLatestRelease() { //todo clean up @@ -130,6 +154,39 @@ func NewVersion(_version string, _vList []string) *Version { 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 { case strings.Contains(v.Version.ToString(), latestRelease): 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] v.Version = *NewSemVersion(release) 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 @@ -191,3 +255,29 @@ func removeSpacesVersion(v string) string { func (v *Version) VersionToString() string { 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 +} diff --git a/versionedTerraform_test.go b/versionedTerraform_test.go index 95193eb..53321f4 100644 --- a/versionedTerraform_test.go +++ b/versionedTerraform_test.go @@ -19,6 +19,9 @@ func testVersionList() []string { "1.1.1", "1.0.12", "1.0.1", + "0.14.0", + "0.13.1", + "0.13.0", "0.12.31", "0.12.30", "0.11.15", @@ -39,6 +42,11 @@ func TestGetVersion(t *testing.T) { {testVersionList(), ">= 0.11.15", "1.1.11"}, {testVersionList(), ">= 0.12.0", "1.1.11"}, {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 { @@ -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) { //todo write test for this //response, _ := getVersionList()