2022-02-23 18:15:12 +00:00
|
|
|
package versionedTerraform
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
//todo include others if needed
|
|
|
|
//todo add comparison i.e. >= 0.11.10, < 0.12.0
|
2022-04-27 11:35:49 +00:00
|
|
|
latestRelease = ">="
|
|
|
|
latestPatch = "~>"
|
|
|
|
versionLessOrEqual = "<="
|
|
|
|
versionLessThan = "<"
|
2022-02-23 18:15:12 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type SemVersion struct {
|
|
|
|
version string
|
2022-06-16 02:52:19 +00:00
|
|
|
isStable bool
|
2022-02-23 18:15:12 +00:00
|
|
|
majorVersion int
|
|
|
|
minorVersion int
|
|
|
|
patchVersion int
|
|
|
|
}
|
|
|
|
|
|
|
|
type SemVersionInterface interface {
|
|
|
|
setMajorVersion()
|
|
|
|
setMinorVersion()
|
|
|
|
setPatchVersion()
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewSemVersion(v string) *SemVersion {
|
|
|
|
s := new(SemVersion)
|
2022-06-16 02:52:19 +00:00
|
|
|
s.isStable = true
|
2022-02-23 18:15:12 +00:00
|
|
|
s.version = removeSpacesVersion(v)
|
|
|
|
|
|
|
|
s.setMajorVersion()
|
|
|
|
s.setMinorVersion()
|
|
|
|
s.setPatchVersion()
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
2022-06-18 13:41:00 +00:00
|
|
|
//setMajorVersion setter for SemVersion.majorVersion
|
2022-02-23 18:15:12 +00:00
|
|
|
func (s *SemVersion) setMajorVersion() {
|
|
|
|
version := s.version
|
|
|
|
majorVersionString := strings.Split(version, ".")[0]
|
|
|
|
s.majorVersion, _ = strconv.Atoi(majorVersionString)
|
|
|
|
}
|
|
|
|
|
2022-06-18 13:41:00 +00:00
|
|
|
//setMinorVersion setter for SemVersion.minorVersion
|
2022-02-23 18:15:12 +00:00
|
|
|
func (s *SemVersion) setMinorVersion() {
|
|
|
|
version := s.version
|
|
|
|
minorVersionString := strings.Split(version, ".")[1]
|
|
|
|
s.minorVersion, _ = strconv.Atoi(minorVersionString)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-06-18 13:41:00 +00:00
|
|
|
//setPatchVersion setter for SemVersion.patchVersion
|
2022-02-23 18:15:12 +00:00
|
|
|
func (s *SemVersion) setPatchVersion() {
|
|
|
|
version := s.version
|
2022-06-16 02:52:19 +00:00
|
|
|
var err error
|
2022-02-23 18:15:12 +00:00
|
|
|
patchStringSlice := strings.Split(version, ".")
|
|
|
|
if len(patchStringSlice) < 3 {
|
|
|
|
s.patchVersion = 0
|
|
|
|
return
|
|
|
|
}
|
2022-06-16 02:52:19 +00:00
|
|
|
s.patchVersion, err = strconv.Atoi(patchStringSlice[2])
|
|
|
|
if err != nil {
|
|
|
|
s.isStable = false
|
|
|
|
patchStringSlice = strings.Split(patchStringSlice[2], "-")
|
|
|
|
s.patchVersion, _ = strconv.Atoi(patchStringSlice[0])
|
|
|
|
}
|
2022-02-23 18:15:12 +00:00
|
|
|
}
|
|
|
|
|
2022-06-18 13:41:00 +00:00
|
|
|
//ToString returns string of SemVersion
|
2022-02-23 18:15:12 +00:00
|
|
|
func (s *SemVersion) ToString() string {
|
|
|
|
return s.version
|
|
|
|
}
|
|
|
|
|
2022-06-18 13:41:00 +00:00
|
|
|
//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
|
2022-02-23 18:15:12 +00:00
|
|
|
func (s *SemVersion) VersionInSlice(sSem []SemVersion) bool {
|
|
|
|
for _, ver := range sSem {
|
|
|
|
if ver.ToString() == s.ToString() {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|