Skip to content

Commit

Permalink
Merge pull request warrensbox#95 from steven-edgar/master
Browse files Browse the repository at this point in the history
Dont contact github if terragrunt version exists locally
  • Loading branch information
warrensbox authored Nov 14, 2021
2 parents 72e7ba0 + eda3d8f commit d1b0575
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 25 deletions.
29 changes: 9 additions & 20 deletions lib/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
Expand All @@ -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
}

Expand Down Expand Up @@ -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`)

Expand Down Expand Up @@ -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 {
Expand All @@ -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))
Expand All @@ -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)
}

Expand Down
18 changes: 18 additions & 0 deletions lib/symlink.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package lib

import (
"fmt"
"log"
"os"
)
Expand Down Expand Up @@ -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

}
27 changes: 22 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)

Expand All @@ -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
Expand All @@ -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
Expand All @@ -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)
Expand Down

0 comments on commit d1b0575

Please sign in to comment.