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:
parent
aed9e15f6e
commit
98493c8d52
@ -14,7 +14,12 @@ download, and execute that version
|
||||
All arguments are passed through to terraform
|
||||
```
|
||||
|
||||
## sample usage
|
||||
## Sample usage
|
||||
`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
|
||||
|
@ -16,6 +16,7 @@ const (
|
||||
|
||||
type SemVersion struct {
|
||||
version string
|
||||
isStable bool
|
||||
majorVersion int
|
||||
minorVersion int
|
||||
patchVersion int
|
||||
@ -29,6 +30,7 @@ type SemVersionInterface interface {
|
||||
|
||||
func NewSemVersion(v string) *SemVersion {
|
||||
s := new(SemVersion)
|
||||
s.isStable = true
|
||||
s.version = removeSpacesVersion(v)
|
||||
|
||||
s.setMajorVersion()
|
||||
@ -52,13 +54,18 @@ func (s *SemVersion) setMinorVersion() {
|
||||
|
||||
func (s *SemVersion) setPatchVersion() {
|
||||
version := s.version
|
||||
var err error
|
||||
patchStringSlice := strings.Split(version, ".")
|
||||
if len(patchStringSlice) < 3 {
|
||||
s.patchVersion = 0
|
||||
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 {
|
||||
|
@ -15,6 +15,8 @@ const (
|
||||
terraformPrefix = "/terraform_"
|
||||
)
|
||||
|
||||
var needsStable = true
|
||||
|
||||
func main() {
|
||||
homeDir, _ := os.UserHomeDir()
|
||||
configDirString := homeDir + shortConfigDirString
|
||||
@ -69,7 +71,12 @@ func main() {
|
||||
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 {
|
||||
fmt.Printf("Unable to retrieve terraform version from files: %v", err)
|
||||
}
|
||||
|
@ -12,10 +12,33 @@ import (
|
||||
)
|
||||
|
||||
type configStruct struct {
|
||||
StableOnly bool
|
||||
LastUpdate int64
|
||||
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) {
|
||||
//todo this is used a lot abstract it?
|
||||
fileHandle, err := fileSystem.Open(availableVersions)
|
||||
@ -120,6 +143,8 @@ func CreateConfig(directory string, configFile string) error {
|
||||
fileHandler, err := os.Create(configFileName)
|
||||
defer fileHandler.Close()
|
||||
|
||||
lineToByte := []byte(fmt.Sprintf("StableOnly: true\n"))
|
||||
fileHandler.Write(lineToByte)
|
||||
err = UpdateConfig(*fileHandler)
|
||||
return err
|
||||
}
|
||||
|
@ -85,6 +85,7 @@ func TestInstalledVersions(t *testing.T) {
|
||||
"terraform_0.11.15": {Data: []byte("")},
|
||||
"terraform_1.0.1": {Data: []byte("")},
|
||||
"terraform_1.0.12": {Data: []byte("")},
|
||||
"terraform_1.2.23-alpha": {Data: []byte("")},
|
||||
"terraform_1.1.1": {Data: []byte("")},
|
||||
"terraform_1.1.2": {Data: []byte("")},
|
||||
"terraform_1.1.3": {Data: []byte("")},
|
||||
|
@ -7,9 +7,13 @@ import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
var needsStable bool
|
||||
|
||||
//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
|
||||
versionFinal = *NewVersion(">= 0.0.0", versionList)
|
||||
dir, err := fs.ReadDir(fileSystem, ".")
|
||||
if err != nil {
|
||||
return &versionFinal, err
|
||||
|
@ -5,7 +5,6 @@ import (
|
||||
"testing/fstest"
|
||||
)
|
||||
|
||||
func TestFileHandler(t *testing.T) {
|
||||
const (
|
||||
firstFile = `
|
||||
resource "aws_mq_broker" "sample" {
|
||||
@ -28,6 +27,7 @@ terraform {
|
||||
`
|
||||
)
|
||||
|
||||
func TestFileHandler(t *testing.T) {
|
||||
want := NewVersion("0.12.31", testVersionList())
|
||||
|
||||
fs := fstest.MapFS{
|
||||
@ -35,7 +35,25 @@ terraform {
|
||||
"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 {
|
||||
t.Fatal(err)
|
||||
|
@ -40,7 +40,8 @@ func (v *Version) getLatestMajorRelease() {
|
||||
for _, release := range v.availableVersions {
|
||||
if release.majorVersion == v.Version.majorVersion &&
|
||||
release.minorVersion == v.Version.minorVersion &&
|
||||
release.patchVersion >= v.Version.patchVersion {
|
||||
release.patchVersion >= v.Version.patchVersion &&
|
||||
(v.Version.isStable || !needsStable) {
|
||||
v.Version = release
|
||||
}
|
||||
}
|
||||
@ -73,16 +74,19 @@ func (v *Version) getOneLessRelease() {
|
||||
func (v *Version) getLatestRelease() {
|
||||
//todo clean up
|
||||
for _, release := range v.availableVersions {
|
||||
if release.majorVersion > v.Version.majorVersion {
|
||||
if release.majorVersion > v.Version.majorVersion &&
|
||||
(release.isStable || !needsStable) {
|
||||
v.Version = release
|
||||
}
|
||||
if release.majorVersion >= v.Version.majorVersion &&
|
||||
release.minorVersion > v.Version.minorVersion {
|
||||
release.minorVersion > v.Version.minorVersion &&
|
||||
(release.isStable || !needsStable) {
|
||||
v.Version = release
|
||||
}
|
||||
if release.majorVersion >= v.Version.majorVersion &&
|
||||
release.minorVersion >= v.Version.minorVersion &&
|
||||
release.patchVersion >= v.Version.patchVersion {
|
||||
release.patchVersion >= v.Version.patchVersion &&
|
||||
(release.isStable || !needsStable) {
|
||||
v.Version = release
|
||||
}
|
||||
}
|
||||
|
@ -6,6 +6,7 @@ import (
|
||||
|
||||
func testVersionList() []string {
|
||||
return []string{
|
||||
"1.2.23-alpha",
|
||||
"1.1.11",
|
||||
"1.1.10",
|
||||
"1.1.9",
|
||||
|
Loading…
Reference in New Issue
Block a user