Skip to content

Commit

Permalink
⚡ improve find terraform bin path
Browse files Browse the repository at this point in the history
Avoid lookup into all Path directories and sub-directories when searching for terraform bin location when specify it on command line
  • Loading branch information
ptavares committed Dec 5, 2022
1 parent 107631f commit 2708030
Showing 1 changed file with 22 additions and 18 deletions.
40 changes: 22 additions & 18 deletions lib/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"log"
"os"
"os/exec"
"os/user"
"path/filepath"
"runtime"
Expand All @@ -26,27 +27,32 @@ var (
)

// initialize : removes existing symlink to terraform binary// I Don't think this is needed
func initialize() {

func initialize(customBinPath string) {
/* Step 1 */
/* 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/terraform"

/* find terraform binary location if terraform is already installed*/
cmd := NewCommand("terraform")
next := cmd.Find()

/* overrride installation default binary path if terraform is already installed */
/* find the last bin path */
for path := next(); len(path) > 0; path = next() {
// Try Custom installation path
path, err := exec.LookPath(customBinPath)

if err != nil {
/* find terraform binary location if terraform is already installed*/
cmd := NewCommand("terraform")
next := cmd.Find()
/* overrride installation default binary path if terraform is already installed */
/* find the last bin path */
for path := next(); len(path) > 0; path = next() {
installedBinPath = path
}
} else {
// override installation binary path with custom one specified in command line
installedBinPath = path
}

/* check if current symlink to terraform binary exist */
symlinkExist := CheckSymlink(installedBinPath)

/* remove current symlink if exist*/
if symlinkExist {
RemoveSymlink(installedBinPath)
Expand Down Expand Up @@ -75,9 +81,8 @@ func GetInstallLocation() string {

}

//Install : Install the provided version in the argument
// Install : Install the provided version in the argument
func Install(tfversion string, binPath string, mirrorURL string) {

// if !ValidVersionFormat(tfversion) {
// fmt.Printf("The provided terraform version format does not exist - %s. Try `tfswitch -l` to see all available versions.\n", tfversion)
// os.Exit(1)
Expand All @@ -89,8 +94,7 @@ func Install(tfversion string, binPath string, mirrorURL string) {
* Tell users to add $HOME/bin to their path
*/
binPath = InstallableBinLocation(binPath)

initialize() //initialize path
initialize(binPath) //initialize path
installLocation = GetInstallLocation() //get installation location - this is where we will put our terraform binary file

goarch := runtime.GOARCH
Expand Down Expand Up @@ -252,15 +256,15 @@ func GetRecentVersions() ([]string, error) {
return nil, nil
}

//CreateRecentFile : create a recent file
// CreateRecentFile : create a recent file
func CreateRecentFile(requestedVersion string) {

installLocation = GetInstallLocation() //get installation location - this is where we will put our terraform binary file

WriteLines([]string{requestedVersion}, filepath.Join(installLocation, recentFile))
}

//ConvertExecutableExt : convert excutable with local OS extension
// ConvertExecutableExt : convert excutable with local OS extension
func ConvertExecutableExt(fpath string) string {
switch runtime.GOOS {
case "windows":
Expand All @@ -273,8 +277,8 @@ func ConvertExecutableExt(fpath string) string {
}
}

//InstallableBinLocation : Checks if terraform is installable in the location provided by the user.
//If not, create $HOME/bin. Ask users to add $HOME/bin to $PATH and return $HOME/bin as install location
// InstallableBinLocation : Checks if terraform is installable in the location provided by the user.
// If not, create $HOME/bin. Ask users to add $HOME/bin to $PATH and return $HOME/bin as install location
func InstallableBinLocation(userBinPath string) string {

usr, errCurr := user.Current()
Expand Down

0 comments on commit 2708030

Please sign in to comment.