From 8690a026b1d34a0b78932ddd8829c8489d6c0d0c Mon Sep 17 00:00:00 2001 From: mitch Date: Tue, 3 Oct 2023 19:26:18 -0400 Subject: [PATCH] added fix to use x86 in for arm mac's where arm package is unavailable --- SemVersion.go | 47 +++ SemVersion_test.go | 685 ++++++++++++++++++++++++++++++++++++++++++ darwin_amd64.go | 4 +- darwin_arm_64.go | 4 +- freebsd_386.go | 4 +- freebsd_amd_64.go | 4 +- freebsd_arm.go | 4 +- linux_386.go | 4 +- linux_amd_64.go | 4 +- linux_arm.go | 4 +- linux_arm64.go | 4 +- openbsd_386.go | 4 +- openbsd_amd64.go | 4 +- solaris_amd64.go | 4 +- versionedTerraform.go | 12 +- windows_386.go | 4 +- windows_amd64.go | 4 +- 17 files changed, 782 insertions(+), 18 deletions(-) diff --git a/SemVersion.go b/SemVersion.go index c6b36fc..90c67ce 100644 --- a/SemVersion.go +++ b/SemVersion.go @@ -86,3 +86,50 @@ func (s *SemVersion) VersionInSlice(sSem []SemVersion) bool { } return false } + +func (s *SemVersion) IsEqualTo(s2 SemVersion) bool { + if s.majorVersion == s2.majorVersion && s.minorVersion == s2.minorVersion && s.patchVersion == s2.patchVersion { + return true + } + return false +} + +func (s *SemVersion) IsLessThan(s2 SemVersion) bool { + if s2.majorVersion > s.majorVersion { + return true + } + if s2.minorVersion > s.minorVersion && s2.majorVersion == s.majorVersion { + return true + } + if s2.patchVersion > s.patchVersion && s2.majorVersion == s.majorVersion && s2.minorVersion == s.minorVersion { + return true + } + return false +} + +func (s *SemVersion) IsGreaterThan(s2 SemVersion) bool { + if s2.majorVersion < s.majorVersion { + return true + } + if s2.minorVersion < s.minorVersion && s2.majorVersion == s.majorVersion { + return true + } + if s2.patchVersion < s.patchVersion && s2.majorVersion == s.majorVersion && s2.minorVersion == s.minorVersion { + return true + } + return false +} + +func (s *SemVersion) IsLessOrEqual(s2 SemVersion) bool { + if s.IsLessThan(s2) || s.IsEqualTo(s2) { + return true + } + return false +} + +func (s *SemVersion) IsGreaterOrEqual(s2 SemVersion) bool { + if s.IsGreaterThan(s2) || s.IsEqualTo(s2) { + return true + } + return false +} diff --git a/SemVersion_test.go b/SemVersion_test.go index a9268e5..546f952 100644 --- a/SemVersion_test.go +++ b/SemVersion_test.go @@ -86,3 +86,688 @@ func TestSemVersion_VersionInSlice_fail(t *testing.T) { t.Errorf("Expected Sem Version to not be found in semArray") } } + +func TestSemVersion_IsLessThan(t *testing.T) { + cases := []struct { + name string + want bool + s1, s2 SemVersion + }{ + {"IsEqualTo", + false, + SemVersion{ + version: "1.2.3", + isStable: false, + majorVersion: 1, + minorVersion: 2, + patchVersion: 3, + }, + SemVersion{ + version: "1.2.3", + isStable: false, + majorVersion: 1, + minorVersion: 2, + patchVersion: 3, + }, + }, + {"MajorVersionLess", + true, + SemVersion{ + version: "1.2.3", + isStable: false, + majorVersion: 1, + minorVersion: 2, + patchVersion: 3, + }, + SemVersion{ + version: "2.2.3", + isStable: false, + majorVersion: 2, + minorVersion: 2, + patchVersion: 3, + }, + }, + {"MinorVersionLess", + true, + SemVersion{ + version: "1.2.3", + isStable: false, + majorVersion: 1, + minorVersion: 2, + patchVersion: 3, + }, + SemVersion{ + version: "1.3.3", + isStable: false, + majorVersion: 1, + minorVersion: 3, + patchVersion: 3, + }, + }, + {"PatchVersionLess", + true, + SemVersion{ + version: "1.2.3", + isStable: false, + majorVersion: 1, + minorVersion: 2, + patchVersion: 3, + }, + SemVersion{ + version: "1.2.3", + isStable: false, + majorVersion: 2, + minorVersion: 2, + patchVersion: 3, + }, + }, + {"MajorVersionGreater", + false, + SemVersion{ + version: "2.2.3", + isStable: false, + majorVersion: 2, + minorVersion: 2, + patchVersion: 3, + }, + SemVersion{ + version: "1.2.4", + isStable: false, + majorVersion: 1, + minorVersion: 2, + patchVersion: 4, + }, + }, + {"MinorVersionGreater", + false, + SemVersion{ + version: "1.3.3", + isStable: false, + majorVersion: 1, + minorVersion: 3, + patchVersion: 3, + }, + SemVersion{ + version: "1.2.3", + isStable: false, + majorVersion: 1, + minorVersion: 2, + patchVersion: 3, + }, + }, + {"PatchVersionGreater", + false, + SemVersion{ + version: "1.2.4", + isStable: false, + majorVersion: 1, + minorVersion: 2, + patchVersion: 4, + }, + SemVersion{ + version: "1.2.3", + isStable: false, + majorVersion: 1, + minorVersion: 2, + patchVersion: 3, + }, + }, + } + for _, c := range cases { + t.Run("Test: "+c.name, func(t *testing.T) { + t.Parallel() + got := c.s1.IsLessThan(c.s2) + if c.want != got { + t.Errorf("Expected %+v got %+v", c.want, got) + } + }) + } +} + +func TestSemVersion_IsGreaterThan(t *testing.T) { + cases := []struct { + name string + want bool + s1, s2 SemVersion + }{ + {"IsEqualTo", + false, + SemVersion{ + version: "1.2.3", + isStable: false, + majorVersion: 1, + minorVersion: 2, + patchVersion: 3, + }, + SemVersion{ + version: "1.2.3", + isStable: false, + majorVersion: 1, + minorVersion: 2, + patchVersion: 3, + }, + }, + {"MajorVersionLess", + false, + SemVersion{ + version: "1.2.3", + isStable: false, + majorVersion: 1, + minorVersion: 2, + patchVersion: 3, + }, + SemVersion{ + version: "2.2.3", + isStable: false, + majorVersion: 2, + minorVersion: 2, + patchVersion: 3, + }, + }, + {"MinorVersionLess", + false, + SemVersion{ + version: "1.2.3", + isStable: false, + majorVersion: 1, + minorVersion: 2, + patchVersion: 3, + }, + SemVersion{ + version: "1.3.3", + isStable: false, + majorVersion: 1, + minorVersion: 3, + patchVersion: 3, + }, + }, + {"PatchVersionLess", + false, + SemVersion{ + version: "1.2.3", + isStable: false, + majorVersion: 1, + minorVersion: 2, + patchVersion: 3, + }, + SemVersion{ + version: "1.2.3", + isStable: false, + majorVersion: 2, + minorVersion: 2, + patchVersion: 3, + }, + }, + {"MajorVersionGreater", + true, + SemVersion{ + version: "2.2.3", + isStable: false, + majorVersion: 2, + minorVersion: 2, + patchVersion: 3, + }, + SemVersion{ + version: "1.2.4", + isStable: false, + majorVersion: 1, + minorVersion: 2, + patchVersion: 4, + }, + }, + {"MinorVersionGreater", + true, + SemVersion{ + version: "1.3.3", + isStable: false, + majorVersion: 1, + minorVersion: 3, + patchVersion: 3, + }, + SemVersion{ + version: "1.2.3", + isStable: false, + majorVersion: 1, + minorVersion: 2, + patchVersion: 3, + }, + }, + {"PatchVersionGreater", + true, + SemVersion{ + version: "1.2.4", + isStable: false, + majorVersion: 1, + minorVersion: 2, + patchVersion: 4, + }, + SemVersion{ + version: "1.2.3", + isStable: false, + majorVersion: 1, + minorVersion: 2, + patchVersion: 3, + }, + }, + } + for _, c := range cases { + t.Run("Test: "+c.name, func(t *testing.T) { + t.Parallel() + got := c.s1.IsGreaterThan(c.s2) + if c.want != got { + t.Errorf("Expected %+v got %+v", c.want, got) + } + }) + } +} + +func TestSemVersion_IsEqualTo(t *testing.T) { + cases := []struct { + name string + want bool + s1, s2 SemVersion + }{ + {"IsEqualTo", + true, + SemVersion{ + version: "1.2.3", + isStable: false, + majorVersion: 1, + minorVersion: 2, + patchVersion: 3, + }, + SemVersion{ + version: "1.2.3", + isStable: false, + majorVersion: 1, + minorVersion: 2, + patchVersion: 3, + }, + }, + {"MajorVersionLess", + false, + SemVersion{ + version: "1.2.3", + isStable: false, + majorVersion: 1, + minorVersion: 2, + patchVersion: 3, + }, + SemVersion{ + version: "2.2.3", + isStable: false, + majorVersion: 2, + minorVersion: 2, + patchVersion: 3, + }, + }, + {"MinorVersionLess", + false, + SemVersion{ + version: "1.2.3", + isStable: false, + majorVersion: 1, + minorVersion: 2, + patchVersion: 3, + }, + SemVersion{ + version: "1.3.3", + isStable: false, + majorVersion: 1, + minorVersion: 3, + patchVersion: 3, + }, + }, + {"PatchVersionLess", + false, + SemVersion{ + version: "1.2.3", + isStable: false, + majorVersion: 1, + minorVersion: 2, + patchVersion: 3, + }, + SemVersion{ + version: "1.2.3", + isStable: false, + majorVersion: 2, + minorVersion: 2, + patchVersion: 3, + }, + }, + {"MajorVersionGreater", + false, + SemVersion{ + version: "2.2.3", + isStable: false, + majorVersion: 2, + minorVersion: 2, + patchVersion: 3, + }, + SemVersion{ + version: "1.2.4", + isStable: false, + majorVersion: 1, + minorVersion: 2, + patchVersion: 4, + }, + }, + {"MinorVersionGreater", + false, + SemVersion{ + version: "1.3.3", + isStable: false, + majorVersion: 1, + minorVersion: 3, + patchVersion: 3, + }, + SemVersion{ + version: "1.2.3", + isStable: false, + majorVersion: 1, + minorVersion: 2, + patchVersion: 3, + }, + }, + {"PatchVersionGreater", + false, + SemVersion{ + version: "1.2.4", + isStable: false, + majorVersion: 1, + minorVersion: 2, + patchVersion: 4, + }, + SemVersion{ + version: "1.2.3", + isStable: false, + majorVersion: 1, + minorVersion: 2, + patchVersion: 3, + }, + }, + } + for _, c := range cases { + t.Run("Test: "+c.name, func(t *testing.T) { + t.Parallel() + got := c.s1.IsEqualTo(c.s2) + if c.want != got { + t.Errorf("Expected %+v got %+v", c.want, got) + } + }) + } +} + +func TestSemVersion_IsLessOrEqual(t *testing.T) { + cases := []struct { + name string + want bool + s1, s2 SemVersion + }{ + {"IsEqualTo", + true, + SemVersion{ + version: "1.2.3", + isStable: false, + majorVersion: 1, + minorVersion: 2, + patchVersion: 3, + }, + SemVersion{ + version: "1.2.3", + isStable: false, + majorVersion: 1, + minorVersion: 2, + patchVersion: 3, + }, + }, + {"MajorVersionLess", + true, + SemVersion{ + version: "1.2.3", + isStable: false, + majorVersion: 1, + minorVersion: 2, + patchVersion: 3, + }, + SemVersion{ + version: "2.2.3", + isStable: false, + majorVersion: 2, + minorVersion: 2, + patchVersion: 3, + }, + }, + {"MinorVersionLess", + true, + SemVersion{ + version: "1.2.3", + isStable: false, + majorVersion: 1, + minorVersion: 2, + patchVersion: 3, + }, + SemVersion{ + version: "1.3.3", + isStable: false, + majorVersion: 1, + minorVersion: 3, + patchVersion: 3, + }, + }, + {"PatchVersionLess", + true, + SemVersion{ + version: "1.2.3", + isStable: false, + majorVersion: 1, + minorVersion: 2, + patchVersion: 3, + }, + SemVersion{ + version: "1.2.3", + isStable: false, + majorVersion: 2, + minorVersion: 2, + patchVersion: 3, + }, + }, + {"MajorVersionGreater", + false, + SemVersion{ + version: "2.2.3", + isStable: false, + majorVersion: 2, + minorVersion: 2, + patchVersion: 3, + }, + SemVersion{ + version: "1.2.4", + isStable: false, + majorVersion: 1, + minorVersion: 2, + patchVersion: 4, + }, + }, + {"MinorVersionGreater", + false, + SemVersion{ + version: "1.3.3", + isStable: false, + majorVersion: 1, + minorVersion: 3, + patchVersion: 3, + }, + SemVersion{ + version: "1.2.3", + isStable: false, + majorVersion: 1, + minorVersion: 2, + patchVersion: 3, + }, + }, + {"PatchVersionGreater", + false, + SemVersion{ + version: "1.2.4", + isStable: false, + majorVersion: 1, + minorVersion: 2, + patchVersion: 4, + }, + SemVersion{ + version: "1.2.3", + isStable: false, + majorVersion: 1, + minorVersion: 2, + patchVersion: 3, + }, + }, + } + for _, c := range cases { + t.Run("Test: "+c.name, func(t *testing.T) { + t.Parallel() + got := c.s1.IsLessOrEqual(c.s2) + if c.want != got { + t.Errorf("Expected %+v got %+v", c.want, got) + } + }) + } +} + +func TestSemVersion_IsGreaterOrEqual(t *testing.T) { + cases := []struct { + name string + want bool + s1, s2 SemVersion + }{ + {"IsEqualTo", + true, + SemVersion{ + version: "1.2.3", + isStable: false, + majorVersion: 1, + minorVersion: 2, + patchVersion: 3, + }, + SemVersion{ + version: "1.2.3", + isStable: false, + majorVersion: 1, + minorVersion: 2, + patchVersion: 3, + }, + }, + {"MajorVersionLess", + false, + SemVersion{ + version: "1.2.3", + isStable: false, + majorVersion: 1, + minorVersion: 2, + patchVersion: 3, + }, + SemVersion{ + version: "2.2.3", + isStable: false, + majorVersion: 2, + minorVersion: 2, + patchVersion: 3, + }, + }, + {"MinorVersionLess", + false, + SemVersion{ + version: "1.2.3", + isStable: false, + majorVersion: 1, + minorVersion: 2, + patchVersion: 3, + }, + SemVersion{ + version: "1.3.3", + isStable: false, + majorVersion: 1, + minorVersion: 3, + patchVersion: 3, + }, + }, + {"PatchVersionLess", + false, + SemVersion{ + version: "1.2.3", + isStable: false, + majorVersion: 1, + minorVersion: 2, + patchVersion: 3, + }, + SemVersion{ + version: "1.2.3", + isStable: false, + majorVersion: 2, + minorVersion: 2, + patchVersion: 3, + }, + }, + {"MajorVersionGreater", + true, + SemVersion{ + version: "2.2.3", + isStable: false, + majorVersion: 2, + minorVersion: 2, + patchVersion: 3, + }, + SemVersion{ + version: "1.2.4", + isStable: false, + majorVersion: 1, + minorVersion: 2, + patchVersion: 4, + }, + }, + {"MinorVersionGreater", + true, + SemVersion{ + version: "1.3.3", + isStable: false, + majorVersion: 1, + minorVersion: 3, + patchVersion: 3, + }, + SemVersion{ + version: "1.2.3", + isStable: false, + majorVersion: 1, + minorVersion: 2, + patchVersion: 3, + }, + }, + {"PatchVersionGreater", + true, + SemVersion{ + version: "1.2.4", + isStable: false, + majorVersion: 1, + minorVersion: 2, + patchVersion: 4, + }, + SemVersion{ + version: "1.2.3", + isStable: false, + majorVersion: 1, + minorVersion: 2, + patchVersion: 3, + }, + }, + } + for _, c := range cases { + t.Run("Test: "+c.name, func(t *testing.T) { + t.Parallel() + got := c.s1.IsGreaterOrEqual(c.s2) + if c.want != got { + t.Errorf("Expected %+v got %+v", c.want, got) + } + }) + } +} diff --git a/darwin_amd64.go b/darwin_amd64.go index b1984ff..322d51d 100644 --- a/darwin_amd64.go +++ b/darwin_amd64.go @@ -4,5 +4,7 @@ package versionedTerraform const ( - fileSuffix = "_darwin_amd64.zip" + fileSuffix = "_darwin_amd64.zip" + minVersion = "0.0.0" + alternateSuffix = "" ) diff --git a/darwin_arm_64.go b/darwin_arm_64.go index 16c18a5..792f1e3 100644 --- a/darwin_arm_64.go +++ b/darwin_arm_64.go @@ -4,5 +4,7 @@ package versionedTerraform const ( - fileSuffix = "_darwin_arm64.zip" + fileSuffix = "_darwin_arm64.zip" + minVersion = "1.0.2" + alternateSuffix = "_darwin_amd64.zip" ) diff --git a/freebsd_386.go b/freebsd_386.go index e0b2411..625f58f 100644 --- a/freebsd_386.go +++ b/freebsd_386.go @@ -4,5 +4,7 @@ package versionedTerraform const ( - fileSuffix = "_freebsd_386.zip" + fileSuffix = "_freebsd_386.zip" + minVersion = "0.0.0" + alternateSuffix = "" ) diff --git a/freebsd_amd_64.go b/freebsd_amd_64.go index 78547e3..a9cb53b 100644 --- a/freebsd_amd_64.go +++ b/freebsd_amd_64.go @@ -4,5 +4,7 @@ package versionedTerraform const ( - fileSuffix = "_freebsd_amd64.zip" + fileSuffix = "_freebsd_amd64.zip" + minVersion = "0.0.0" + alternateSuffix = "" ) diff --git a/freebsd_arm.go b/freebsd_arm.go index 2676b1e..65dff3f 100644 --- a/freebsd_arm.go +++ b/freebsd_arm.go @@ -4,5 +4,7 @@ package versionedTerraform const ( - fileSuffix = "_freebsd_arm.zip" + fileSuffix = "_freebsd_arm.zip" + minVersion = "0.0.0" + alternateSuffix = "" ) diff --git a/linux_386.go b/linux_386.go index 36913f0..b161e9a 100644 --- a/linux_386.go +++ b/linux_386.go @@ -4,5 +4,7 @@ package versionedTerraform const ( - fileSuffix = "_linux_386.zip" + fileSuffix = "_linux_386.zip" + minVersion = "0.0.0" + alternateSuffix = "" ) diff --git a/linux_amd_64.go b/linux_amd_64.go index 9aceb1d..f8ebb7f 100644 --- a/linux_amd_64.go +++ b/linux_amd_64.go @@ -4,5 +4,7 @@ package versionedTerraform const ( - fileSuffix = "_linux_amd64.zip" + fileSuffix = "_linux_amd64.zip" + minVersion = "0.0.0" + alternateSuffix = "" ) diff --git a/linux_arm.go b/linux_arm.go index e5512a5..b2f3648 100644 --- a/linux_arm.go +++ b/linux_arm.go @@ -4,5 +4,7 @@ package versionedTerraform const ( - fileSuffix = "_linux_arm.zip" + fileSuffix = "_linux_arm.zip" + minVersion = "0.0.0" + alternateSuffix = "" ) diff --git a/linux_arm64.go b/linux_arm64.go index 3969d64..d83b7e0 100644 --- a/linux_arm64.go +++ b/linux_arm64.go @@ -4,5 +4,7 @@ package versionedTerraform const ( - fileSuffix = "_linux_arm64.zip" + fileSuffix = "_linux_arm64.zip" + minVersion = "0.0.0" + alternateSuffix = "" ) diff --git a/openbsd_386.go b/openbsd_386.go index fe61814..699e660 100644 --- a/openbsd_386.go +++ b/openbsd_386.go @@ -4,5 +4,7 @@ package versionedTerraform const ( - fileSuffix = "_openbsd_386.zip" + fileSuffix = "_openbsd_386.zip" + minVersion = "0.0.0" + alternateSuffix = "" ) diff --git a/openbsd_amd64.go b/openbsd_amd64.go index 75eee73..3f70608 100644 --- a/openbsd_amd64.go +++ b/openbsd_amd64.go @@ -4,5 +4,7 @@ package versionedTerraform const ( - fileSuffix = "_openbsd_amd64.zip" + fileSuffix = "_openbsd_amd64.zip" + minVersion = "0.0.0" + alternateSuffix = "" ) diff --git a/solaris_amd64.go b/solaris_amd64.go index 75793ef..a047084 100644 --- a/solaris_amd64.go +++ b/solaris_amd64.go @@ -4,5 +4,7 @@ package versionedTerraform const ( - fileSuffix = "_solaris_amd64.zip" + fileSuffix = "_solaris_amd64.zip" + minVersion = "0.0.0" + alternateSuffix = "" ) diff --git a/versionedTerraform.go b/versionedTerraform.go index 5aaf8b5..4e62e16 100644 --- a/versionedTerraform.go +++ b/versionedTerraform.go @@ -62,7 +62,6 @@ func (v *Version) getOneLessRelease() { //getLatestRelease returns the latest release from Version func (v *Version) getLatestRelease() { - //todo clean up for _, release := range v.availableVersions { if release.majorVersion > v.Version.majorVersion && (release.isStable || !needsStable) { @@ -86,11 +85,16 @@ func (v *Version) getLatestRelease() { //configuration directory func (v *Version) InstallTerraformVersion() error { homeDir, _ := os.UserHomeDir() + suffix := fileSuffix + minV := NewSemVersion(minVersion) + if v.Version.IsLessThan(*minV) { + suffix = alternateSuffix + } resp, err := http.Get(hashicorpUrl + v.Version.ToString() + "/" + terraformPrefix + v.Version.ToString() + - fileSuffix) + suffix) if err != nil { return err } @@ -118,8 +122,8 @@ func (v *Version) InstallTerraformVersion() error { } defer versionedFile.Close() - for _, zipFIle := range zipReader.File { - zr, err := zipFIle.Open() + for _, zipFile := range zipReader.File { + zr, err := zipFile.Open() if err != nil { return err } diff --git a/windows_386.go b/windows_386.go index 75a2eda..9eedef6 100644 --- a/windows_386.go +++ b/windows_386.go @@ -4,5 +4,7 @@ package versionedTerraform const ( - fileSuffix = "_windows_386.zip" + fileSuffix = "_windows_386.zip" + minVersion = "0.0.0" + alternateSuffix = "" ) diff --git a/windows_amd64.go b/windows_amd64.go index dc561d8..ce418a7 100644 --- a/windows_amd64.go +++ b/windows_amd64.go @@ -4,5 +4,7 @@ package versionedTerraform const ( - fileSuffix = "_windows_amd64.zip" + fileSuffix = "_windows_amd64.zip" + minVersion = "0.0.0" + alternateSuffix = "" )