Skip to content

Commit

Permalink
Merge branch 'master' into develop
Browse files Browse the repository at this point in the history
Added features by other contributors
  • Loading branch information
warrensbox committed Mar 3, 2020
2 parents ae66905 + e7a6865 commit 8f538af
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
3 changes: 2 additions & 1 deletion README.md
Expand Up @@ -89,6 +89,7 @@ version = "0.11.3"
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`

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

**Automatically switch with bash**

Expand Down Expand Up @@ -139,7 +140,7 @@ cd(){

## Additional Info

See how to *upgrade*, *uninstall*, *troubleshoot* here:[More info](https://warrensbox.github.io/terraform-switcher/additional)
See how to *upgrade*, *uninstall*, *troubleshoot* here: [More info](https://warrensbox.github.io/terraform-switcher/additional)


## Issues
Expand Down
21 changes: 20 additions & 1 deletion main.go
Expand Up @@ -33,6 +33,7 @@ import (
const (
hashiURL = "https://releases.hashicorp.com/terraform/"
defaultBin = "/usr/local/bin/terraform" //default bin installation dir
tfvFilename = ".terraform-version"
rcFilename = ".tfswitchrc"
tomlFilename = ".tfswitch.toml"
)
Expand All @@ -56,6 +57,7 @@ func main() {
os.Exit(1)
}

tfvfile := dir + fmt.Sprintf("/%s", tfvFilename) //settings for .terraform-version file in current directory (tfenv compatible)
rcfile := dir + fmt.Sprintf("/%s", rcFilename) //settings for .tfswitchrc file in current directory (backward compatible purpose)
configfile := dir + fmt.Sprintf("/%s", tomlFilename) //settings for .tfswitch.toml file in current directory (option to specify bin directory)

Expand Down Expand Up @@ -88,7 +90,7 @@ func main() {

bin := viper.Get("bin") // read custom binary location
if binPath == defaultBin && bin != nil { // if the bin path is the same as the default binary path and if the custom binary is provided in the toml file (use it)
binPath = bin.(string)
binPath = os.ExpandEnv(bin.(string))
}
version := viper.Get("version") //attempt to get the version if it's provided in the toml

Expand Down Expand Up @@ -131,6 +133,23 @@ func main() {
}
tfversion := strings.TrimSuffix(string(fileContents), "\n")

if lib.ValidVersionFormat(tfversion) { //check if version is correct
lib.Install(string(tfversion), *custBinPath)
} else {
fmt.Println("Invalid terraform version format. Format should be #.#.# or #.#.#-@# where # is numbers and @ is word characters. For example, 0.11.7 and 0.11.9-beta1 are valid versions")
os.Exit(1)
}
} else if _, err := os.Stat(tfvfile); err == nil && len(args) == 0 { //if there is a .terraform-version file, and no command line arguments
fmt.Printf("Reading required terraform version %s ", tfvFilename)

fileContents, err := ioutil.ReadFile(tfvfile)
if err != nil {
fmt.Printf("Failed to read %s file. Follow the README.md instructions for setup. https://github.com/warrensbox/terraform-switcher/blob/master/README.md\n", tfvFilename)
fmt.Printf("Error: %s\n", err)
os.Exit(1)
}
tfversion := strings.TrimSuffix(string(fileContents), "\n")

if lib.ValidVersionFormat(tfversion) { //check if version is correct
lib.Install(string(tfversion), *custBinPath)
} else {
Expand Down

0 comments on commit 8f538af

Please sign in to comment.