Compare commits
25 Commits
Author | SHA1 | Date | |
---|---|---|---|
fe80f029b5 | |||
15742e8f3f | |||
5251c15271 | |||
2d3d1f881e | |||
cde1201da5 | |||
6df08053c6 | |||
8690a026b1 | |||
a7cb1f8aa1 | |||
1bc2fddc09 | |||
4ffb4abbcc | |||
b4c95f7ce3 | |||
727001e838 | |||
9001410471 | |||
af2855a18c | |||
c0bacf3d52 | |||
b02e04e27b | |||
5cfcabad4b | |||
4c99d54c51 | |||
55da2682ef | |||
2282560e7a | |||
|
ce4d829184 | ||
|
98493c8d52 | ||
|
aed9e15f6e | ||
|
3dc30db64c | ||
|
ae2805a729 |
49
.circleci/config.yml
Normal file
49
.circleci/config.yml
Normal file
@ -0,0 +1,49 @@
|
|||||||
|
# Use the latest 2.1 version of CircleCI pipeline process engine.
|
||||||
|
# See: https://circleci.com/docs/2.0/configuration-reference
|
||||||
|
version: 2.1
|
||||||
|
|
||||||
|
# Define a job to be invoked later in a workflow.
|
||||||
|
# See: https://circleci.com/docs/2.0/configuration-reference/#jobs
|
||||||
|
jobs:
|
||||||
|
build:
|
||||||
|
resource_class: small
|
||||||
|
working_directory: ~/repo
|
||||||
|
# Specify the execution environment. You can specify an image from Dockerhub or use one of our Convenience Images from CircleCI's Developer Hub.
|
||||||
|
# See: https://circleci.com/docs/2.0/configuration-reference/#docker-machine-macos-windows-executor
|
||||||
|
docker:
|
||||||
|
- image: cimg/go:1.17
|
||||||
|
# Add steps to the job
|
||||||
|
# See: https://circleci.com/docs/2.0/configuration-reference/#steps
|
||||||
|
steps:
|
||||||
|
- checkout
|
||||||
|
- restore_cache:
|
||||||
|
keys:
|
||||||
|
- go-mod-v4-{{ checksum "go.sum" }}
|
||||||
|
- run:
|
||||||
|
name: Install Dependencies
|
||||||
|
command: go mod download
|
||||||
|
- save_cache:
|
||||||
|
key: go-mod-v4-{{ checksum "go.sum" }}
|
||||||
|
paths:
|
||||||
|
- "/go/pkg/mod"
|
||||||
|
- run:
|
||||||
|
name: Run tests
|
||||||
|
command: |
|
||||||
|
mkdir -p /tmp/test-reports
|
||||||
|
mkdir -p /tmp/artifacts
|
||||||
|
go test -coverprofile=c.out
|
||||||
|
go tool cover -html=c.out -o coverage.html
|
||||||
|
mv coverage.html /tmp/artifacts
|
||||||
|
gotestsum --junitfile /tmp/test-reports/unit-tests.xml
|
||||||
|
- store_test_results:
|
||||||
|
path: /tmp/test-reports
|
||||||
|
- store_artifacts:
|
||||||
|
path: /tmp/artifacts
|
||||||
|
|
||||||
|
# Invoke jobs via workflows
|
||||||
|
# See: https://circleci.com/docs/2.0/configuration-reference/#workflows
|
||||||
|
workflows:
|
||||||
|
build: # This is the name of the workflow, feel free to change it to better match your workflow.
|
||||||
|
# Inside the workflow, you define the jobs you want to run.
|
||||||
|
jobs:
|
||||||
|
- build
|
6
Makefile
6
Makefile
@ -7,12 +7,12 @@ build:
|
|||||||
# Clean
|
# Clean
|
||||||
####################################################
|
####################################################
|
||||||
clean:
|
clean:
|
||||||
rm -f ~/.local/bin/versionedTerraform
|
rm -f $(shell go env GOPATH)/bin/versionedTerraform
|
||||||
####################################################
|
####################################################
|
||||||
# Install
|
# Install
|
||||||
####################################################
|
####################################################
|
||||||
install:
|
install:
|
||||||
mv versionedTerraform ~/.local/bin/
|
mv versionedTerraform $(shell go env GOPATH)/bin/
|
||||||
####################################################
|
####################################################
|
||||||
# help feature
|
# help feature
|
||||||
####################################################
|
####################################################
|
||||||
@ -22,6 +22,6 @@ help:
|
|||||||
@echo 'Targets:'
|
@echo 'Targets:'
|
||||||
@echo ' build go build -o versionedTerraform ./cmd'
|
@echo ' build go build -o versionedTerraform ./cmd'
|
||||||
@echo ' clean removes installed versionedTerraform file'
|
@echo ' clean removes installed versionedTerraform file'
|
||||||
@echo ' install installs versionedTerraform to local user bin folder'
|
@echo ' install installs versionedTerraform to bin folder in GOPATH'
|
||||||
@echo ' all Nothing to do.'
|
@echo ' all Nothing to do.'
|
||||||
@echo ''
|
@echo ''
|
15
README.md
15
README.md
@ -1,9 +1,11 @@
|
|||||||
#Versioned Terraform
|
[![<ORG_NAME>](https://circleci.com/gh/mitch-thompson/versionedTerraform.svg?style=svg)](<LINK>)
|
||||||
|
|
||||||
|
# Versioned Terraform
|
||||||
A wrapper for terraform to detect the expected version of terraform,
|
A wrapper for terraform to detect the expected version of terraform,
|
||||||
download, and execute that version
|
download, and execute that version
|
||||||
|
|
||||||
## Requirements
|
## Requirements
|
||||||
- go compiler (only tested on go1.17)
|
- go
|
||||||
|
|
||||||
## Install
|
## Install
|
||||||
`make build install` for installation to local user<br>
|
`make build install` for installation to local user<br>
|
||||||
@ -14,9 +16,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
|
||||||
Currently, does not support semantic versioning between values<br>
|
|
||||||
i.e. `required_version = "~> 0.14", "< 0.14.3"`
|
|
||||||
|
@ -10,10 +10,13 @@ const (
|
|||||||
//todo add comparison i.e. >= 0.11.10, < 0.12.0
|
//todo add comparison i.e. >= 0.11.10, < 0.12.0
|
||||||
latestRelease = ">="
|
latestRelease = ">="
|
||||||
latestPatch = "~>"
|
latestPatch = "~>"
|
||||||
|
versionLessOrEqual = "<="
|
||||||
|
versionLessThan = "<"
|
||||||
)
|
)
|
||||||
|
|
||||||
type SemVersion struct {
|
type SemVersion struct {
|
||||||
version string
|
version string
|
||||||
|
isStable bool
|
||||||
majorVersion int
|
majorVersion int
|
||||||
minorVersion int
|
minorVersion int
|
||||||
patchVersion int
|
patchVersion int
|
||||||
@ -27,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()
|
||||||
@ -35,12 +39,14 @@ func NewSemVersion(v string) *SemVersion {
|
|||||||
return s
|
return s
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//setMajorVersion setter for SemVersion.majorVersion
|
||||||
func (s *SemVersion) setMajorVersion() {
|
func (s *SemVersion) setMajorVersion() {
|
||||||
version := s.version
|
version := s.version
|
||||||
majorVersionString := strings.Split(version, ".")[0]
|
majorVersionString := strings.Split(version, ".")[0]
|
||||||
s.majorVersion, _ = strconv.Atoi(majorVersionString)
|
s.majorVersion, _ = strconv.Atoi(majorVersionString)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//setMinorVersion setter for SemVersion.minorVersion
|
||||||
func (s *SemVersion) setMinorVersion() {
|
func (s *SemVersion) setMinorVersion() {
|
||||||
version := s.version
|
version := s.version
|
||||||
minorVersionString := strings.Split(version, ".")[1]
|
minorVersionString := strings.Split(version, ".")[1]
|
||||||
@ -48,21 +54,30 @@ func (s *SemVersion) setMinorVersion() {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//setPatchVersion setter for SemVersion.patchVersion
|
||||||
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])
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//ToString returns string of SemVersion
|
||||||
func (s *SemVersion) ToString() string {
|
func (s *SemVersion) ToString() string {
|
||||||
return s.version
|
return s.version
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//VersionInSlice iterates through slices of SemVersion to check if version is in slice
|
||||||
|
//Used by main.go to determine if terraform version is currently installed
|
||||||
func (s *SemVersion) VersionInSlice(sSem []SemVersion) bool {
|
func (s *SemVersion) VersionInSlice(sSem []SemVersion) bool {
|
||||||
for _, ver := range sSem {
|
for _, ver := range sSem {
|
||||||
if ver.ToString() == s.ToString() {
|
if ver.ToString() == s.ToString() {
|
||||||
@ -71,3 +86,50 @@ func (s *SemVersion) VersionInSlice(sSem []SemVersion) bool {
|
|||||||
}
|
}
|
||||||
return false
|
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
|
||||||
|
}
|
||||||
|
773
SemVersion_test.go
Normal file
773
SemVersion_test.go
Normal file
@ -0,0 +1,773 @@
|
|||||||
|
package versionedTerraform
|
||||||
|
|
||||||
|
import "testing"
|
||||||
|
|
||||||
|
func TestSemVersion_VersionInSlice_success(t *testing.T) {
|
||||||
|
want := SemVersion{
|
||||||
|
version: "1.1.9",
|
||||||
|
isStable: true,
|
||||||
|
majorVersion: 1,
|
||||||
|
minorVersion: 1,
|
||||||
|
patchVersion: 9,
|
||||||
|
}
|
||||||
|
ver1 := SemVersion{
|
||||||
|
version: "1.1.10",
|
||||||
|
isStable: true,
|
||||||
|
majorVersion: 1,
|
||||||
|
minorVersion: 1,
|
||||||
|
patchVersion: 10,
|
||||||
|
}
|
||||||
|
|
||||||
|
ver2 := SemVersion{
|
||||||
|
version: "0.1.9",
|
||||||
|
isStable: true,
|
||||||
|
majorVersion: 0,
|
||||||
|
minorVersion: 1,
|
||||||
|
patchVersion: 9,
|
||||||
|
}
|
||||||
|
|
||||||
|
ver3 := SemVersion{
|
||||||
|
version: "1.0.9",
|
||||||
|
isStable: true,
|
||||||
|
majorVersion: 1,
|
||||||
|
minorVersion: 0,
|
||||||
|
patchVersion: 9,
|
||||||
|
}
|
||||||
|
|
||||||
|
var semArray []SemVersion
|
||||||
|
semArray = append(semArray, ver1)
|
||||||
|
semArray = append(semArray, ver2)
|
||||||
|
semArray = append(semArray, ver3)
|
||||||
|
semArray = append(semArray, want)
|
||||||
|
|
||||||
|
if !want.VersionInSlice(semArray) {
|
||||||
|
t.Errorf("Expected Sem Version to be found in semArray")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSemVersion_VersionInSlice_fail(t *testing.T) {
|
||||||
|
want := SemVersion{
|
||||||
|
version: "1.1.9",
|
||||||
|
isStable: true,
|
||||||
|
majorVersion: 1,
|
||||||
|
minorVersion: 1,
|
||||||
|
patchVersion: 9,
|
||||||
|
}
|
||||||
|
ver1 := SemVersion{
|
||||||
|
version: "1.1.10",
|
||||||
|
isStable: true,
|
||||||
|
majorVersion: 1,
|
||||||
|
minorVersion: 1,
|
||||||
|
patchVersion: 10,
|
||||||
|
}
|
||||||
|
|
||||||
|
ver2 := SemVersion{
|
||||||
|
version: "0.1.9",
|
||||||
|
isStable: true,
|
||||||
|
majorVersion: 0,
|
||||||
|
minorVersion: 1,
|
||||||
|
patchVersion: 9,
|
||||||
|
}
|
||||||
|
|
||||||
|
ver3 := SemVersion{
|
||||||
|
version: "1.0.9",
|
||||||
|
isStable: true,
|
||||||
|
majorVersion: 1,
|
||||||
|
minorVersion: 0,
|
||||||
|
patchVersion: 9,
|
||||||
|
}
|
||||||
|
|
||||||
|
var semArray []SemVersion
|
||||||
|
semArray = append(semArray, ver1)
|
||||||
|
semArray = append(semArray, ver2)
|
||||||
|
semArray = append(semArray, ver3)
|
||||||
|
|
||||||
|
if want.VersionInSlice(semArray) {
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
33
cmd/main.go
33
cmd/main.go
@ -15,20 +15,25 @@ const (
|
|||||||
terraformPrefix = "/terraform_"
|
terraformPrefix = "/terraform_"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var needsStable = true
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
homeDir, _ := os.UserHomeDir()
|
homeDir, _ := os.UserHomeDir()
|
||||||
configDirString := homeDir + shortConfigDirString
|
configDirString := homeDir + shortConfigDirString
|
||||||
|
|
||||||
|
// Create configuration directory if it does not exist
|
||||||
_, err := os.Stat(configDirString)
|
_, err := os.Stat(configDirString)
|
||||||
if os.IsNotExist(err) {
|
if os.IsNotExist(err) {
|
||||||
err = versionedTerraform.CreateConfig(configDirString, configFileLocation)
|
err = versionedTerraform.CreateConfig(configDirString, configFileLocation)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Create configuration file if it does not exist
|
||||||
_, err = os.Stat(configDirString + "/" + configFileLocation)
|
_, err = os.Stat(configDirString + "/" + configFileLocation)
|
||||||
if os.IsNotExist(err) {
|
if os.IsNotExist(err) {
|
||||||
err = versionedTerraform.CreateConfig(configDirString, configFileLocation)
|
err = versionedTerraform.CreateConfig(configDirString, configFileLocation)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Let the user know if we couldn't create the config directory or file
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("Unable to create config directory: %v", err)
|
fmt.Printf("Unable to create config directory: %v", err)
|
||||||
}
|
}
|
||||||
@ -40,24 +45,28 @@ func main() {
|
|||||||
flag.Parse()
|
flag.Parse()
|
||||||
args := flag.Args()
|
args := flag.Args()
|
||||||
|
|
||||||
|
//Load available versions from configuration file
|
||||||
versionsFromConfig, err = versionedTerraform.LoadVersionsFromConfig(configDir, configFileLocation)
|
versionsFromConfig, err = versionedTerraform.LoadVersionsFromConfig(configDir, configFileLocation)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("Unable to read config: %v", err)
|
fmt.Printf("Unable to read config: %v\n", err)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//Check if we need to update available versions with terraform's website
|
||||||
|
//Then update configuration if we do
|
||||||
|
//todo move this above loading the config
|
||||||
needsUpdate, err := versionedTerraform.NeedToUpdateAvailableVersions(configDir, configFileLocation)
|
needsUpdate, err := versionedTerraform.NeedToUpdateAvailableVersions(configDir, configFileLocation)
|
||||||
if os.ErrNotExist == err {
|
if os.ErrNotExist == err {
|
||||||
fmt.Println("Unable to update version: %v", err)
|
fmt.Printf("Unable to update version: %v\n", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if needsUpdate {
|
|
||||||
fileHandle, _ := os.OpenFile(configDirString+"/"+configFileLocation, os.O_RDWR, 0666)
|
fileHandle, _ := os.OpenFile(configDirString+"/"+configFileLocation, os.O_RDWR, 0666)
|
||||||
defer fileHandle.Close()
|
defer fileHandle.Close()
|
||||||
|
if needsUpdate {
|
||||||
versionedTerraform.UpdateConfig(*fileHandle)
|
versionedTerraform.UpdateConfig(*fileHandle)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Load a slice of versions which have already been installed
|
||||||
installedVersions, err := versionedTerraform.LoadInstalledVersions(configDir)
|
installedVersions, err := versionedTerraform.LoadInstalledVersions(configDir)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("Unable to verify installed verisons: %v", err)
|
fmt.Printf("Unable to verify installed verisons: %v", err)
|
||||||
@ -69,7 +78,14 @@ func main() {
|
|||||||
vSlice = append(vSlice, v.ToString())
|
vSlice = append(vSlice, v.ToString())
|
||||||
}
|
}
|
||||||
|
|
||||||
ver, err := versionedTerraform.GetVersionFromFile(workingDir, vSlice)
|
// Check if stable version of terraform is required
|
||||||
|
needsStable, err = versionedTerraform.ConfigRequiresStable(*fileHandle)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Fprintf(os.Stderr, "Unable to open config file, defaulting to stable versions of terraform only")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Load version required from terraform directory
|
||||||
|
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)
|
||||||
}
|
}
|
||||||
@ -82,6 +98,7 @@ func main() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Execute terraform
|
||||||
terraformFile := configDirString + terraformPrefix + ver.VersionToString()
|
terraformFile := configDirString + terraformPrefix + ver.VersionToString()
|
||||||
argsForTerraform := append([]string{""}, args...)
|
argsForTerraform := append([]string{""}, args...)
|
||||||
cmd := exec.Cmd{
|
cmd := exec.Cmd{
|
||||||
@ -93,5 +110,9 @@ func main() {
|
|||||||
Stdout: os.Stdout,
|
Stdout: os.Stdout,
|
||||||
Stderr: os.Stderr,
|
Stderr: os.Stderr,
|
||||||
}
|
}
|
||||||
cmd.Run()
|
if err := cmd.Run(); err != nil {
|
||||||
|
if exitError, ok := err.(*exec.ExitError); ok {
|
||||||
|
os.Exit(exitError.ExitCode())
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -12,10 +12,36 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type configStruct struct {
|
type configStruct struct {
|
||||||
|
StableOnly bool
|
||||||
LastUpdate int64
|
LastUpdate int64
|
||||||
AvailableVersions []string
|
AvailableVersions []string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//ConfigRequiresStable returns bool, error only false if StableOnly: false is set in configuration file
|
||||||
|
func ConfigRequiresStable(File os.File) (bool, error) {
|
||||||
|
fileHandle, err := os.Open(File.Name())
|
||||||
|
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
|
||||||
|
}
|
||||||
|
|
||||||
|
//NeedToUpdateAvailableVersions returns bool, error checks if last update was older than 1 day ago
|
||||||
|
// this prevents us from spamming the list of available terraform versions page
|
||||||
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)
|
||||||
@ -45,6 +71,8 @@ func NeedToUpdateAvailableVersions(fileSystem fs.FS, availableVersions string) (
|
|||||||
return false, nil
|
return false, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//LoadVersionsFromConfig returns slice of SemVersions and an error from AvailableVersions in configuration file
|
||||||
|
//This is stored from GetVersionList()
|
||||||
func LoadVersionsFromConfig(fileSystem fs.FS, configFile string) ([]SemVersion, error) {
|
func LoadVersionsFromConfig(fileSystem fs.FS, configFile string) ([]SemVersion, error) {
|
||||||
fileHandle, err := fileSystem.Open(configFile)
|
fileHandle, err := fileSystem.Open(configFile)
|
||||||
removeOpenBracket := regexp.MustCompile("\\[")
|
removeOpenBracket := regexp.MustCompile("\\[")
|
||||||
@ -74,6 +102,7 @@ func LoadVersionsFromConfig(fileSystem fs.FS, configFile string) ([]SemVersion,
|
|||||||
return nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//LoadInstalledVersions returns list of SemVersions and an error from the directory listing of the .versionedTerraform
|
||||||
func LoadInstalledVersions(fileSystem fs.FS) ([]SemVersion, error) {
|
func LoadInstalledVersions(fileSystem fs.FS) ([]SemVersion, error) {
|
||||||
dir, err := fs.ReadDir(fileSystem, ".")
|
dir, err := fs.ReadDir(fileSystem, ".")
|
||||||
var installedTerraformVersions []SemVersion
|
var installedTerraformVersions []SemVersion
|
||||||
@ -92,24 +121,39 @@ func LoadInstalledVersions(fileSystem fs.FS) ([]SemVersion, error) {
|
|||||||
return installedTerraformVersions, nil
|
return installedTerraformVersions, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func UpdateConfig(File os.File) error {
|
//UpdateConfig returns an error, and updates configuration file
|
||||||
|
// adding:
|
||||||
|
// a new date to the last updated field
|
||||||
|
// the available versions listed on terraforms website
|
||||||
|
// the status of if the user wants only stable releases
|
||||||
|
func UpdateConfig(File os.File, timeNow ...time.Time) error {
|
||||||
configValues := new(configStruct)
|
configValues := new(configStruct)
|
||||||
|
|
||||||
configValues.AvailableVersions, _ = GetVersionList()
|
configValues.AvailableVersions, _ = GetVersionList()
|
||||||
|
configValues.StableOnly, _ = ConfigRequiresStable(File)
|
||||||
|
|
||||||
timeNow := time.Now()
|
var t time.Time
|
||||||
configValues.LastUpdate = timeNow.Unix()
|
if len(timeNow) > 0 {
|
||||||
|
t = timeNow[0]
|
||||||
|
} else {
|
||||||
|
t = time.Now()
|
||||||
|
}
|
||||||
|
configValues.LastUpdate = t.Unix()
|
||||||
|
|
||||||
File.Truncate(0)
|
File.Truncate(0)
|
||||||
File.Seek(0, 0)
|
File.Seek(0, 0)
|
||||||
|
|
||||||
lineToByte := []byte(fmt.Sprintf("LastUpdate: %d\n", configValues.LastUpdate))
|
lineToByte := []byte(fmt.Sprintf("StableOnly: %+v\n", configValues.StableOnly))
|
||||||
|
File.Write(lineToByte)
|
||||||
|
|
||||||
|
lineToByte = []byte(fmt.Sprintf("LastUpdate: %d\n", configValues.LastUpdate))
|
||||||
File.Write(lineToByte)
|
File.Write(lineToByte)
|
||||||
lineToByte = []byte(fmt.Sprintf("AvailableVersions: %+v\n", configValues.AvailableVersions))
|
lineToByte = []byte(fmt.Sprintf("AvailableVersions: %+v\n", configValues.AvailableVersions))
|
||||||
File.Write(lineToByte)
|
File.Write(lineToByte)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//CreateConfig returns error, creates a new configuration file
|
||||||
func CreateConfig(directory string, configFile string) error {
|
func CreateConfig(directory string, configFile string) error {
|
||||||
configFileName := directory + "/" + configFile
|
configFileName := directory + "/" + configFile
|
||||||
err := os.MkdirAll(directory, 0755)
|
err := os.MkdirAll(directory, 0755)
|
||||||
@ -120,6 +164,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
|
||||||
}
|
}
|
||||||
|
@ -2,8 +2,11 @@ package versionedTerraform
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"os"
|
||||||
"reflect"
|
"reflect"
|
||||||
"sort"
|
"sort"
|
||||||
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
"testing/fstest"
|
"testing/fstest"
|
||||||
"time"
|
"time"
|
||||||
@ -23,26 +26,26 @@ func TestUpdateAvailableVersions(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
t.Run("Test success last update time", func(t *testing.T) {
|
t.Run("Test success last update time", func(t *testing.T) {
|
||||||
want := true
|
want := false
|
||||||
got, err := NeedToUpdateAvailableVersions(fs, "successConfig.conf")
|
got, err := NeedToUpdateAvailableVersions(fs, "successConfig.conf")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if got != want {
|
if got != want {
|
||||||
t.Errorf("updateAvailableVersions had incorrect output expected %v got %v", got, want)
|
t.Errorf("updateAvailableVersions had incorrect output expected %v got %v", want, got)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
t.Run("Test failed last update time", func(t *testing.T) {
|
t.Run("Test failed last update time", func(t *testing.T) {
|
||||||
want := false
|
want := true
|
||||||
got, err := NeedToUpdateAvailableVersions(fs, "failConfig.conf")
|
got, err := NeedToUpdateAvailableVersions(fs, "failConfig.conf")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if got != want {
|
if got != want {
|
||||||
t.Errorf("updateAvailableVersions had incorrect output expected %v got %v", got, want)
|
t.Errorf("updateAvailableVersions had incorrect output expected %v got %v", want, got)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@ -85,6 +88,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("")},
|
||||||
@ -96,6 +100,9 @@ func TestInstalledVersions(t *testing.T) {
|
|||||||
"terraform_1.1.9": {Data: []byte("")},
|
"terraform_1.1.9": {Data: []byte("")},
|
||||||
"terraform_1.1.10": {Data: []byte("")},
|
"terraform_1.1.10": {Data: []byte("")},
|
||||||
"terraform_1.1.11": {Data: []byte("")},
|
"terraform_1.1.11": {Data: []byte("")},
|
||||||
|
"terraform_0.14.0": {Data: []byte("")},
|
||||||
|
"terraform_0.13.1": {Data: []byte("")},
|
||||||
|
"terraform_0.13.0": {Data: []byte("")},
|
||||||
}
|
}
|
||||||
|
|
||||||
t.Run("Test installed versions", func(t *testing.T) {
|
t.Run("Test installed versions", func(t *testing.T) {
|
||||||
@ -110,3 +117,73 @@ func TestInstalledVersions(t *testing.T) {
|
|||||||
})
|
})
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestConfigRequiresStable(t *testing.T) {
|
||||||
|
availableVersions, _ := GetVersionList()
|
||||||
|
versions := strings.Join(availableVersions, " ")
|
||||||
|
cases := []struct {
|
||||||
|
name, content, want string
|
||||||
|
timeNow time.Time
|
||||||
|
}{
|
||||||
|
{"StableOnly True", "StableOnly: true\n" +
|
||||||
|
"LastUpdate: 1674481203\n" +
|
||||||
|
"AvailableVersions: [1.3.7]",
|
||||||
|
"StableOnly: true\n" +
|
||||||
|
"LastUpdate: 1286705410\n" +
|
||||||
|
"AvailableVersions: [" +
|
||||||
|
versions + "]\n",
|
||||||
|
time.Date(2010, 10, 10, 10, 10, 10, 10, time.UTC)},
|
||||||
|
{"StableOnly False", "StableOnly: false\n" +
|
||||||
|
"LastUpdate: 1674481203\n" +
|
||||||
|
"AvailableVersions: [1.3.7]",
|
||||||
|
"StableOnly: false\n" +
|
||||||
|
"LastUpdate: 1286705410\n" +
|
||||||
|
"AvailableVersions: [" +
|
||||||
|
versions + "]\n",
|
||||||
|
time.Date(2010, 10, 10, 10, 10, 10, 10, time.UTC)},
|
||||||
|
{"StableOnly not found", "LastUpdate: 1674481203\n" +
|
||||||
|
"AvailableVersions: [1.3.7]",
|
||||||
|
"StableOnly: true\n" +
|
||||||
|
"LastUpdate: 1286705410\n" +
|
||||||
|
"AvailableVersions: [" +
|
||||||
|
versions + "]\n",
|
||||||
|
time.Date(2010, 10, 10, 10, 10, 10, 10, time.UTC)},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, c := range cases {
|
||||||
|
t.Run("Test: "+c.name, func(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
tempDir := os.TempDir()
|
||||||
|
tempFile, err := os.Create(tempDir + "/config")
|
||||||
|
defer tempFile.Close()
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("Unable to execute test : %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
UpdateConfig(*tempFile, c.timeNow)
|
||||||
|
|
||||||
|
tempFile.Seek(0, 0)
|
||||||
|
data := make([]byte, 1024)
|
||||||
|
var got string
|
||||||
|
for {
|
||||||
|
n, err := tempFile.Read(data)
|
||||||
|
if err == io.EOF {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("File reading error : %v", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
got += string(data[:n])
|
||||||
|
}
|
||||||
|
|
||||||
|
if !reflect.DeepEqual(got, c.want) {
|
||||||
|
t.Errorf("%v test failed to meet conditions", c.name)
|
||||||
|
fmt.Fprintf(os.Stdout, "%v\n", c.want)
|
||||||
|
fmt.Fprintf(os.Stdout, "%v\n", got)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -5,4 +5,6 @@ package versionedTerraform
|
|||||||
|
|
||||||
const (
|
const (
|
||||||
fileSuffix = "_darwin_amd64.zip"
|
fileSuffix = "_darwin_amd64.zip"
|
||||||
|
minVersion = "0.0.0"
|
||||||
|
alternateSuffix = ""
|
||||||
)
|
)
|
||||||
|
@ -5,4 +5,6 @@ package versionedTerraform
|
|||||||
|
|
||||||
const (
|
const (
|
||||||
fileSuffix = "_darwin_arm64.zip"
|
fileSuffix = "_darwin_arm64.zip"
|
||||||
|
minVersion = "1.0.2"
|
||||||
|
alternateSuffix = "_darwin_amd64.zip"
|
||||||
)
|
)
|
||||||
|
@ -7,9 +7,15 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var needsStable bool
|
||||||
|
|
||||||
|
//GetVersionFromFile returns Version pointer and error
|
||||||
|
//Iterates through files in current directory and sending to parseVersionFromFile
|
||||||
//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
|
||||||
@ -29,6 +35,9 @@ func GetVersionFromFile(fileSystem fs.FS, versionList []string) (*Version, error
|
|||||||
}
|
}
|
||||||
|
|
||||||
//todo same here
|
//todo same here
|
||||||
|
//parseVersionFromFile returns Version pointer, bool, and error
|
||||||
|
//bool returns true if required_version is found
|
||||||
|
//if required_version is found sends to NewVersion to determine required version for the application
|
||||||
func parseVersionFromFile(f fs.FS, fileName string, versionList []string) (*Version, bool, error) {
|
func parseVersionFromFile(f fs.FS, fileName string, versionList []string) (*Version, bool, error) {
|
||||||
fileHandle, err := f.Open(fileName)
|
fileHandle, err := f.Open(fileName)
|
||||||
regex := regexp.MustCompile("required_version\\s+?=")
|
regex := regexp.MustCompile("required_version\\s+?=")
|
||||||
|
@ -5,8 +5,7 @@ import (
|
|||||||
"testing/fstest"
|
"testing/fstest"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestFileHandler(t *testing.T) {
|
const (
|
||||||
const (
|
|
||||||
firstFile = `
|
firstFile = `
|
||||||
resource "aws_mq_broker" "sample" {
|
resource "aws_mq_broker" "sample" {
|
||||||
depends_on = [aws_security_group.mq]
|
depends_on = [aws_security_group.mq]
|
||||||
@ -26,8 +25,9 @@ terraform {
|
|||||||
required_version = "~> 0.12.4"
|
required_version = "~> 0.12.4"
|
||||||
}
|
}
|
||||||
`
|
`
|
||||||
)
|
)
|
||||||
|
|
||||||
|
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)
|
||||||
|
@ -5,4 +5,6 @@ package versionedTerraform
|
|||||||
|
|
||||||
const (
|
const (
|
||||||
fileSuffix = "_freebsd_386.zip"
|
fileSuffix = "_freebsd_386.zip"
|
||||||
|
minVersion = "0.0.0"
|
||||||
|
alternateSuffix = ""
|
||||||
)
|
)
|
||||||
|
@ -5,4 +5,6 @@ package versionedTerraform
|
|||||||
|
|
||||||
const (
|
const (
|
||||||
fileSuffix = "_freebsd_amd64.zip"
|
fileSuffix = "_freebsd_amd64.zip"
|
||||||
|
minVersion = "0.0.0"
|
||||||
|
alternateSuffix = ""
|
||||||
)
|
)
|
||||||
|
@ -5,4 +5,6 @@ package versionedTerraform
|
|||||||
|
|
||||||
const (
|
const (
|
||||||
fileSuffix = "_freebsd_arm.zip"
|
fileSuffix = "_freebsd_arm.zip"
|
||||||
|
minVersion = "0.0.0"
|
||||||
|
alternateSuffix = ""
|
||||||
)
|
)
|
||||||
|
@ -5,4 +5,6 @@ package versionedTerraform
|
|||||||
|
|
||||||
const (
|
const (
|
||||||
fileSuffix = "_linux_386.zip"
|
fileSuffix = "_linux_386.zip"
|
||||||
|
minVersion = "0.0.0"
|
||||||
|
alternateSuffix = ""
|
||||||
)
|
)
|
||||||
|
@ -5,4 +5,6 @@ package versionedTerraform
|
|||||||
|
|
||||||
const (
|
const (
|
||||||
fileSuffix = "_linux_amd64.zip"
|
fileSuffix = "_linux_amd64.zip"
|
||||||
|
minVersion = "0.0.0"
|
||||||
|
alternateSuffix = ""
|
||||||
)
|
)
|
||||||
|
@ -5,4 +5,6 @@ package versionedTerraform
|
|||||||
|
|
||||||
const (
|
const (
|
||||||
fileSuffix = "_linux_arm.zip"
|
fileSuffix = "_linux_arm.zip"
|
||||||
|
minVersion = "0.0.0"
|
||||||
|
alternateSuffix = ""
|
||||||
)
|
)
|
||||||
|
@ -5,4 +5,6 @@ package versionedTerraform
|
|||||||
|
|
||||||
const (
|
const (
|
||||||
fileSuffix = "_linux_arm64.zip"
|
fileSuffix = "_linux_arm64.zip"
|
||||||
|
minVersion = "0.0.0"
|
||||||
|
alternateSuffix = ""
|
||||||
)
|
)
|
||||||
|
@ -5,4 +5,6 @@ package versionedTerraform
|
|||||||
|
|
||||||
const (
|
const (
|
||||||
fileSuffix = "_openbsd_386.zip"
|
fileSuffix = "_openbsd_386.zip"
|
||||||
|
minVersion = "0.0.0"
|
||||||
|
alternateSuffix = ""
|
||||||
)
|
)
|
||||||
|
@ -5,4 +5,6 @@ package versionedTerraform
|
|||||||
|
|
||||||
const (
|
const (
|
||||||
fileSuffix = "_openbsd_amd64.zip"
|
fileSuffix = "_openbsd_amd64.zip"
|
||||||
|
minVersion = "0.0.0"
|
||||||
|
alternateSuffix = ""
|
||||||
)
|
)
|
||||||
|
@ -5,4 +5,6 @@ package versionedTerraform
|
|||||||
|
|
||||||
const (
|
const (
|
||||||
fileSuffix = "_solaris_amd64.zip"
|
fileSuffix = "_solaris_amd64.zip"
|
||||||
|
minVersion = "0.0.0"
|
||||||
|
alternateSuffix = ""
|
||||||
)
|
)
|
||||||
|
@ -5,22 +5,15 @@ import (
|
|||||||
"bufio"
|
"bufio"
|
||||||
"bytes"
|
"bytes"
|
||||||
"errors"
|
"errors"
|
||||||
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (*Version) latestMajorVersion() {
|
|
||||||
}
|
|
||||||
|
|
||||||
func (*Version) latestMinorVersion() {
|
|
||||||
}
|
|
||||||
|
|
||||||
func (*Version) latestPatchVersion() {
|
|
||||||
}
|
|
||||||
|
|
||||||
type Version struct {
|
type Version struct {
|
||||||
Version SemVersion
|
Version SemVersion
|
||||||
availableVersions []SemVersion
|
availableVersions []SemVersion
|
||||||
@ -33,95 +26,124 @@ const (
|
|||||||
versionedTerraformFolder = "/.versionedTerraform"
|
versionedTerraformFolder = "/.versionedTerraform"
|
||||||
)
|
)
|
||||||
|
|
||||||
//getLatestMajorRelease() returns the latest major release from Version
|
// getLatestMajorRelease() returns the latest major release from Version
|
||||||
func (v *Version) getLatestMajorRelease() {
|
func (v *Version) getLatestMajorRelease() {
|
||||||
//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.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
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//getLatestRelease returns the latest release from Version
|
// getGreatestRelease() returns less than release
|
||||||
func (v *Version) getLatestRelease() {
|
func (v *Version) getOneLessRelease() {
|
||||||
//todo clean up
|
var vSlice []Version
|
||||||
|
|
||||||
for _, release := range v.availableVersions {
|
for _, release := range v.availableVersions {
|
||||||
if release.majorVersion > v.Version.majorVersion {
|
_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() {
|
||||||
|
for _, release := range v.availableVersions {
|
||||||
|
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
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//InstallTerraformVersion installs the defined terraform Version in the application
|
// InstallTerraformVersion installs the defined terraform Version in the application
|
||||||
//configuration directory
|
// configuration directory
|
||||||
func (v *Version) InstallTerraformVersion() error {
|
func (v *Version) InstallTerraformVersion() error {
|
||||||
homeDir, _ := os.UserHomeDir()
|
homeDir, _ := os.UserHomeDir()
|
||||||
resp, err := http.Get(hashicorpUrl +
|
suffix := fileSuffix
|
||||||
|
minV := NewSemVersion(minVersion)
|
||||||
|
if v.Version.IsLessThan(*minV) {
|
||||||
|
suffix = alternateSuffix
|
||||||
|
}
|
||||||
|
url := hashicorpUrl +
|
||||||
v.Version.ToString() +
|
v.Version.ToString() +
|
||||||
"/" + terraformPrefix +
|
"/" + terraformPrefix +
|
||||||
v.Version.ToString() +
|
v.Version.ToString() +
|
||||||
fileSuffix)
|
suffix
|
||||||
|
|
||||||
|
resp, err := http.Get(url)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return fmt.Errorf("failed to download Terraform: %v", err)
|
||||||
}
|
}
|
||||||
defer resp.Body.Close()
|
defer resp.Body.Close()
|
||||||
|
|
||||||
body, err := ioutil.ReadAll(resp.Body)
|
body, err := ioutil.ReadAll(resp.Body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return fmt.Errorf("failed to read response body: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
zipReader, err := zip.NewReader(bytes.NewReader(body), int64(len(body)))
|
zipReader, err := zip.NewReader(bytes.NewReader(body), int64(len(body)))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return fmt.Errorf("failed to create zip reader: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
versionedFileName := homeDir + versionedTerraformFolder + "/" + terraformPrefix + v.Version.ToString()
|
versionedFileName := homeDir + versionedTerraformFolder + "/" + terraformPrefix + v.Version.ToString()
|
||||||
versionedFile, err := os.OpenFile(versionedFileName, os.O_WRONLY, 0755)
|
versionedFile, err := os.OpenFile(versionedFileName, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0755)
|
||||||
if os.IsNotExist(err) {
|
|
||||||
//_, err = os.Create(versionedFileName)
|
|
||||||
//if err != nil {
|
|
||||||
// return err
|
|
||||||
//}
|
|
||||||
versionedFile, err = os.OpenFile(versionedFileName, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0755)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return fmt.Errorf("failed to create output file: %v", err)
|
||||||
}
|
|
||||||
}
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
defer versionedFile.Close()
|
defer versionedFile.Close()
|
||||||
|
|
||||||
for _, zipFIle := range zipReader.File {
|
for _, zipFile := range zipReader.File {
|
||||||
zr, err := zipFIle.Open()
|
if zipFile.Name != "terraform" {
|
||||||
if err != nil {
|
continue
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
unzippedFileBytes, _ := ioutil.ReadAll(zr)
|
|
||||||
|
|
||||||
_, err = versionedFile.Write(unzippedFileBytes)
|
zr, err := zipFile.Open()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return fmt.Errorf("failed to open zip file: %v", err)
|
||||||
}
|
}
|
||||||
zr.Close()
|
defer zr.Close()
|
||||||
|
|
||||||
|
_, err = io.Copy(versionedFile, zr)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to write terraform binary: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
//NewVersion creates a new Version using sem versioning for determining the
|
// NewVersion creates a new Version using sem versioning for determining the
|
||||||
//latest release
|
// latest release
|
||||||
func NewVersion(_version string, _vList []string) *Version {
|
func NewVersion(_version string, _vList []string) *Version {
|
||||||
v := new(Version)
|
v := new(Version)
|
||||||
v.Version = *NewSemVersion(_version)
|
v.Version = *NewSemVersion(_version)
|
||||||
@ -130,6 +152,39 @@ func NewVersion(_version string, _vList []string) *Version {
|
|||||||
v.availableVersions = append(v.availableVersions, *NewSemVersion(release))
|
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 {
|
switch {
|
||||||
case strings.Contains(v.Version.ToString(), latestRelease):
|
case strings.Contains(v.Version.ToString(), latestRelease):
|
||||||
release := strings.Split(v.Version.ToString(), latestRelease)[1]
|
release := strings.Split(v.Version.ToString(), latestRelease)[1]
|
||||||
@ -139,12 +194,19 @@ func NewVersion(_version string, _vList []string) *Version {
|
|||||||
release := strings.Split(v.Version.ToString(), latestPatch)[1]
|
release := strings.Split(v.Version.ToString(), latestPatch)[1]
|
||||||
v.Version = *NewSemVersion(release)
|
v.Version = *NewSemVersion(release)
|
||||||
v.getLatestMajorRelease()
|
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
|
return v
|
||||||
}
|
}
|
||||||
|
|
||||||
//GetVersionList returns a list of available versions from hashicorp's release page
|
// GetVersionList returns a list of available versions from hashicorp's release page
|
||||||
func GetVersionList() ([]string, error) {
|
func GetVersionList() ([]string, error) {
|
||||||
var versionList []string
|
var versionList []string
|
||||||
resp, err := http.Get(hashicorpUrl)
|
resp, err := http.Get(hashicorpUrl)
|
||||||
@ -176,7 +238,7 @@ func GetVersionList() ([]string, error) {
|
|||||||
return versionList, nil
|
return versionList, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
//removeSpacesVersion removes spaces from Version string for parsing
|
// removeSpacesVersion removes spaces from Version string for parsing
|
||||||
func removeSpacesVersion(v string) string {
|
func removeSpacesVersion(v string) string {
|
||||||
splitV := strings.Split(v, " ")
|
splitV := strings.Split(v, " ")
|
||||||
var returnString string
|
var returnString string
|
||||||
@ -187,7 +249,33 @@ func removeSpacesVersion(v string) string {
|
|||||||
return strings.TrimSpace(returnString)
|
return strings.TrimSpace(returnString)
|
||||||
}
|
}
|
||||||
|
|
||||||
//VersionToString returns string of a Version
|
// VersionToString returns string of a Version
|
||||||
func (v *Version) VersionToString() string {
|
func (v *Version) VersionToString() string {
|
||||||
return v.Version.ToString()
|
return v.Version.ToString()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// isVersionGreater 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
|
||||||
|
}
|
||||||
|
@ -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",
|
||||||
@ -19,6 +20,9 @@ func testVersionList() []string {
|
|||||||
"1.1.1",
|
"1.1.1",
|
||||||
"1.0.12",
|
"1.0.12",
|
||||||
"1.0.1",
|
"1.0.1",
|
||||||
|
"0.14.0",
|
||||||
|
"0.13.1",
|
||||||
|
"0.13.0",
|
||||||
"0.12.31",
|
"0.12.31",
|
||||||
"0.12.30",
|
"0.12.30",
|
||||||
"0.11.15",
|
"0.11.15",
|
||||||
@ -39,6 +43,11 @@ func TestGetVersion(t *testing.T) {
|
|||||||
{testVersionList(), ">= 0.11.15", "1.1.11"},
|
{testVersionList(), ">= 0.11.15", "1.1.11"},
|
||||||
{testVersionList(), ">= 0.12.0", "1.1.11"},
|
{testVersionList(), ">= 0.12.0", "1.1.11"},
|
||||||
{testVersionList(), "~> 0.12", "0.12.31"},
|
{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 {
|
for _, c := range cases {
|
||||||
@ -74,9 +83,62 @@ 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) {
|
func TestGetVersionList(t *testing.T) {
|
||||||
//todo write test for this
|
//todo write test for this
|
||||||
//response, _ := getVersionList()
|
//response, _ := GetVersionList()
|
||||||
//for _, Version := range response {
|
//for _, Version := range response {
|
||||||
// t.Errorf("%v", Version)
|
// t.Errorf("%v", Version)
|
||||||
//}
|
//}
|
||||||
|
@ -5,4 +5,6 @@ package versionedTerraform
|
|||||||
|
|
||||||
const (
|
const (
|
||||||
fileSuffix = "_windows_386.zip"
|
fileSuffix = "_windows_386.zip"
|
||||||
|
minVersion = "0.0.0"
|
||||||
|
alternateSuffix = ""
|
||||||
)
|
)
|
||||||
|
@ -5,4 +5,6 @@ package versionedTerraform
|
|||||||
|
|
||||||
const (
|
const (
|
||||||
fileSuffix = "_windows_amd64.zip"
|
fileSuffix = "_windows_amd64.zip"
|
||||||
|
minVersion = "0.0.0"
|
||||||
|
alternateSuffix = ""
|
||||||
)
|
)
|
||||||
|
Loading…
Reference in New Issue
Block a user