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 Apr 19, 2024
1 parent c0bc923 commit 7272162
Show file tree
Hide file tree
Showing 5 changed files with 125 additions and 99 deletions.
23 changes: 18 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,30 +13,36 @@ The installation is minimal and easy.
Once installed, simply select the version you require from the dropdown and start using Terraform.

## Documentation

Click [here](https://tfswitch.warrensbox.com) for our extended documentation.

## NOTE

Going forward we will change the version identifier of `tfswitch` to align with the common go package versioning.
Please be advised to change any automated implementation you might have that is relying on the `tfswitch` version string.
**Old version string:** `0.1.2412`
**New version string:** `v1.0.0` Note the `v` that is preceding all version numbers.

## Installation
`tfswitch` is available as a binary and on various package managers (eg. Homebrew).

`tfswitch` is available as a binary and on various package managers (eg. Homebrew).

## Windows

Download and extract the Windows version of `tfswitch` that is compatible with your system.
We are building binaries for 386, amd64, arm6 and arm7 CPU structure.
See the [release page](https://github.com/warrensbox/terraform-switcher/releases/latest) for your download.

## Homebrew

For macOS or various Linux distributions, Homebrew offers the simplest installation process. <a href="https://brew.sh/" target="_blank">If you do not have homebrew installed, click here</a>.

```ruby
brew install warrensbox/tap/tfswitch
```

## Linux

Installation for Linux operating systems.

```sh
Expand All @@ -59,34 +65,41 @@ Alternatively, you can install the binary from the source <a href="https://githu

See [our installation documentation](https://tfswitch.warrensbox.com/Install) for more details.

> [!IMPORTANT]
> [!IMPORTANT]
> The version identifier of `tfswitch` has changed to align with the common `go` package versioning.
>
> Version numbers will now be prefixed with a `v` - eg. `v1.0.3`.
>
> Please change any automated implementations relying on the `tfswitch` version string.
> Please change any automated implementations relying on the `tfswitch` version string.
>
> **Old version string:** `0.1.2412`
> **New version string:** `v1.0.3`
[Having trouble installing](https://tfswitch.warrensbox.com/Troubleshoot/)

## Quick Start

### Dropdown Menu

Execute `tfswitch` and select the desired Terraform version via the dropdown menu.

### Version on command line

Use `tfswitch 1.7.0` to install Terraform version 1.7.0. Replace the version number as required.

More [usage guide here](https://tfswitch.warrensbox.com/Quick-Start/)

## How to contribute
An open source project becomes meaningful when people collaborate to improve the code.

An open source project becomes meaningful when people collaborate to improve the code.
Feel free to look at the code, critique and make suggestions. Let's make `tfswitch` better!

See step-by-step instructions on how to contribute here: [Contribute](https://tfswitch.warrensbox.com/How-to-Contribute/)

## Additional Info

See how to *upgrade*, *uninstall*, *troubleshoot* here: [More info](https://tfswitch.warrensbox.com/Upgrade-or-Uninstall/)

## Issues
Please open *issues* here: [New Issue](https://github.com/warrensbox/terraform-switcher/issues)

Please open *issues* here: [New Issue](https://github.com/warrensbox/terraform-switcher/issues)
43 changes: 17 additions & 26 deletions lib/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
const (
installFile = "terraform"
versionPrefix = "terraform_"
installPath = ".terraform.versions"
installDir = ".terraform.versions"
recentFile = "RECENT"
defaultBin = "/usr/local/bin/terraform" //default bin installation dir
tfDarwinArm64StartVersion = "1.0.2"
Expand Down Expand Up @@ -48,19 +48,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 */
homedir, errCurr := homedir.Dir()
if errCurr != nil {
logger.Fatal(errCurr)
os.Exit(1)
}

userCommon := homedir

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

/* Create local installation directory if it does not exist */
CreateDirIfNotExist(installLocation)
Expand All @@ -70,16 +61,16 @@ 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) {
/* Check to see if user has permission to the default bin location which is "/usr/local/bin/terraform"
* If user does not have permission to default bin location, proceed to create $HOME/bin and install the tfswitch there
* Inform user that they don't have permission to default location, therefore tfswitch was installed in $HOME/bin
* Tell users to add $HOME/bin to their path
*/
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 @@ -108,7 +99,7 @@ func Install(tfversion string, binPath string, mirrorURL string) {
/* set symlink to desired version */
CreateSymlink(installFileVersionPath, binPath)
logger.Infof("Switched terraform to version %q", 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 @@ -152,14 +143,14 @@ func Install(tfversion string, binPath string, mirrorURL string) {
/* set symlink to desired version */
CreateSymlink(installFileVersionPath, binPath)
logger.Infof("Switched terraform to version %q", 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 @@ -175,7 +166,7 @@ func AddRecent(requestedVersion string) {
if !ValidVersionFormat(line) {
logger.Infof("File %q is dirty (recreating cache file)", versionFile)
RemoveFiles(versionFile)
CreateRecentFile(requestedVersion)
CreateRecentFile(requestedVersion, installPath)
return
}
}
Expand All @@ -195,14 +186,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 @@ -239,9 +230,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 @@ -91,7 +91,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 7272162

Please sign in to comment.