Skip to content

Commit

Permalink
Merge pull request #36 from MarcusNoble/feature/tfswitchrc
Browse files Browse the repository at this point in the history
Added support for a .tfswitchrc file
  • Loading branch information
warrensbox authored Apr 5, 2019
2 parents 58520b2 + 38e3488 commit 151d44e
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 11 deletions.
28 changes: 24 additions & 4 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

49 changes: 42 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@
[![Go Report Card](https://goreportcard.com/badge/github.com/warrensbox/terraform-switcher)](https://goreportcard.com/report/github.com/warrensbox/terraform-switcher)
[![CircleCI](https://circleci.com/gh/warrensbox/terraform-switcher/tree/master.svg?style=shield&circle-token=55ddceec95ff67eb38269152282f8a7d761c79a5)](https://circleci.com/gh/warrensbox/terraform-switcher)

# Terraform Switcher
# Terraform Switcher

<img style="text-allign:center" src="https://s3.us-east-2.amazonaws.com/kepler-images/warrensbox/tfswitch/smallerlogo.png" alt="drawing" width="120" height="130"/>

<!-- ![gopher](https://s3.us-east-2.amazonaws.com/kepler-images/warrensbox/tfswitch/logo.png =100x20) -->

The `tfswitch` command line tool lets you switch between different versions of [terraform](https://www.terraform.io/).
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.
The installation is minimal and easy.
Once installed, simply select the version you require from the dropdown and start using terraform.

See installation guide here: [tfswitch installation](https://warrensbox.github.io/terraform-switcher/)

Expand All @@ -21,7 +21,7 @@ See installation guide here: [tfswitch installation](https://warrensbox.github.i

### Homebrew

Installation for MacOS is the easiest with Homebrew. [If you do not have homebrew installed, click here](https://brew.sh/).
Installation for MacOS is the easiest with Homebrew. [If you do not have homebrew installed, click here](https://brew.sh/).


```ruby
Expand All @@ -38,13 +38,13 @@ curl -L https://raw.githubusercontent.com/warrensbox/terraform-switcher/release/

### Install from source

Alternatively, you can install the binary from source [here](https://github.com/warrensbox/terraform-switcher/releases)
Alternatively, you can install the binary from source [here](https://github.com/warrensbox/terraform-switcher/releases)

## How to use:
### Use dropdown menu to select version
<img src="https://s3.us-east-2.amazonaws.com/kepler-images/warrensbox/tfswitch/tfswitch.gif" alt="drawing" style="width: 180px;"/>

1. You can switch between different versions of terraform by typing the command `tfswitch` on your terminal.
1. You can switch between different versions of terraform by typing the command `tfswitch` on your terminal.
2. Select the version of terraform you require by using the up and down arrow.
3. Hit **Enter** to select the desired version.

Expand All @@ -57,6 +57,41 @@ The most recently selected versions are presented at the top of the dropdown.
2. For example, `tfswitch 0.10.5` for version 0.10.5 of terraform.
3. Hit **Enter** to switch.

### Use .tfswitchrc file

1. Create a `.tfswitchrc` file containing the desired version
2. For example, `echo "0.10.5" >> .tfswitchrc` for version 0.10.5 of terraform
3. Run the command `tfswitch` in the same directory as your `.tfswitchrc`

**Automatically switch with zsh**

Add the following to the end of your `~/.zshrc` file:
```
load-tfswitch() {
local tfswitchrc_path=".tfswitchrc"
if [ -f "$tfswitchrc_path" ]; then
tfswitch
fi
}
add-zsh-hook chpwd load-tfswitch
load-tfswitch
```

**Automatically switch with bash**

Add the following to the end of your `~/.bashrc` file:
```
cdtfswitch(){
cd "$@";
if [ -f ".tfswitchrc" ]; then
tfswitch
fi
}
alias cd='cdtfswitch'
```

## Additional Info

See how to *upgrade*, *uninstall*, *troubleshoot* here:[More info](https://warrensbox.github.io/terraform-switcher/additional)
Expand Down
20 changes: 20 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@ package main

import (
"fmt"
"io/ioutil"
"log"
"os"
"strings"

"regexp"

Expand All @@ -43,6 +45,13 @@ func main() {
getopt.Parse()
args := getopt.Args()

dir, err := os.Getwd()
if err != nil {
log.Printf("Failed to get current directory %v\n", err)
os.Exit(1)
}
rcfile := dir + "/.tfswitchrc"

if *versionFlag {
fmt.Printf("\nVersion: %v\n", version)
} else if *helpFlag {
Expand Down Expand Up @@ -71,6 +80,17 @@ func main() {
usageMessage()
}

} else if _, err := os.Stat(rcfile); err == nil {

file_contents, err := ioutil.ReadFile(rcfile)
if err != nil {
log.Printf("Failed to read .tfswitchrc %v\n", err)
os.Exit(1)
}
tfversion := strings.TrimSuffix(string(file_contents), "\n")

lib.AddRecent(string(tfversion)) //add to recent file for faster lookup
lib.Install(string(tfversion))
} else if len(args) == 0 {

tflist, _ := lib.GetTFList(hashiURL)
Expand Down

0 comments on commit 151d44e

Please sign in to comment.