Added comments
This commit is contained in:
parent
98493c8d52
commit
ce4d829184
@ -39,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]
|
||||||
@ -52,6 +54,7 @@ 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
|
var err error
|
||||||
@ -68,10 +71,13 @@ func (s *SemVersion) setPatchVersion() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//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() {
|
||||||
|
12
cmd/main.go
12
cmd/main.go
@ -21,16 +21,19 @@ 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)
|
||||||
}
|
}
|
||||||
@ -42,13 +45,16 @@ 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\n", 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.Printf("Unable to update version: %v\n", err)
|
fmt.Printf("Unable to update version: %v\n", err)
|
||||||
@ -60,6 +66,7 @@ func main() {
|
|||||||
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)
|
||||||
@ -71,11 +78,13 @@ func main() {
|
|||||||
vSlice = append(vSlice, v.ToString())
|
vSlice = append(vSlice, v.ToString())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Check if stable version of terraform is required
|
||||||
needsStable, err = versionedTerraform.ConfigRequiresStable(configDir, configFileLocation)
|
needsStable, err = versionedTerraform.ConfigRequiresStable(configDir, configFileLocation)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Fprintf(os.Stderr, "Unable to open config file, defaulting to stable versions of terraform only")
|
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)
|
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)
|
||||||
@ -89,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{
|
||||||
|
@ -17,6 +17,7 @@ type configStruct struct {
|
|||||||
AvailableVersions []string
|
AvailableVersions []string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//ConfigRequiresStable returns bool, error only false if StableOnly: false is set in configuration file
|
||||||
func ConfigRequiresStable(fileSystem fs.FS, configFile string) (bool, error) {
|
func ConfigRequiresStable(fileSystem fs.FS, configFile string) (bool, error) {
|
||||||
fileHandle, err := fileSystem.Open(configFile)
|
fileHandle, err := fileSystem.Open(configFile)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -39,6 +40,8 @@ func ConfigRequiresStable(fileSystem fs.FS, configFile string) (bool, error) {
|
|||||||
return true, 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)
|
||||||
@ -68,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("\\[")
|
||||||
@ -97,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
|
||||||
@ -115,6 +121,11 @@ func LoadInstalledVersions(fileSystem fs.FS) ([]SemVersion, error) {
|
|||||||
return installedTerraformVersions, nil
|
return installedTerraformVersions, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//UpdateConfig returns an error, and updates configuration file
|
||||||
|
// adding:
|
||||||
|
// a new date to the last updated field
|
||||||
|
// the available versions listed on terraforms website
|
||||||
|
// teh status of if the user wants only stable releases
|
||||||
func UpdateConfig(File os.File) error {
|
func UpdateConfig(File os.File) error {
|
||||||
configValues := new(configStruct)
|
configValues := new(configStruct)
|
||||||
|
|
||||||
@ -133,6 +144,7 @@ func UpdateConfig(File os.File) error {
|
|||||||
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)
|
||||||
|
@ -9,6 +9,8 @@ import (
|
|||||||
|
|
||||||
var needsStable bool
|
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, needsStableValue bool) (*Version, error) {
|
func GetVersionFromFile(fileSystem fs.FS, versionList []string, needsStableValue bool) (*Version, error) {
|
||||||
needsStable = needsStableValue
|
needsStable = needsStableValue
|
||||||
@ -33,6 +35,9 @@ func GetVersionFromFile(fileSystem fs.FS, versionList []string, needsStableValue
|
|||||||
}
|
}
|
||||||
|
|
||||||
//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+?=")
|
||||||
|
@ -260,7 +260,7 @@ func (v *Version) VersionToString() string {
|
|||||||
return v.Version.ToString()
|
return v.Version.ToString()
|
||||||
}
|
}
|
||||||
|
|
||||||
//versionCompare returns true if v1 is greater than v2
|
//isVersionGreater returns true if v1 is greater than v2
|
||||||
func isVersionGreater(v1 Version, v2 Version) bool {
|
func isVersionGreater(v1 Version, v2 Version) bool {
|
||||||
if v1.Version.majorVersion != v2.Version.majorVersion {
|
if v1.Version.majorVersion != v2.Version.majorVersion {
|
||||||
if v1.Version.majorVersion > v2.Version.majorVersion {
|
if v1.Version.majorVersion > v2.Version.majorVersion {
|
||||||
|
Loading…
Reference in New Issue
Block a user