Skip to content

Commit 778ef83

Browse files
authored
Merge pull request #130 from warrensbox/master
Provide option to pass environment variable
2 parents b83d98e + 7272aeb commit 778ef83

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+4619
-231
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
EXE := tfswitch
22
PKG := github.com/warrensbox/terraform-switcher
3-
VER := $(shell git ls-remote --tags git://github.com/warrensbox/terraform-switcher | awk '{print $$2}'| awk -F"/" '{print $$3}' | sort -n -t. -k1,1 -k2,2 -k3,3 | tail -n 1)
3+
VER := $(shell git ls-remote --tags git://github.com/warrensbox/terraform-switcher | awk '{print $$2}'| awk -F"/" '{print $$3}' | sort -n -t. -k1,1 -k2,2 -k3,3 | tail -n 2 | head -n1)
44
PATH := build:$(PATH)
55
GOOS ?= $(shell go env GOOS)
66
GOARCH ?= $(shell go env GOARCH)

README.md

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,16 @@ The most recently selected versions are presented at the top of the dropdown.
7070
2. For example, `tfswitch -l` or `tfswitch --list-all` to see all versions.
7171
3. Hit **Enter** to select the desired version.
7272

73+
### Use environment variable
74+
You can also set the `TF_VERSION` environment version to your desired terraform version.
75+
For example:
76+
```bash
77+
export TF_VERSION=0.14.4
78+
tfswitch #will automatically switch to terraform version 0.14.4
79+
```
7380
### Install latest version only
7481
1. Install the latest stable version only.
75-
2. Run `tfswitch -u` or `tfswitch --latest` to see all versions.
82+
2. Run `tfswitch -u` or `tfswitch --latest`.
7683
3. Hit **Enter** to install.
7784
### Install latest implicit version for stable releases
7885
1. Install the latest implicit stable version.
@@ -96,7 +103,6 @@ terraform {
96103
```
97104
<img src="https://s3.us-east-2.amazonaws.com/kepler-images/warrensbox/tfswitch/versiontf.gif#1" alt="drawing" style="width: 370px;"/>
98105

99-
100106
### Use .tfswitch.toml file (For non-admin - users with limited privilege on their computers)
101107
This is similiar to using a .tfswitchrc file, but you can specify a custom binary path for your terraform installation
102108

@@ -123,7 +129,7 @@ version = "0.11.3"
123129
3. Run the command `tfswitch` in the same directory as your `.tfswitchrc`
124130

125131
#### *Instead of a `.tfswitchrc` file, a `.terraform-version` file may be used for compatibility with [`tfenv`](https://github.com/tfutils/tfenv#terraform-version-file) and other tools which use it*
126-
132+
## Automation
127133
**Automatically switch with bash**
128134

129135
Add the following to the end of your `~/.bashrc` file:
@@ -256,7 +262,17 @@ jobs:
256262

257263
terraform -v #testing version
258264
```
265+
## Order of precedence
266+
267+
| Order | Method |
268+
| --- | ----------- |
269+
| 1 | .tfswitch.toml |
270+
| 2 | .tfswitchrc |
271+
| 3 | .terraform-version |
272+
| 4 | Environment variable |
259273
274+
With 1 being the highest precedence and 4 the lowest
275+
*(If you disagree with this order of precedence, please open an issue)*
260276
## How to contribute
261277
An open source project becomes meaningful when people collaborate to improve the code.
262278
Feel free to look at the code, critique and make suggestions. Lets make `tfswitch` better!

go.sum538389371.tmp

Lines changed: 0 additions & 221 deletions
This file was deleted.

main.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,29 +106,44 @@ func main() {
106106
}
107107

108108
switch {
109+
/* GIVEN A TOML FILE, */
110+
/* show all terraform version including betas and RCs*/
109111
case *listAllFlag:
110112
listAll := true //set list all true - all versions including beta and rc will be displayed
111113
installOption(listAll, &binPath)
114+
/* latest pre-release implicit version. Ex: tfswitch --latest-pre 0.13 downloads 0.13.0-rc1 (latest) */
112115
case *latestPre != "":
113116
preRelease := true
114117
installLatestImplicitVersion(*latestPre, custBinPath, preRelease)
118+
/* latest implicit version. Ex: tfswitch --latest 0.13 downloads 0.13.5 (latest) */
115119
case *latestStable != "":
116120
preRelease := false
117121
installLatestImplicitVersion(*latestStable, custBinPath, preRelease)
122+
/* latest stable version */
118123
case *latestFlag:
119124
installLatestVersion(custBinPath)
125+
/* version provided on command line as arg */
120126
case len(args) == 1:
121127
installVersion(args[0], &binPath)
128+
/* provide an tfswitchrc file (IN ADDITION TO A TOML FILE) */
122129
case fileExists(RCFile) && len(args) == 0:
123130
readingFileMsg(rcFilename)
124131
tfversion := retrieveFileContents(RCFile)
125132
installVersion(tfversion, &binPath)
133+
/* if .terraform-version file found (IN ADDITION TO A TOML FILE) */
126134
case fileExists(TFVersionFile) && len(args) == 0:
127135
readingFileMsg(tfvFilename)
128136
tfversion := retrieveFileContents(TFVersionFile)
129137
installVersion(tfversion, &binPath)
138+
/* if versions.tf file found (IN ADDITION TO A TOML FILE) */
130139
case checkTFModuleFileExist(dir) && len(args) == 0:
131140
installTFProvidedModule(dir, &binPath)
141+
/* if Terraform Version environment variable is set */
142+
case checkTFEnvExist() && len(args) == 0 && version == "":
143+
tfversion := os.Getenv("TF_VERSION")
144+
fmt.Printf("Terraform version environment variable: %s\n", tfversion)
145+
installVersion(tfversion, custBinPath)
146+
// if no arg is provided - but toml file is provided
132147
case version != "":
133148
installVersion(version, &binPath)
134149
default:
@@ -174,6 +189,12 @@ func main() {
174189
case checkTFModuleFileExist(dir) && len(args) == 0:
175190
installTFProvidedModule(dir, custBinPath)
176191

192+
/* if Terraform Version environment variable is set */
193+
case checkTFEnvExist() && len(args) == 0:
194+
tfversion := os.Getenv("TF_VERSION")
195+
fmt.Printf("Terraform version environment variable: %s\n", tfversion)
196+
installVersion(tfversion, custBinPath)
197+
177198
// if no arg is provided
178199
default:
179200
listAll := false //set list all false - only official release will be displayed
@@ -273,6 +294,15 @@ func checkTFModuleFileExist(dir string) bool {
273294
return false
274295
}
275296

297+
// checkTFEnvExist - checks if the TF_VERSION environment variable is set
298+
func checkTFEnvExist() bool {
299+
tfversion := os.Getenv("TF_VERSION")
300+
if tfversion != "" {
301+
return true
302+
}
303+
return false
304+
}
305+
276306
/* parses everything in the toml file, return required version and bin path */
277307
func getParamsTOML(binPath string, dir string) (string, string) {
278308
path, _ := homedir.Dir()

snapcraft.yaml

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
name: tfswitch
2-
version: git
2+
version: '0.10.958'
33
summary: A command line tool to switch between different versions of terraform
44
description: |
55
The `tfswitch` command line tool lets you switch between different versions of terraform(https://www.terraform.io/).
66
If you do not have a particular version of terraform installed, `tfswitch` will download the version you desire.
77
The installation is minimal and easy.
88
Once installed, simply select the version you require from the dropdown and start using terraform.
99
architectures:
10-
- build-on: arm64
10+
- build-on: [amd64,arm64]
11+
run-on: [amd64,arm64]
1112
assumes: [snapd2.45]
1213
base: core18
1314

@@ -27,9 +28,9 @@ parts:
2728
source: .
2829
plugin: go
2930
go-importpath: github.com/warrensbox/terraform-switcher
30-
build-packages:
31-
- gcc-multilib
31+
build-packages: [gcc, g++, make]
3232
go-buildtags:
3333
- tfswitch
3434
override-build:
35-
go build -o ../install/bin/tfswitch
35+
VER=$(git ls-remote --tags git://github.com/warrensbox/terraform-switcher | awk '{print $2}'| awk -F"/" '{print $3}' | sort -n -t. -k1,1 -k2,2 -k3,3 | tail -n 2 | head -n1)
36+
go build -v -ldflags "-X main.version=0.10.958" -o ../install/bin/tfswitch

www/docs/Quick-Start.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,16 @@ The most recently selected versions are presented at the top of the dropdown.
2222
1. Display all versions including beta, alpha and release candidates(rc).
2323
2. For example, `tfswitch -l` or `tfswitch --list-all` to see all versions.
2424
3. Hit **Enter** to select the desired version.
25+
### Use environment variables
26+
You can also set the `TF_VERSION` environment version to your desired terraform version.
27+
For example:
28+
```bash
29+
export TF_VERSION=0.14.4
30+
tfswitch #will automatically switch to terraform version 0.14.4
31+
```
2532
### Install latest version only
2633
1. Install the latest stable version only.
27-
2. Run `tfswitch -u` or `tfswitch --latest` to see all versions.
34+
2. Run `tfswitch -u` or `tfswitch --latest`.
2835
3. Hit **Enter** to install.
2936
### Install latest implicit version for stable releases
3037
1. Install the latest implicit stable version.

0 commit comments

Comments
 (0)