Skip to content

Commit

Permalink
Combine RECENT files into a single file with keys for each product
Browse files Browse the repository at this point in the history
Issue #315
  • Loading branch information
MatthewJohn committed May 27, 2024
1 parent 689c396 commit e67f851
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 25 deletions.
2 changes: 1 addition & 1 deletion lib/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@ const (
DefaultMirror = "https://releases.hashicorp.com/terraform"
DefaultLatest = ""
InstallDir = ".terraform.versions"
recentFilePrefix = "RECENT_"
recentFile = "RECENT"
tfDarwinArm64StartVersion = "1.0.2"
)
69 changes: 45 additions & 24 deletions lib/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,19 +140,12 @@ func install(product Product, tfversion string, binPath string, installPath stri
return
}

// getRecentFileName : Obtain recent version filename for product
func getRecentFileName(product Product) string {
return recentFilePrefix + product.GetId() + ".json"
type RecentFile struct {
Terraform []string
OpenTofu []string
}

// addRecent : add to recent file
func addRecentVersion(product Product, requestedVersion string, installPath string) {
installLocation = GetInstallLocation(installPath) //get installation location - this is where we will put our terraform binary file
recentFilePath := filepath.Join(installLocation, getRecentFileName(product))

// Obtain pre-existing latest version
versions := getRecentVersions(product, installPath)

func appendRecentVersionToList(versions []string, requestedVersion string) []string {
// Check for requestedVersion in versions list
for versionIndex, versionVal := range versions {
if versionVal == requestedVersion {
Expand All @@ -166,6 +159,23 @@ func addRecentVersion(product Product, requestedVersion string, installPath stri
versions = versions[0:2]
}

return versions
}

// addRecent : add to recent file
func addRecentVersion(product Product, requestedVersion string, installPath string) {
installLocation = GetInstallLocation(installPath) //get installation location - this is where we will put our terraform binary file
recentFilePath := filepath.Join(installLocation, recentFile)

// Obtain pre-existing latest version
recentData := getRecentFileData(installPath)

if product.GetId() == "terraform" {
recentData.Terraform = appendRecentVersionToList(recentData.Terraform, requestedVersion)
} else if product.GetId() == "opentofu" {
recentData.OpenTofu = appendRecentVersionToList(recentData.OpenTofu, requestedVersion)
}

// Write new versions back to recent files
recentVersionFh, err := os.OpenFile(recentFilePath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
if err != nil {
Expand All @@ -175,41 +185,52 @@ func addRecentVersion(product Product, requestedVersion string, installPath stri
defer recentVersionFh.Close()

// Marhsall data and write to file
jsonData, err := json.Marshal(versions)
jsonData, err := json.Marshal(recentData)
if err != nil {
logger.Errorf("Failed to marshal recent versions data: %v", err)
logger.Warnf("Error during marshalling recent versions data from %s file: %v. Ignoring", recentFilePath, err)
}

_, err = recentVersionFh.Write(jsonData)
if err != nil {
logger.Errorf("Failed to write recent versions file data: %v", err)
logger.Warnf("Error writing recent versions file (%q): %v. Ignoring", recentFilePath, err)
}
}

// getRecentVersions : get recent version from file
func getRecentVersions(product Product, installPath string) []string {

func getRecentFileData(installPath string) RecentFile {
installLocation = GetInstallLocation(installPath) //get installation location - this is where we will put our terraform binary file
recentVersionFile := filepath.Join(installLocation, getRecentFileName(product))
var outputRecent []string
recentFilePath := filepath.Join(installLocation, recentFile)
var outputRecent RecentFile

fileExist := CheckFileExist(recentVersionFile)
fileExist := CheckFileExist(recentFilePath)
if fileExist {
content, err := ioutil.ReadFile(recentVersionFile)
content, err := ioutil.ReadFile(recentFilePath)
if err != nil {
logger.Warnf("Error when opening recent version file (%s): %s", recentVersionFile, err)
logger.Warnf("Error opening recent versions file (%q): %v. Ignoring", recentFilePath, err)
return outputRecent
}

err = json.Unmarshal(content, &outputRecent)
if err != nil {
logger.Warnf("Error during unmarshalling recentVersionFile: ", err)
logger.Warnf("Error during unmarshalling recent versions data from %s file: %v. Ignoring", recentFilePath, err)
}
}

return outputRecent
}

// getRecentVersions : get recent version from file
func getRecentVersions(product Product, installPath string) []string {
outputRecent := getRecentFileData(installPath)

if product.GetId() == "terraform" {
return outputRecent.Terraform
} else if product.GetId() == "opentofu" {
return outputRecent.OpenTofu
}

// Catch-all for unmatched product
return []string{}
}

// ConvertExecutableExt : convert excutable with local OS extension
func ConvertExecutableExt(fpath string) string {
switch runtime.GOOS {
Expand Down

0 comments on commit e67f851

Please sign in to comment.