-
Notifications
You must be signed in to change notification settings - Fork 135
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
912f548
commit 746549d
Showing
5 changed files
with
181 additions
and
199 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package lib | ||
|
||
import ( | ||
"fmt" | ||
"sort" | ||
|
||
semver "github.com/hashicorp/go-version" | ||
) | ||
|
||
// GetSemver : returns version that will be installed based on server constaint provided | ||
func GetSemver(tfconstraint *string, mirrorURL *string) (string, error) { | ||
|
||
listAll := true | ||
tflist, _ := GetTFList(*mirrorURL, listAll) //get list of versions | ||
fmt.Printf("Reading required version from constraint: %s\n", *tfconstraint) | ||
tfversion, err := SemVerParser(tfconstraint, tflist) | ||
return tfversion, err | ||
} | ||
|
||
// ValidateSemVer : Goes through the list of terraform version, return a valid tf version for contraint provided | ||
func SemVerParser(tfconstraint *string, tflist []string) (string, error) { | ||
tfversion := "" | ||
constraints, err := semver.NewConstraint(*tfconstraint) //NewConstraint returns a Constraints instance that a Version instance can be checked against | ||
if err != nil { | ||
return "", fmt.Errorf("error parsing constraint: %s", *tfconstraint) | ||
} | ||
versions := make([]*semver.Version, len(tflist)) | ||
//put tfversion into semver object | ||
for i, tfvals := range tflist { | ||
version, err := semver.NewVersion(tfvals) //NewVersion parses a given version and returns an instance of Version or an error if unable to parse the version. | ||
if err != nil { | ||
return "", fmt.Errorf("error parsing constraint: %s", err) | ||
} | ||
versions[i] = version | ||
} | ||
|
||
sort.Sort(sort.Reverse(semver.Collection(versions))) | ||
|
||
for _, element := range versions { | ||
if constraints.Check(element) { // Validate a version against a constraint | ||
tfversion = element.String() | ||
fmt.Printf("Matched version: %s\n", tfversion) | ||
if ValidVersionFormat(tfversion) { //check if version format is correct | ||
return tfversion, nil | ||
} | ||
} | ||
} | ||
|
||
PrintInvalidTFVersion() | ||
return "", fmt.Errorf("error parsing constraint: %s", *tfconstraint) | ||
} | ||
|
||
// Print invalid TF version | ||
func PrintInvalidTFVersion() { | ||
fmt.Println("Version does not exist or invalid terraform version format.\n Format should be #.#.# or #.#.#-@# where # are numbers and @ are word characters.\n For example, 0.11.7 and 0.11.9-beta1 are valid versions") | ||
} | ||
|
||
// Print invalid TF version | ||
func PrintInvalidMinorTFVersion() { | ||
fmt.Println("Invalid minor terraform version format. Format should be #.# where # are numbers. For example, 0.11 is valid version") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
package lib_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/warrensbox/terraform-switcher/lib" | ||
) | ||
|
||
var versionsRaw = []string{ | ||
"1.1", | ||
"1.2.1", | ||
"1.2.2", | ||
"1.2.3", | ||
"1.3", | ||
"1.1.4", | ||
"0.7.1", | ||
"1.4-beta", | ||
"1.4", | ||
"2"} | ||
|
||
// TestSemverParser1 : Test to see if SemVerParser parses valid version | ||
// Test version 1.1 | ||
func TestSemverParserCase1(t *testing.T) { | ||
|
||
tfconstraint := "1.1" | ||
tfversion, _ := lib.SemVerParser(&tfconstraint, versionsRaw) | ||
expected := "1.1.0" | ||
if tfversion == expected { | ||
t.Logf("Version exist in list %v [expected]", expected) | ||
} else { | ||
t.Logf("Version does not exist in list %v [unexpected]", tfconstraint) | ||
t.Errorf("This is unexpected. Parsing failed. Expected: %v", expected) | ||
} | ||
} | ||
|
||
// TestSemverParserCase2 : Test to see if SemVerParser parses valid version | ||
// Test version ~> 1.1 should return 1.1.4 | ||
func TestSemverParserCase2(t *testing.T) { | ||
|
||
tfconstraint := "~> 1.1.0" | ||
tfversion, _ := lib.SemVerParser(&tfconstraint, versionsRaw) | ||
expected := "1.1.4" | ||
if tfversion == expected { | ||
t.Logf("Version exist in list %v [expected]", expected) | ||
} else { | ||
t.Logf("Version does not exist in list %v [unexpected]", tfconstraint) | ||
t.Errorf("This is unexpected. Parsing failed. Expected: %v", expected) | ||
} | ||
} | ||
|
||
// TestSemverParserCase3 : Test to see if SemVerParser parses valid version | ||
// Test version ~> 1.1 should return 1.1.4 | ||
func TestSemverParserCase3(t *testing.T) { | ||
|
||
tfconstraint := "~> 1.A.0" | ||
_, err := lib.SemVerParser(&tfconstraint, versionsRaw) | ||
if err != nil { | ||
t.Logf("This test is suppose to error %v [expected]", tfconstraint) | ||
} else { | ||
t.Errorf("This test is suppose to error but passed %v [expected]", tfconstraint) | ||
} | ||
} | ||
|
||
// TestSemverParserCase4 : Test to see if SemVerParser parses valid version | ||
// Test version ~> >= 1.0, < 1.4 should return 1.3.0 | ||
func TestSemverParserCase4(t *testing.T) { | ||
|
||
tfconstraint := ">= 1.0, < 1.4" | ||
tfversion, _ := lib.SemVerParser(&tfconstraint, versionsRaw) | ||
expected := "1.3.0" | ||
if tfversion == expected { | ||
t.Logf("Version exist in list %v [expected]", expected) | ||
} else { | ||
t.Logf("Version does not exist in list %v [unexpected]", tfconstraint) | ||
t.Errorf("This is unexpected. Parsing failed. Expected: %v", expected) | ||
} | ||
} | ||
|
||
// TestSemverParserCase5 : Test to see if SemVerParser parses valid version | ||
// Test version ~> >= 1.0 should return 2.0.0 | ||
func TestSemverParserCase5(t *testing.T) { | ||
|
||
tfconstraint := ">= 1.0" | ||
tfversion, _ := lib.SemVerParser(&tfconstraint, versionsRaw) | ||
expected := "2.0.0" | ||
if tfversion == expected { | ||
t.Logf("Version exist in list %v [expected]", expected) | ||
} else { | ||
t.Logf("Version does not exist in list %v [unexpected]", tfconstraint) | ||
t.Errorf("This is unexpected. Parsing failed. Expected: %v", expected) | ||
} | ||
} |
Oops, something went wrong.