From 4b758d74fac20a6c56499eeacab095c77d7ac0a0 Mon Sep 17 00:00:00 2001 From: "warren.veerasingam@gmail.com" Date: Mon, 11 May 2020 13:27:26 -0500 Subject: [PATCH] fix bug --- lib/install.go | 59 ++++++++++++++++++++++++++++++-------------- lib/list_versions.go | 9 ++++--- main.go | 7 +++--- 3 files changed, 49 insertions(+), 26 deletions(-) diff --git a/lib/install.go b/lib/install.go index c2c7d04..7e440ff 100644 --- a/lib/install.go +++ b/lib/install.go @@ -15,28 +15,21 @@ const ( gruntURL = "https://github.com/gruntwork-io/terragrunt/releases/download/" installFile = "terragrunt" installVersion = "terragrunt_" - binLocation = "/usr/local/bin/terragrunt" installPath = "/.terragrunt.versions/" recentFile = "RECENT" ) var ( - installLocation = "/tmp" - installedBinPath = "/tmp" + installLocation = "/tmp" ) -func init() { - /* get current user */ - usr, errCurr := user.Current() - if errCurr != nil { - log.Fatal(errCurr) - } - - /* set installation location */ - installLocation = usr.HomeDir + installPath +// initialize : removes existing symlink to terragrunt binary +func initialize() { - /* set default binary path for terragrunt */ - installedBinPath = binLocation + /* initilize default binary path for terraform */ + /* assumes that terraform is installed here */ + /* we will find the terraform path instalation later and replace this variable with the correct installed bin path */ + installedBinPath := "/usr/local/bin/terragrunt" /* find terragrunt binary location if terragrunt is already installed*/ cmd := NewCommand("terragrunt") @@ -54,17 +47,33 @@ func init() { if symlinkExist { RemoveSymlink(installedBinPath) } +} + +// getInstallLocation : get location where the terraform binary will be installed, +// will create a directory in the home location if it does not exist +func getInstallLocation() string { + /* get current user */ + usr, errCurr := user.Current() + if errCurr != nil { + log.Fatal(errCurr) + } + /* set installation location */ + installLocation = usr.HomeDir + installPath /* Create local installation directory if it does not exist */ CreateDirIfNotExist(installLocation) + return installLocation } //Install : Install the provided version in the argument -func Install(url string, appversion string, assests []modal.Repo, userBinPath *string) string { +func Install(url string, appversion string, assests []modal.Repo, installedBinPath string) string { + + initialize() + installLocation = getInstallLocation() //get installation location - this is where we will put our terraform binary file /* If user provided bin path use user one instead of default */ - if userBinPath != nil { - installedBinPath = *userBinPath - } + // if userBinPath != nil { + // installedBinPath = *userBinPath + // } pathDir := Path(installedBinPath) //get path directory from binary path binDirExist := CheckDirExist(pathDir) //check bin path exist @@ -145,6 +154,8 @@ func Install(url string, appversion string, assests []modal.Repo, userBinPath *s // AddRecent : add to recent file func AddRecent(requestedVersion string, installLocation string) { + installLocation = getInstallLocation() + semverRegex := regexp.MustCompile(`\d+(\.\d+){2}\z`) fileExist := CheckFileExist(installLocation + recentFile) @@ -186,11 +197,14 @@ func AddRecent(requestedVersion string, installLocation string) { // GetRecentVersions : get recent version from file func GetRecentVersions() ([]string, error) { + installLocation = getInstallLocation() + fileExist := CheckFileExist(installLocation + recentFile) if fileExist { semverRegex := regexp.MustCompile(`\A\d+(\.\d+){2}\z`) lines, errRead := ReadLines(installLocation + recentFile) + outputRecent := []string{} if errRead != nil { fmt.Printf("Error: %s\n", errRead) @@ -202,14 +216,21 @@ func GetRecentVersions() ([]string, error) { RemoveFiles(installLocation + recentFile) return nil, errRead } + + /* output can be confusing since it displays the 3 most recent used terraform version + append the string *recent to the output to make it more user friendly + */ + outputRecent = append(outputRecent, fmt.Sprintf("%s *recent", line)) } - return lines, nil + return outputRecent, nil } return nil, nil } //CreateRecentFile : create a recent file func CreateRecentFile(requestedVersion string) { + + installLocation = getInstallLocation() WriteLines([]string{requestedVersion}, installLocation+recentFile) } diff --git a/lib/list_versions.go b/lib/list_versions.go index 714e3a3..4158fa9 100644 --- a/lib/list_versions.go +++ b/lib/list_versions.go @@ -101,14 +101,15 @@ func RemoveDuplicateVersions(elements []string) []string { encountered := map[string]bool{} result := []string{} - for v := range elements { - if encountered[elements[v]] == true { + for _, val := range elements { + versionOnly := strings.Trim(val, " *recent") + if encountered[versionOnly] == true { // Do not add duplicate. } else { // Record this element as an encountered element. - encountered[elements[v]] = true + encountered[versionOnly] = true // Append to result slice. - result = append(result, elements[v]) + result = append(result, val) } } // Return the new slice. diff --git a/main.go b/main.go index ee96908..ed1fe35 100644 --- a/main.go +++ b/main.go @@ -82,7 +82,7 @@ func main() { _, assets := lib.GetAppList(terragruntURL, &client) if lib.ValidVersionFormat(tfversion) { //check if version is correct - lib.Install(terragruntURL, string(tfversion), assets, custBinPath) + lib.Install(terragruntURL, string(tfversion), assets, *custBinPath) } else { fmt.Println("Invalid terragrunt version format. Format should be #.#.# or #.#.#-@# where # is numbers and @ is word characters. For example, 0.11.7 and 0.11.9-beta1 are valid versions") os.Exit(1) @@ -98,7 +98,7 @@ func main() { exist := lib.VersionExist(requestedVersion, tflist) if exist { - installLocation := lib.Install(terragruntURL, requestedVersion, assets, custBinPath) + installLocation := lib.Install(terragruntURL, requestedVersion, assets, *custBinPath) lib.AddRecent(requestedVersion, installLocation) //add to recent file for faster lookup } else { fmt.Println("Not a valid terragrunt version") @@ -124,13 +124,14 @@ func main() { } _, tgversion, errPrompt := prompt.Run() + tgversion = strings.Trim(tgversion, " *recent") if errPrompt != nil { log.Printf("Prompt failed %v\n", errPrompt) os.Exit(1) } - installLocation := lib.Install(terragruntURL, tgversion, assets, custBinPath) + installLocation := lib.Install(terragruntURL, tgversion, assets, *custBinPath) lib.AddRecent(tgversion, installLocation) //add to recent file for faster lookup os.Exit(0) } else {