diff --git a/lib/install.go b/lib/install.go index 7e440ff..774813f 100644 --- a/lib/install.go +++ b/lib/install.go @@ -49,9 +49,9 @@ func initialize() { } } -// getInstallLocation : get location where the terraform binary will be installed, +// 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 { +func GetInstallLocation() string { /* get current user */ usr, errCurr := user.Current() if errCurr != nil { @@ -68,11 +68,11 @@ func getInstallLocation() 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 + 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 + // installedBinPath = *userBinPath // } pathDir := Path(installedBinPath) //get path directory from binary path @@ -86,19 +86,8 @@ func Install(url string, appversion string, assests []modal.Repo, installedBinPa /* check if selected version already downloaded */ fileExist := CheckFileExist(installLocation + installVersion + appversion) - - /* if selected version already exist, */ if fileExist { - - /* remove current symlink if exist*/ - symlinkExist := CheckSymlink(installedBinPath) - - if symlinkExist { - RemoveSymlink(installedBinPath) - } - /* set symlink to desired version */ - CreateSymlink(installLocation+installVersion+appversion, installedBinPath) - fmt.Printf("Switched terragrunt to version %q \n", appversion) + installLocation := ChangeSymlink(installedBinPath, appversion) return installLocation } @@ -154,7 +143,7 @@ func Install(url string, appversion string, assests []modal.Repo, installedBinPa // AddRecent : add to recent file func AddRecent(requestedVersion string, installLocation string) { - installLocation = getInstallLocation() + installLocation = GetInstallLocation() semverRegex := regexp.MustCompile(`\d+(\.\d+){2}\z`) @@ -197,7 +186,7 @@ func AddRecent(requestedVersion string, installLocation string) { // GetRecentVersions : get recent version from file func GetRecentVersions() ([]string, error) { - installLocation = getInstallLocation() + installLocation = GetInstallLocation() fileExist := CheckFileExist(installLocation + recentFile) if fileExist { @@ -217,7 +206,7 @@ func GetRecentVersions() ([]string, error) { return nil, errRead } - /* output can be confusing since it displays the 3 most recent used terraform version + /* 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)) @@ -230,7 +219,7 @@ func GetRecentVersions() ([]string, error) { //CreateRecentFile : create a recent file func CreateRecentFile(requestedVersion string) { - installLocation = getInstallLocation() + installLocation = GetInstallLocation() WriteLines([]string{requestedVersion}, installLocation+recentFile) } diff --git a/lib/symlink.go b/lib/symlink.go index 02d9efe..fd33c62 100644 --- a/lib/symlink.go +++ b/lib/symlink.go @@ -1,6 +1,7 @@ package lib import ( + "fmt" "log" "os" ) @@ -64,3 +65,20 @@ func CheckSymlink(symlinkPath string) bool { return false } + +// ChangeSymlink : move symlink to existing binary +func ChangeSymlink(installedBinPath string, appversion string) string { + + installLocation = GetInstallLocation() //get installation location - this is where we will put our terraform binary file + + /* remove current symlink if exist*/ + symlinkExist := CheckSymlink(installedBinPath) + if symlinkExist { + RemoveSymlink(installedBinPath) + } + /* set symlink to desired version */ + CreateSymlink(installLocation+installVersion+appversion, installedBinPath) + fmt.Printf("Switched terragrunt to version %q \n", appversion) + return installLocation + +} diff --git a/main.go b/main.go index 1b7426e..cafca7c 100644 --- a/main.go +++ b/main.go @@ -30,10 +30,11 @@ import ( ) const ( - terragruntURL = "https://api.github.com/repos/gruntwork-io/terragrunt/releases?" - defaultBin = "/usr/local/bin/terragrunt" //default bin installation dir - rcFilename = ".tgswitchrc" - tgvFilename = ".terragrunt-version" + terragruntURL = "https://api.github.com/repos/gruntwork-io/terragrunt/releases?" + defaultBin = "/usr/local/bin/terragrunt" //default bin installation dir + rcFilename = ".tgswitchrc" + tgvFilename = ".terragrunt-version" + installVersion = "terragrunt_" ) var version = "0.2.0\n" @@ -70,7 +71,7 @@ func main() { } else if *helpFlag { usageMessage() } else { - + installLocation := lib.GetInstallLocation() if _, err := os.Stat(rcfile); err == nil && len(args) == 0 { //if there is a .tgswitchrc file, and no commmand line arguments fmt.Printf("Reading required terragrunt version %s \n", rcFilename) @@ -81,6 +82,11 @@ func main() { os.Exit(1) } tgversion := strings.TrimSuffix(string(fileContents), "\n") + fileExist := lib.CheckFileExist(installLocation + installVersion + tgversion) + if fileExist { + lib.ChangeSymlink(*custBinPath, string(tgversion)) + os.Exit(0) + } _, assets := lib.GetAppList(terragruntURL, &client) if lib.ValidVersionFormat(tgversion) { //check if version is correct @@ -100,6 +106,11 @@ func main() { os.Exit(1) } tgversion := strings.TrimSuffix(string(fileContents), "\n") + fileExist := lib.CheckFileExist(installLocation + installVersion + string(tgversion)) + if fileExist { + lib.ChangeSymlink(*custBinPath, string(tgversion)) + os.Exit(0) + } _, assets := lib.GetAppList(terragruntURL, &client) if lib.ValidVersionFormat(tgversion) { //check if version is correct @@ -115,6 +126,12 @@ func main() { if semverRegex.MatchString(args[0]) { requestedVersion := args[0] + fileExist := lib.CheckFileExist(installLocation + installVersion + string(requestedVersion)) + if fileExist { + lib.ChangeSymlink(*custBinPath, string(requestedVersion)) + os.Exit(0) + } + //check if version exist before downloading it tflist, assets := lib.GetAppList(terragruntURL, &client) exist := lib.VersionExist(requestedVersion, tflist)