Skip to content

Commit

Permalink
Move recentFile-related code into new recent file
Browse files Browse the repository at this point in the history
Issue #315
  • Loading branch information
MatthewJohn committed May 27, 2024
1 parent e67f851 commit cb64930
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 93 deletions.
93 changes: 0 additions & 93 deletions lib/install.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package lib

import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"runtime"
Expand Down Expand Up @@ -140,97 +138,6 @@ func install(product Product, tfversion string, binPath string, installPath stri
return
}

type RecentFile struct {
Terraform []string
OpenTofu []string
}

func appendRecentVersionToList(versions []string, requestedVersion string) []string {
// Check for requestedVersion in versions list
for versionIndex, versionVal := range versions {
if versionVal == requestedVersion {
versions = append(versions[:versionIndex], versions[versionIndex+1:]...)
}
}

// Add new version to start of slice
versions = append([]string{requestedVersion}, versions...)
if len(versions) > 3 {
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 {
logger.Errorf("Error to open %q file for writing: %v", recentFilePath, err)
return
}
defer recentVersionFh.Close()

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

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

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

fileExist := CheckFileExist(recentFilePath)
if fileExist {
content, err := ioutil.ReadFile(recentFilePath)
if err != nil {
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 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
99 changes: 99 additions & 0 deletions lib/recent.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package lib

import (
"encoding/json"
"io/ioutil"
"os"
"path/filepath"
)

type RecentFile struct {
Terraform []string
OpenTofu []string
}

func appendRecentVersionToList(versions []string, requestedVersion string) []string {
// Check for requestedVersion in versions list
for versionIndex, versionVal := range versions {
if versionVal == requestedVersion {
versions = append(versions[:versionIndex], versions[versionIndex+1:]...)
}
}

// Add new version to start of slice
versions = append([]string{requestedVersion}, versions...)
if len(versions) > 3 {
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 {
logger.Errorf("Error to open %q file for writing: %v", recentFilePath, err)
return
}
defer recentVersionFh.Close()

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

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

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

fileExist := CheckFileExist(recentFilePath)
if fileExist {
content, err := ioutil.ReadFile(recentFilePath)
if err != nil {
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 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{}
}

0 comments on commit cb64930

Please sign in to comment.