-
Notifications
You must be signed in to change notification settings - Fork 135
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Convert old RECENT file to JSON Format * added tests * updated dependencies * make sure only the last 5 of the versions are marked with *recent and returned in the list --------- Co-authored-by: George L. Yermulnik <yz@yz.kiev.ua> Co-authored-by: Matt John <matthew@dockstudios.co.uk>
- Loading branch information
1 parent
439cb16
commit d59ef38
Showing
7 changed files
with
265 additions
and
112 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
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
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,111 @@ | ||
package lib | ||
|
||
import ( | ||
"encoding/json" | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
) | ||
|
||
type RecentFiles struct { | ||
Terraform []string `json:"terraform"` | ||
OpenTofu []string `json:"opentofu"` | ||
} | ||
|
||
func addRecent(requestedVersion string, installPath string, distribution string) { | ||
if !validVersionFormat(requestedVersion) { | ||
logger.Errorf("The version %q is not a valid version string and won't be stored", requestedVersion) | ||
return | ||
} | ||
installLocation := GetInstallLocation(installPath) | ||
recentFilePath := filepath.Join(installLocation, recentFile) | ||
var recentFileData RecentFiles | ||
if CheckFileExist(recentFilePath) { | ||
unmarshalRecentFileData(recentFilePath, &recentFileData) | ||
} | ||
prependRecentVersionToList(requestedVersion, distribution, &recentFileData) | ||
saveRecentFile(recentFileData, recentFilePath) | ||
} | ||
|
||
func prependRecentVersionToList(version, distribution string, r *RecentFiles) { | ||
var sliceToCheck []string | ||
switch distribution { | ||
case distributionTerraform: | ||
sliceToCheck = r.Terraform | ||
case distributionOpenTofu: | ||
sliceToCheck = r.OpenTofu | ||
} | ||
for versionIndex, versionValue := range sliceToCheck { | ||
if versionValue == version { | ||
sliceToCheck = append(sliceToCheck[:versionIndex], sliceToCheck[versionIndex+1:]...) | ||
} | ||
} | ||
sliceToCheck = append([]string{version}, sliceToCheck...) | ||
|
||
switch distribution { | ||
case distributionTerraform: | ||
r.Terraform = sliceToCheck | ||
case distributionOpenTofu: | ||
r.OpenTofu = sliceToCheck | ||
} | ||
} | ||
|
||
func getRecentVersions(installPath string, dist string) ([]string, error) { | ||
installLocation := GetInstallLocation(installPath) | ||
recentFilePath := filepath.Join(installLocation, recentFile) | ||
var recentFileData RecentFiles | ||
unmarshalRecentFileData(recentFilePath, &recentFileData) | ||
var listOfRecentVersions []string | ||
switch dist { | ||
case distributionTerraform: | ||
listOfRecentVersions = recentFileData.Terraform | ||
case distributionOpenTofu: | ||
listOfRecentVersions = recentFileData.OpenTofu | ||
} | ||
var maxCount int | ||
if len(listOfRecentVersions) >= 5 { | ||
maxCount = 5 | ||
} else { | ||
maxCount = len(listOfRecentVersions) | ||
} | ||
var returnedRecentVersions []string | ||
for i := 0; i < maxCount; i++ { | ||
returnedRecentVersions = append(returnedRecentVersions, listOfRecentVersions[i]+" *recent") | ||
} | ||
return returnedRecentVersions, nil | ||
} | ||
|
||
func unmarshalRecentFileData(recentFilePath string, recentFileData *RecentFiles) { | ||
recentFileContent, err := os.ReadFile(recentFilePath) | ||
if err != nil { | ||
logger.Errorf("Could not open recent versions file %q", recentFilePath) | ||
} | ||
if string(recentFileContent[0:1]) != "{" { | ||
convertOldRecentFile(recentFileContent, recentFileData) | ||
} else { | ||
err = json.Unmarshal(recentFileContent, &recentFileData) | ||
if err != nil { | ||
logger.Errorf("Could not unmarshal recent versions content from %q file", recentFilePath) | ||
} | ||
} | ||
} | ||
|
||
func convertOldRecentFile(content []byte, recentFileData *RecentFiles) { | ||
lines := strings.Split(string(content), "\n") | ||
for _, s := range lines { | ||
if s != "" { | ||
recentFileData.Terraform = append(recentFileData.Terraform, s) | ||
} | ||
} | ||
} | ||
|
||
func saveRecentFile(data RecentFiles, path string) { | ||
bytes, err := json.Marshal(data) | ||
if err != nil { | ||
logger.Errorf("Could not marshal data to JSON: %v", err) | ||
} | ||
err = os.WriteFile(path, bytes, 0644) | ||
if err != nil { | ||
logger.Errorf("Could not save file %q: %v", path, err) | ||
} | ||
} |
Oops, something went wrong.