Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Develop #74

Merged
merged 3 commits into from
May 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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