Skip to content

Commit

Permalink
Merge pull request #130 from warrensbox/master
Browse files Browse the repository at this point in the history
Provide option to pass environment variable
  • Loading branch information
warrensbox committed Feb 5, 2021
2 parents b83d98e + 7272aeb commit 778ef83
Show file tree
Hide file tree
Showing 54 changed files with 4,619 additions and 231 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
EXE := tfswitch
PKG := github.com/warrensbox/terraform-switcher
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)
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)
PATH := build:$(PATH)
GOOS ?= $(shell go env GOOS)
GOARCH ?= $(shell go env GOARCH)
Expand Down
22 changes: 19 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,16 @@ The most recently selected versions are presented at the top of the dropdown.
2. For example, `tfswitch -l` or `tfswitch --list-all` to see all versions.
3. Hit **Enter** to select the desired version.

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


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

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

#### *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*

## Automation
**Automatically switch with bash**

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

terraform -v #testing version
```
## Order of precedence
| Order | Method |
| --- | ----------- |
| 1 | .tfswitch.toml |
| 2 | .tfswitchrc |
| 3 | .terraform-version |
| 4 | Environment variable |
With 1 being the highest precedence and 4 the lowest
*(If you disagree with this order of precedence, please open an issue)*
## How to contribute
An open source project becomes meaningful when people collaborate to improve the code.
Feel free to look at the code, critique and make suggestions. Lets make `tfswitch` better!
Expand Down
221 changes: 0 additions & 221 deletions go.sum538389371.tmp

This file was deleted.

30 changes: 30 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,29 +106,44 @@ func main() {
}

switch {
/* GIVEN A TOML FILE, */
/* show all terraform version including betas and RCs*/
case *listAllFlag:
listAll := true //set list all true - all versions including beta and rc will be displayed
installOption(listAll, &binPath)
/* latest pre-release implicit version. Ex: tfswitch --latest-pre 0.13 downloads 0.13.0-rc1 (latest) */
case *latestPre != "":
preRelease := true
installLatestImplicitVersion(*latestPre, custBinPath, preRelease)
/* latest implicit version. Ex: tfswitch --latest 0.13 downloads 0.13.5 (latest) */
case *latestStable != "":
preRelease := false
installLatestImplicitVersion(*latestStable, custBinPath, preRelease)
/* latest stable version */
case *latestFlag:
installLatestVersion(custBinPath)
/* version provided on command line as arg */
case len(args) == 1:
installVersion(args[0], &binPath)
/* provide an tfswitchrc file (IN ADDITION TO A TOML FILE) */
case fileExists(RCFile) && len(args) == 0:
readingFileMsg(rcFilename)
tfversion := retrieveFileContents(RCFile)
installVersion(tfversion, &binPath)
/* if .terraform-version file found (IN ADDITION TO A TOML FILE) */
case fileExists(TFVersionFile) && len(args) == 0:
readingFileMsg(tfvFilename)
tfversion := retrieveFileContents(TFVersionFile)
installVersion(tfversion, &binPath)
/* if versions.tf file found (IN ADDITION TO A TOML FILE) */
case checkTFModuleFileExist(dir) && len(args) == 0:
installTFProvidedModule(dir, &binPath)
/* if Terraform Version environment variable is set */
case checkTFEnvExist() && len(args) == 0 && version == "":
tfversion := os.Getenv("TF_VERSION")
fmt.Printf("Terraform version environment variable: %s\n", tfversion)
installVersion(tfversion, custBinPath)
// if no arg is provided - but toml file is provided
case version != "":
installVersion(version, &binPath)
default:
Expand Down Expand Up @@ -174,6 +189,12 @@ func main() {
case checkTFModuleFileExist(dir) && len(args) == 0:
installTFProvidedModule(dir, custBinPath)

/* if Terraform Version environment variable is set */
case checkTFEnvExist() && len(args) == 0:
tfversion := os.Getenv("TF_VERSION")
fmt.Printf("Terraform version environment variable: %s\n", tfversion)
installVersion(tfversion, custBinPath)

// if no arg is provided
default:
listAll := false //set list all false - only official release will be displayed
Expand Down Expand Up @@ -273,6 +294,15 @@ func checkTFModuleFileExist(dir string) bool {
return false
}

// checkTFEnvExist - checks if the TF_VERSION environment variable is set
func checkTFEnvExist() bool {
tfversion := os.Getenv("TF_VERSION")
if tfversion != "" {
return true
}
return false
}

/* parses everything in the toml file, return required version and bin path */
func getParamsTOML(binPath string, dir string) (string, string) {
path, _ := homedir.Dir()
Expand Down
11 changes: 6 additions & 5 deletions snapcraft.yaml
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
name: tfswitch
version: git
version: '0.10.958'
summary: A command line tool to switch between different versions of terraform
description: |
The `tfswitch` command line tool lets you switch between different versions of terraform(https://www.terraform.io/).
If you do not have a particular version of terraform installed, `tfswitch` will download the version you desire.
The installation is minimal and easy.
Once installed, simply select the version you require from the dropdown and start using terraform.
architectures:
- build-on: arm64
- build-on: [amd64,arm64]
run-on: [amd64,arm64]
assumes: [snapd2.45]
base: core18

Expand All @@ -27,9 +28,9 @@ parts:
source: .
plugin: go
go-importpath: github.com/warrensbox/terraform-switcher
build-packages:
- gcc-multilib
build-packages: [gcc, g++, make]
go-buildtags:
- tfswitch
override-build:
go build -o ../install/bin/tfswitch
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)
go build -v -ldflags "-X main.version=0.10.958" -o ../install/bin/tfswitch
9 changes: 8 additions & 1 deletion www/docs/Quick-Start.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,16 @@ The most recently selected versions are presented at the top of the dropdown.
1. Display all versions including beta, alpha and release candidates(rc).
2. For example, `tfswitch -l` or `tfswitch --list-all` to see all versions.
3. Hit **Enter** to select the desired version.
### Use environment variables
You can also set the `TF_VERSION` environment version to your desired terraform version.
For example:
```bash
export TF_VERSION=0.14.4
tfswitch #will automatically switch to terraform version 0.14.4
```
### Install latest version only
1. Install the latest stable version only.
2. Run `tfswitch -u` or `tfswitch --latest` to see all versions.
2. Run `tfswitch -u` or `tfswitch --latest`.
3. Hit **Enter** to install.
### Install latest implicit version for stable releases
1. Install the latest implicit stable version.
Expand Down

0 comments on commit 778ef83

Please sign in to comment.