-
Notifications
You must be signed in to change notification settings - Fork 137
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move recentFile-related code into new recent file
Issue #315
- Loading branch information
1 parent
039e9f1
commit 3110e33
Showing
2 changed files
with
99 additions
and
93 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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{} | ||
} |