Skip to content

Commit

Permalink
Merge pull request #75 from warrensbox/master
Browse files Browse the repository at this point in the history
Bug fixes - Ctrl-C Interrupt Breaks `terragrunt` executable
  • Loading branch information
warrensbox committed May 11, 2020
2 parents fba0e68 + 8965495 commit bb1ed35
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 29 deletions.
2 changes: 1 addition & 1 deletion docs/Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ GEM
jekyll-seo-tag (~> 2.1)
minitest (5.11.3)
multipart-post (2.0.0)
nokogiri (1.10.4)
nokogiri (1.10.8)
mini_portile2 (~> 2.4.0)
octokit (4.13.0)
sawyer (~> 0.8.0, >= 0.5.3)
Expand Down
4 changes: 2 additions & 2 deletions docs/_site/Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ GEM
jekyll-seo-tag (~> 2.1)
minitest (5.11.3)
multipart-post (2.0.0)
nokogiri (1.10.1)
nokogiri (1.10.8)
mini_portile2 (~> 2.4.0)
octokit (4.13.0)
sawyer (~> 0.8.0, >= 0.5.3)
Expand All @@ -219,7 +219,7 @@ GEM
ruby-enum (0.7.2)
i18n
ruby_dep (1.5.0)
rubyzip (1.2.2)
rubyzip (1.3.0)
safe_yaml (1.0.4)
sass (3.7.3)
sass-listen (~> 4.0.0)
Expand Down
59 changes: 40 additions & 19 deletions lib/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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)
}

Expand Down
9 changes: 5 additions & 4 deletions lib/list_versions.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
7 changes: 4 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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")
Expand All @@ -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 {
Expand Down

0 comments on commit bb1ed35

Please sign in to comment.