From 13b73a4abc795923885ac135a9024b79a1262fa5 Mon Sep 17 00:00:00 2001 From: Matthew John Date: Mon, 27 May 2024 09:01:24 +0100 Subject: [PATCH] Add handling migration of Recent file data from previous format Based on changes by MatrixCrawler in https://github.com/warrensbox/terraform-switcher/pull/437/commits/64d6bec6663be36c9272179b46700aff4e050488 --- lib/recent.go | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/lib/recent.go b/lib/recent.go index dc1d4f5f..c22bfba1 100644 --- a/lib/recent.go +++ b/lib/recent.go @@ -5,6 +5,7 @@ import ( "io/ioutil" "os" "path/filepath" + "strings" ) type RecentFile struct { @@ -63,6 +64,15 @@ func addRecentVersion(product Product, requestedVersion string, installPath stri } } +func convertLegacyRecentData(content []byte, recentFileContent *RecentFile) { + lines := strings.Split(string(content), "\n") + for _, s := range lines { + if s != "" { + recentFileContent.Terraform = append(recentFileContent.Terraform, s) + } + } +} + 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) @@ -76,6 +86,10 @@ func getRecentFileData(installPath string) RecentFile { return outputRecent } + if !strings.HasPrefix(string(content), "{") { + convertLegacyRecentData(content, &outputRecent) + } + err = json.Unmarshal(content, &outputRecent) if err != nil { logger.Warnf("Error during unmarshalling recent versions data from %s file: %v. Ignoring", recentFilePath, err)