Skip to content

Commit 7272162

Browse files
committed
Feature: Add flag for install location (optional)
Add the ability to pass -i or --install to change the default install location for the Terraform binaries
1 parent c0bc923 commit 7272162

File tree

5 files changed

+125
-99
lines changed

5 files changed

+125
-99
lines changed

README.md

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,30 +13,36 @@ The installation is minimal and easy.
1313
Once installed, simply select the version you require from the dropdown and start using Terraform.
1414

1515
## Documentation
16+
1617
Click [here](https://tfswitch.warrensbox.com) for our extended documentation.
1718

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

2426
## Installation
25-
`tfswitch` is available as a binary and on various package managers (eg. Homebrew).
27+
28+
`tfswitch` is available as a binary and on various package managers (eg. Homebrew).
2629

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

3236
## Homebrew
37+
3338
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>.
3439

3540
```ruby
3641
brew install warrensbox/tap/tfswitch
3742
```
3843

3944
## Linux
45+
4046
Installation for Linux operating systems.
4147

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

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

62-
> [!IMPORTANT]
68+
> [!IMPORTANT]
6369
> The version identifier of `tfswitch` has changed to align with the common `go` package versioning.
6470
>
6571
> Version numbers will now be prefixed with a `v` - eg. `v1.0.3`.
6672
>
67-
> Please change any automated implementations relying on the `tfswitch` version string.
73+
> Please change any automated implementations relying on the `tfswitch` version string.
6874
>
6975
> **Old version string:** `0.1.2412`
7076
> **New version string:** `v1.0.3`
7177
7278
[Having trouble installing](https://tfswitch.warrensbox.com/Troubleshoot/)
7379

7480
## Quick Start
81+
7582
### Dropdown Menu
83+
7684
Execute `tfswitch` and select the desired Terraform version via the dropdown menu.
85+
7786
### Version on command line
87+
7888
Use `tfswitch 1.7.0` to install Terraform version 1.7.0. Replace the version number as required.
7989

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

8292
## How to contribute
83-
An open source project becomes meaningful when people collaborate to improve the code.
93+
94+
An open source project becomes meaningful when people collaborate to improve the code.
8495
Feel free to look at the code, critique and make suggestions. Let's make `tfswitch` better!
8596

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

8899
## Additional Info
100+
89101
See how to *upgrade*, *uninstall*, *troubleshoot* here: [More info](https://tfswitch.warrensbox.com/Upgrade-or-Uninstall/)
90102

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

lib/install.go

Lines changed: 17 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import (
1414
const (
1515
installFile = "terraform"
1616
versionPrefix = "terraform_"
17-
installPath = ".terraform.versions"
17+
installDir = ".terraform.versions"
1818
recentFile = "RECENT"
1919
defaultBin = "/usr/local/bin/terraform" //default bin installation dir
2020
tfDarwinArm64StartVersion = "1.0.2"
@@ -48,19 +48,10 @@ func initialize(binPath string) {
4848
}
4949

5050
// GetInstallLocation : get location where the terraform binary will be installed,
51-
// will create a directory in the home location if it does not exist
52-
func GetInstallLocation() string {
53-
/* get current user */
54-
homedir, errCurr := homedir.Dir()
55-
if errCurr != nil {
56-
logger.Fatal(errCurr)
57-
os.Exit(1)
58-
}
59-
60-
userCommon := homedir
61-
51+
// will create the installDir if it does not exist
52+
func GetInstallLocation(installPath string) string {
6253
/* set installation location */
63-
installLocation = filepath.Join(userCommon, installPath)
54+
installLocation = filepath.Join(installPath, installDir)
6455

6556
/* Create local installation directory if it does not exist */
6657
CreateDirIfNotExist(installLocation)
@@ -70,16 +61,16 @@ func GetInstallLocation() string {
7061
}
7162

7263
// Install : Install the provided version in the argument
73-
func Install(tfversion string, binPath string, mirrorURL string) {
64+
func Install(tfversion string, binPath string, installPath string, mirrorURL string) {
7465
/* Check to see if user has permission to the default bin location which is "/usr/local/bin/terraform"
7566
* If user does not have permission to default bin location, proceed to create $HOME/bin and install the tfswitch there
7667
* Inform user that they don't have permission to default location, therefore tfswitch was installed in $HOME/bin
7768
* Tell users to add $HOME/bin to their path
7869
*/
7970
binPath = InstallableBinLocation(binPath)
8071

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

8475
goarch := runtime.GOARCH
8576
goos := runtime.GOOS
@@ -108,7 +99,7 @@ func Install(tfversion string, binPath string, mirrorURL string) {
10899
/* set symlink to desired version */
109100
CreateSymlink(installFileVersionPath, binPath)
110101
logger.Infof("Switched terraform to version %q", tfversion)
111-
AddRecent(tfversion) //add to recent file for faster lookup
102+
AddRecent(tfversion, installPath) //add to recent file for faster lookup
112103
os.Exit(0)
113104
}
114105

@@ -152,14 +143,14 @@ func Install(tfversion string, binPath string, mirrorURL string) {
152143
/* set symlink to desired version */
153144
CreateSymlink(installFileVersionPath, binPath)
154145
logger.Infof("Switched terraform to version %q", tfversion)
155-
AddRecent(tfversion) //add to recent file for faster lookup
146+
AddRecent(tfversion, installPath) //add to recent file for faster lookup
156147
os.Exit(0)
157148
}
158149

159150
// AddRecent : add to recent file
160-
func AddRecent(requestedVersion string) {
151+
func AddRecent(requestedVersion string, installPath string) {
161152

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

165156
fileExist := CheckFileExist(versionFile)
@@ -175,7 +166,7 @@ func AddRecent(requestedVersion string) {
175166
if !ValidVersionFormat(line) {
176167
logger.Infof("File %q is dirty (recreating cache file)", versionFile)
177168
RemoveFiles(versionFile)
178-
CreateRecentFile(requestedVersion)
169+
CreateRecentFile(requestedVersion, installPath)
179170
return
180171
}
181172
}
@@ -195,14 +186,14 @@ func AddRecent(requestedVersion string) {
195186
}
196187

197188
} else {
198-
CreateRecentFile(requestedVersion)
189+
CreateRecentFile(requestedVersion, installPath)
199190
}
200191
}
201192

202193
// GetRecentVersions : get recent version from file
203-
func GetRecentVersions() ([]string, error) {
194+
func GetRecentVersions(installPath string) ([]string, error) {
204195

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

208199
fileExist := CheckFileExist(versionFile)
@@ -239,9 +230,9 @@ func GetRecentVersions() ([]string, error) {
239230
}
240231

241232
// CreateRecentFile : create a recent file
242-
func CreateRecentFile(requestedVersion string) {
233+
func CreateRecentFile(requestedVersion string, installPath string) {
243234

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

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

lib/symlink.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,6 @@ func CheckSymlink(symlinkPath string) bool {
9191
// ChangeSymlink : move symlink to existing binary
9292
func ChangeSymlink(binVersionPath string, binPath string) {
9393

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

9796
/* remove current symlink if exist*/

0 commit comments

Comments
 (0)