Added support for default terraform version

Added default behavior for > and >= to not use non-stable release versions
Added configuration option of `StableOnly` which defaults to true.  Setting this to false will permit the use of -alpha and -beta releases
This commit is contained in:
mitch 2022-06-15 22:52:19 -04:00
parent aed9e15f6e
commit 98493c8d52
9 changed files with 107 additions and 35 deletions

View File

@ -14,7 +14,12 @@ download, and execute that version
All arguments are passed through to terraform All arguments are passed through to terraform
``` ```
## sample usage ## Sample usage
`versionedTerraform version` will display the terraform version executed in a folder `versionedTerraform version` will display the terraform version executed in a folder
## Configuration
A configuration file is created in `~/.versionedTerraform`<br><br>
`StableOnly` boolean values: <b>true</b>/false<br>
This value is used to restrict terraform to release versions only defaults to true
## Known Issues ## Known Issues

View File

@ -16,6 +16,7 @@ const (
type SemVersion struct { type SemVersion struct {
version string version string
isStable bool
majorVersion int majorVersion int
minorVersion int minorVersion int
patchVersion int patchVersion int
@ -29,6 +30,7 @@ type SemVersionInterface interface {
func NewSemVersion(v string) *SemVersion { func NewSemVersion(v string) *SemVersion {
s := new(SemVersion) s := new(SemVersion)
s.isStable = true
s.version = removeSpacesVersion(v) s.version = removeSpacesVersion(v)
s.setMajorVersion() s.setMajorVersion()
@ -52,13 +54,18 @@ func (s *SemVersion) setMinorVersion() {
func (s *SemVersion) setPatchVersion() { func (s *SemVersion) setPatchVersion() {
version := s.version version := s.version
var err error
patchStringSlice := strings.Split(version, ".") patchStringSlice := strings.Split(version, ".")
if len(patchStringSlice) < 3 { if len(patchStringSlice) < 3 {
s.patchVersion = 0 s.patchVersion = 0
return return
} }
s.patchVersion, _ = strconv.Atoi(patchStringSlice[2]) s.patchVersion, err = strconv.Atoi(patchStringSlice[2])
if err != nil {
s.isStable = false
patchStringSlice = strings.Split(patchStringSlice[2], "-")
s.patchVersion, _ = strconv.Atoi(patchStringSlice[0])
}
} }
func (s *SemVersion) ToString() string { func (s *SemVersion) ToString() string {

View File

@ -15,6 +15,8 @@ const (
terraformPrefix = "/terraform_" terraformPrefix = "/terraform_"
) )
var needsStable = true
func main() { func main() {
homeDir, _ := os.UserHomeDir() homeDir, _ := os.UserHomeDir()
configDirString := homeDir + shortConfigDirString configDirString := homeDir + shortConfigDirString
@ -69,7 +71,12 @@ func main() {
vSlice = append(vSlice, v.ToString()) vSlice = append(vSlice, v.ToString())
} }
ver, err := versionedTerraform.GetVersionFromFile(workingDir, vSlice) needsStable, err = versionedTerraform.ConfigRequiresStable(configDir, configFileLocation)
if err != nil {
fmt.Fprintf(os.Stderr, "Unable to open config file, defaulting to stable versions of terraform only")
}
ver, err := versionedTerraform.GetVersionFromFile(workingDir, vSlice, needsStable)
if err != nil { if err != nil {
fmt.Printf("Unable to retrieve terraform version from files: %v", err) fmt.Printf("Unable to retrieve terraform version from files: %v", err)
} }

View File

@ -12,10 +12,33 @@ import (
) )
type configStruct struct { type configStruct struct {
StableOnly bool
LastUpdate int64 LastUpdate int64
AvailableVersions []string AvailableVersions []string
} }
func ConfigRequiresStable(fileSystem fs.FS, configFile string) (bool, error) {
fileHandle, err := fileSystem.Open(configFile)
if err != nil {
return true, err
}
defer fileHandle.Close()
fileScanner := bufio.NewScanner(fileHandle)
fileScanner.Split(bufio.ScanLines)
for fileScanner.Scan() {
_line := fileScanner.Text()
if strings.Contains(_line, "StableOnly: ") {
isStable := strings.SplitAfter(_line, "StableOnly: ")[1]
if strings.EqualFold(isStable, "false") {
return false, nil
}
}
}
return true, nil
}
func NeedToUpdateAvailableVersions(fileSystem fs.FS, availableVersions string) (bool, error) { func NeedToUpdateAvailableVersions(fileSystem fs.FS, availableVersions string) (bool, error) {
//todo this is used a lot abstract it? //todo this is used a lot abstract it?
fileHandle, err := fileSystem.Open(availableVersions) fileHandle, err := fileSystem.Open(availableVersions)
@ -120,6 +143,8 @@ func CreateConfig(directory string, configFile string) error {
fileHandler, err := os.Create(configFileName) fileHandler, err := os.Create(configFileName)
defer fileHandler.Close() defer fileHandler.Close()
lineToByte := []byte(fmt.Sprintf("StableOnly: true\n"))
fileHandler.Write(lineToByte)
err = UpdateConfig(*fileHandler) err = UpdateConfig(*fileHandler)
return err return err
} }

View File

@ -85,6 +85,7 @@ func TestInstalledVersions(t *testing.T) {
"terraform_0.11.15": {Data: []byte("")}, "terraform_0.11.15": {Data: []byte("")},
"terraform_1.0.1": {Data: []byte("")}, "terraform_1.0.1": {Data: []byte("")},
"terraform_1.0.12": {Data: []byte("")}, "terraform_1.0.12": {Data: []byte("")},
"terraform_1.2.23-alpha": {Data: []byte("")},
"terraform_1.1.1": {Data: []byte("")}, "terraform_1.1.1": {Data: []byte("")},
"terraform_1.1.2": {Data: []byte("")}, "terraform_1.1.2": {Data: []byte("")},
"terraform_1.1.3": {Data: []byte("")}, "terraform_1.1.3": {Data: []byte("")},

View File

@ -7,9 +7,13 @@ import (
"strings" "strings"
) )
var needsStable bool
//todo this should be (Version) GetVers... //todo this should be (Version) GetVers...
func GetVersionFromFile(fileSystem fs.FS, versionList []string) (*Version, error) { func GetVersionFromFile(fileSystem fs.FS, versionList []string, needsStableValue bool) (*Version, error) {
needsStable = needsStableValue
var versionFinal Version var versionFinal Version
versionFinal = *NewVersion(">= 0.0.0", versionList)
dir, err := fs.ReadDir(fileSystem, ".") dir, err := fs.ReadDir(fileSystem, ".")
if err != nil { if err != nil {
return &versionFinal, err return &versionFinal, err

View File

@ -5,7 +5,6 @@ import (
"testing/fstest" "testing/fstest"
) )
func TestFileHandler(t *testing.T) {
const ( const (
firstFile = ` firstFile = `
resource "aws_mq_broker" "sample" { resource "aws_mq_broker" "sample" {
@ -28,6 +27,7 @@ terraform {
` `
) )
func TestFileHandler(t *testing.T) {
want := NewVersion("0.12.31", testVersionList()) want := NewVersion("0.12.31", testVersionList())
fs := fstest.MapFS{ fs := fstest.MapFS{
@ -35,7 +35,25 @@ terraform {
"versions.tf": {Data: []byte(secondFile)}, "versions.tf": {Data: []byte(secondFile)},
} }
version, err := GetVersionFromFile(fs, testVersionList()) version, err := GetVersionFromFile(fs, testVersionList(), true)
if err != nil {
t.Fatal(err)
}
got := *version
if got.Version != want.Version {
t.Errorf("Expected %v, got %v", want.Version, got.Version)
}
}
func TestEmptyTerraformVersion(t *testing.T) {
want := NewVersion("1.1.11", testVersionList())
fs := fstest.MapFS{"main.tf": {Data: []byte(firstFile)}}
version, err := GetVersionFromFile(fs, testVersionList(), true)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)

View File

@ -40,7 +40,8 @@ func (v *Version) getLatestMajorRelease() {
for _, release := range v.availableVersions { for _, release := range v.availableVersions {
if release.majorVersion == v.Version.majorVersion && if release.majorVersion == v.Version.majorVersion &&
release.minorVersion == v.Version.minorVersion && release.minorVersion == v.Version.minorVersion &&
release.patchVersion >= v.Version.patchVersion { release.patchVersion >= v.Version.patchVersion &&
(v.Version.isStable || !needsStable) {
v.Version = release v.Version = release
} }
} }
@ -73,16 +74,19 @@ func (v *Version) getOneLessRelease() {
func (v *Version) getLatestRelease() { func (v *Version) getLatestRelease() {
//todo clean up //todo clean up
for _, release := range v.availableVersions { for _, release := range v.availableVersions {
if release.majorVersion > v.Version.majorVersion { if release.majorVersion > v.Version.majorVersion &&
(release.isStable || !needsStable) {
v.Version = release v.Version = release
} }
if release.majorVersion >= v.Version.majorVersion && if release.majorVersion >= v.Version.majorVersion &&
release.minorVersion > v.Version.minorVersion { release.minorVersion > v.Version.minorVersion &&
(release.isStable || !needsStable) {
v.Version = release v.Version = release
} }
if release.majorVersion >= v.Version.majorVersion && if release.majorVersion >= v.Version.majorVersion &&
release.minorVersion >= v.Version.minorVersion && release.minorVersion >= v.Version.minorVersion &&
release.patchVersion >= v.Version.patchVersion { release.patchVersion >= v.Version.patchVersion &&
(release.isStable || !needsStable) {
v.Version = release v.Version = release
} }
} }

View File

@ -6,6 +6,7 @@ import (
func testVersionList() []string { func testVersionList() []string {
return []string{ return []string{
"1.2.23-alpha",
"1.1.11", "1.1.11",
"1.1.10", "1.1.10",
"1.1.9", "1.1.9",