Skip to content

Commit

Permalink
Feature: Add flag for install location (optional)
Browse files Browse the repository at this point in the history
Add the ability to pass -i or --install to change the default install location for the Terraform binaries
  • Loading branch information
ArronaxKP committed May 17, 2023
1 parent d6bbae6 commit f69d390
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 84 deletions.
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,23 @@ tfswitch -c terraform_dir
To install from a remote mirror other than the default(https://releases.hashicorp.com/terraform). Use the `-m` or `--mirror` parameter.
Ex: `tfswitch --mirror https://example.jfrog.io/artifactory/hashicorp`

### Install to non-default location

By default `tfswitch` will download the Terraform binary to the user home directory under this path: `/Users/warrenveerasingam/.terraform.versions`

If you want to install the binaries outside of the home directory then you can provide the `-i` or `--install` to install Terraform binaries to a non-standard path. Useful if you want to install versions of Terraform that can be shared with multiple users.

The Terraform binaries will then be placed in the folder `.terraform.versions` under the custom install path e.g. `/opt/terraform/.terraform.versions`

```bash
tfswitch -i /opt/terraform/
```

**NOTE**

* The folder must exists before you run `tfswitch`
* The folder passed in `-i`/`--install` must be created before running `tfswtich`

### Set a default TF version for CICD pipeline
1. When using a CICD pipeline, you may want a default or fallback version to avoid the pipeline from hanging.
2. Ex: `tfswitch -d 1.2.3` or `tfswitch --default 1.2.3` installs version `1.2.3` when no other versions could be detected.
Expand Down
42 changes: 17 additions & 25 deletions lib/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
const (
installFile = "terraform"
versionPrefix = "terraform_"
installPath = ".terraform.versions"
installFolder = ".terraform.versions"
recentFile = "RECENT"
defaultBin = "/usr/local/bin/terraform" //default bin installation dir
tfDarwinArm64StartVersion = "1.0.2"
Expand Down Expand Up @@ -49,18 +49,10 @@ func initialize(binPath string) {
}

// 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)
}

userCommon := usr.HomeDir

// will create the installFolder if it does not exist
func GetInstallLocation(installPath string) string {
/* set installation location */
installLocation = filepath.Join(userCommon, installPath)
installLocation = filepath.Join(installPath, installFolder)

/* Create local installation directory if it does not exist */
CreateDirIfNotExist(installLocation)
Expand All @@ -70,7 +62,7 @@ func GetInstallLocation() string {
}

// Install : Install the provided version in the argument
func Install(tfversion string, binPath string, mirrorURL string) {
func Install(tfversion string, binPath string, installPath 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)
Expand All @@ -84,8 +76,8 @@ func Install(tfversion string, binPath string, mirrorURL string) {
*/
binPath = InstallableBinLocation(binPath)

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

goarch := runtime.GOARCH
goos := runtime.GOOS
Expand Down Expand Up @@ -114,7 +106,7 @@ func Install(tfversion string, binPath string, mirrorURL string) {
/* set symlink to desired version */
CreateSymlink(installFileVersionPath, binPath)
fmt.Printf("Switched terraform to version %q \n", tfversion)
AddRecent(tfversion) //add to recent file for faster lookup
AddRecent(tfversion, installPath) //add to recent file for faster lookup
os.Exit(0)
}

Expand Down Expand Up @@ -160,14 +152,14 @@ func Install(tfversion string, binPath string, mirrorURL string) {
/* set symlink to desired version */
CreateSymlink(installFileVersionPath, binPath)
fmt.Printf("Switched terraform to version %q \n", tfversion)
AddRecent(tfversion) //add to recent file for faster lookup
AddRecent(tfversion, installPath) //add to recent file for faster lookup
os.Exit(0)
}

// AddRecent : add to recent file
func AddRecent(requestedVersion string) {
func AddRecent(requestedVersion string, installPath string) {

installLocation = GetInstallLocation() //get installation location - this is where we will put our terraform binary file
installLocation = GetInstallLocation(installPath) //get installation location - this is where we will put our terraform binary file
versionFile := filepath.Join(installLocation, recentFile)

fileExist := CheckFileExist(versionFile)
Expand All @@ -183,7 +175,7 @@ func AddRecent(requestedVersion string) {
if !ValidVersionFormat(line) {
fmt.Println("File dirty. Recreating cache file.")
RemoveFiles(versionFile)
CreateRecentFile(requestedVersion)
CreateRecentFile(requestedVersion, installPath)
return
}
}
Expand All @@ -203,14 +195,14 @@ func AddRecent(requestedVersion string) {
}

} else {
CreateRecentFile(requestedVersion)
CreateRecentFile(requestedVersion, installPath)
}
}

// GetRecentVersions : get recent version from file
func GetRecentVersions() ([]string, error) {
func GetRecentVersions(installPath string) ([]string, error) {

installLocation = GetInstallLocation() //get installation location - this is where we will put our terraform binary file
installLocation = GetInstallLocation(installPath) //get installation location - this is where we will put our terraform binary file
versionFile := filepath.Join(installLocation, recentFile)

fileExist := CheckFileExist(versionFile)
Expand Down Expand Up @@ -247,9 +239,9 @@ func GetRecentVersions() ([]string, error) {
}

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

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

WriteLines([]string{requestedVersion}, filepath.Join(installLocation, recentFile))
}
Expand Down
1 change: 0 additions & 1 deletion lib/symlink.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ func CheckSymlink(symlinkPath string) bool {
// ChangeSymlink : move symlink to existing binary
func ChangeSymlink(binVersionPath string, binPath string) {

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

/* remove current symlink if exist*/
Expand Down
Loading

0 comments on commit f69d390

Please sign in to comment.