diff --git a/SemVersion.go b/SemVersion.go index 6ae8a9d..c6b36fc 100644 --- a/SemVersion.go +++ b/SemVersion.go @@ -39,12 +39,14 @@ func NewSemVersion(v string) *SemVersion { return s } +//setMajorVersion setter for SemVersion.majorVersion func (s *SemVersion) setMajorVersion() { version := s.version majorVersionString := strings.Split(version, ".")[0] s.majorVersion, _ = strconv.Atoi(majorVersionString) } +//setMinorVersion setter for SemVersion.minorVersion func (s *SemVersion) setMinorVersion() { version := s.version minorVersionString := strings.Split(version, ".")[1] @@ -52,6 +54,7 @@ func (s *SemVersion) setMinorVersion() { } +//setPatchVersion setter for SemVersion.patchVersion func (s *SemVersion) setPatchVersion() { version := s.version var err error @@ -68,10 +71,13 @@ func (s *SemVersion) setPatchVersion() { } } +//ToString returns string of SemVersion func (s *SemVersion) ToString() string { 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 { for _, ver := range sSem { if ver.ToString() == s.ToString() { diff --git a/cmd/main.go b/cmd/main.go index c5a739f..8f79c2f 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -21,16 +21,19 @@ func main() { homeDir, _ := os.UserHomeDir() configDirString := homeDir + shortConfigDirString + // Create configuration directory if it does not exist _, err := os.Stat(configDirString) if os.IsNotExist(err) { err = versionedTerraform.CreateConfig(configDirString, configFileLocation) } + // Create configuration file if it does not exist _, err = os.Stat(configDirString + "/" + configFileLocation) if os.IsNotExist(err) { err = versionedTerraform.CreateConfig(configDirString, configFileLocation) } + // Let the user know if we couldn't create the config directory or file if err != nil { fmt.Printf("Unable to create config directory: %v", err) } @@ -42,13 +45,16 @@ func main() { flag.Parse() args := flag.Args() + //Load available versions from configuration file versionsFromConfig, err = versionedTerraform.LoadVersionsFromConfig(configDir, configFileLocation) - if err != nil { fmt.Printf("Unable to read config: %v\n", err) 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) if os.ErrNotExist == err { fmt.Printf("Unable to update version: %v\n", err) @@ -60,6 +66,7 @@ func main() { versionedTerraform.UpdateConfig(*fileHandle) } + // Load a slice of versions which have already been installed installedVersions, err := versionedTerraform.LoadInstalledVersions(configDir) if err != nil { fmt.Printf("Unable to verify installed verisons: %v", err) @@ -71,11 +78,13 @@ func main() { vSlice = append(vSlice, v.ToString()) } + // Check if stable version of terraform is required 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") } + // Load version required from terraform directory ver, err := versionedTerraform.GetVersionFromFile(workingDir, vSlice, needsStable) if err != nil { fmt.Printf("Unable to retrieve terraform version from files: %v", err) @@ -89,6 +98,7 @@ func main() { } } + // Execute terraform terraformFile := configDirString + terraformPrefix + ver.VersionToString() argsForTerraform := append([]string{""}, args...) cmd := exec.Cmd{ diff --git a/configManagement.go b/configManagement.go index 4886235..a93d643 100644 --- a/configManagement.go +++ b/configManagement.go @@ -17,6 +17,7 @@ type configStruct struct { 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) { fileHandle, err := fileSystem.Open(configFile) if err != nil { @@ -39,6 +40,8 @@ func ConfigRequiresStable(fileSystem fs.FS, configFile string) (bool, error) { 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) { //todo this is used a lot abstract it? fileHandle, err := fileSystem.Open(availableVersions) @@ -68,6 +71,8 @@ func NeedToUpdateAvailableVersions(fileSystem fs.FS, availableVersions string) ( 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) { fileHandle, err := fileSystem.Open(configFile) removeOpenBracket := regexp.MustCompile("\\[") @@ -97,6 +102,7 @@ func LoadVersionsFromConfig(fileSystem fs.FS, configFile string) ([]SemVersion, 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) { dir, err := fs.ReadDir(fileSystem, ".") var installedTerraformVersions []SemVersion @@ -115,6 +121,11 @@ func LoadInstalledVersions(fileSystem fs.FS) ([]SemVersion, error) { 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 { configValues := new(configStruct) @@ -133,6 +144,7 @@ func UpdateConfig(File os.File) error { return nil } +//CreateConfig returns error, creates a new configuration file func CreateConfig(directory string, configFile string) error { configFileName := directory + "/" + configFile err := os.MkdirAll(directory, 0755) diff --git a/fileHandler.go b/fileHandler.go index 4b68bf0..d2b8a75 100644 --- a/fileHandler.go +++ b/fileHandler.go @@ -9,6 +9,8 @@ import ( 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... func GetVersionFromFile(fileSystem fs.FS, versionList []string, needsStableValue bool) (*Version, error) { needsStable = needsStableValue @@ -33,6 +35,9 @@ func GetVersionFromFile(fileSystem fs.FS, versionList []string, needsStableValue } //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) { fileHandle, err := f.Open(fileName) regex := regexp.MustCompile("required_version\\s+?=") diff --git a/versionedTerraform.go b/versionedTerraform.go index 5f7e60d..398de32 100644 --- a/versionedTerraform.go +++ b/versionedTerraform.go @@ -260,7 +260,7 @@ func (v *Version) VersionToString() string { 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 { if v1.Version.majorVersion != v2.Version.majorVersion { if v1.Version.majorVersion > v2.Version.majorVersion {