From 63eae29903b4c2aafd10a66ac0f2fda3156f6691 Mon Sep 17 00:00:00 2001 From: "warren.veerasingam@gmail.com" Date: Thu, 16 Apr 2020 15:43:58 -0500 Subject: [PATCH] Changes to allow tfswitch to read version.tf file --- README.md | 18 +++++++++++++-- docs/_site/additional.html | 16 ++++++------- docs/_site/index.html | 26 +++++++++++++++------ docs/_site/index.md | 13 +++++++++++ docs/index.md | 13 +++++++++++ main.go | 46 +++++++++++++++++++------------------- version | 2 +- 7 files changed, 93 insertions(+), 41 deletions(-) diff --git a/README.md b/README.md index 2f5d02a5..e90ee4c7 100644 --- a/README.md +++ b/README.md @@ -64,11 +64,25 @@ 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 version.tf file +If a .tf file with the terraform constrain is included in the current directory, it should automatically download or switch to that terraform version. For example, the following should automatically switch terraform to version `0.12.24`: +``` +terraform { + required_version = ">= 0.12.9" + + required_providers { + aws = ">= 2.52.0" + kubernetes = ">= 1.11.1" + } +} +``` +drawing + ### 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 -drawing +drawing drawing 1. Create a custom binary path. Ex: `mkdir /Users/warrenveerasingam/bin` (replace warrenveerasingam with your username) @@ -89,7 +103,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* +#### *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** diff --git a/docs/_site/additional.html b/docs/_site/additional.html index 8f502959..3804fb02 100644 --- a/docs/_site/additional.html +++ b/docs/_site/additional.html @@ -12,18 +12,18 @@ -tfswitch | Manage terraform versions - the tfswitch command line tool lets you switch between different versions of terraform +tfswitch | A command line tool to switch between different versions of terraform (with homebrew and more) - - + + +{"description":"A command line tool to switch between different versions of terraform (with homebrew and more)","@type":"WebPage","url":"/additional.html","publisher":{"@type":"Organization","logo":{"@type":"ImageObject","url":"/assets/img/logo.png"}},"headline":"tfswitch","@context":"http://schema.org"} - + @@ -51,7 +51,7 @@ Logo

-

Manage terraform versions - the tfswitch command line tool lets you switch between different versions of terraform

+

A command line tool to switch between different versions of terraform (with homebrew and more)

Travis Go Report @@ -60,8 +60,8 @@ diff --git a/docs/_site/index.html b/docs/_site/index.html index 93cd3025..afbfaedd 100644 --- a/docs/_site/index.html +++ b/docs/_site/index.html @@ -16,14 +16,14 @@ - - + + +{"name":"tfswitch","description":"A command line tool to switch between different versions of terraform (with homebrew and more)","@type":"WebSite","url":"/","publisher":{"@type":"Organization","logo":{"@type":"ImageObject","url":"/assets/img/logo.png"}},"headline":"Terraform Switcher","@context":"http://schema.org"} - + @@ -51,7 +51,7 @@ Logo

-

Manage terraform versions - the tfswitch command line tool lets you switch between different versions of terraform

+

A command line tool to switch between different versions of terraform (with homebrew and more)

Travis Go Report @@ -60,8 +60,8 @@ @@ -134,6 +134,18 @@

See all
  • Hit Enter to select the desired version.
  • +

    Use version.tf file

    +

    If a .tf file with the terraform constrain is included in the current directory, it should automatically download or switch to that terraform version. For example, the following should automatically switch terraform to version 0.12.24:

    +
    terraform {
    +  required_version = ">= 0.12.9"
    +
    +  required_providers {
    +    aws        = ">= 2.52.0"
    +    kubernetes = ">= 1.11.1"
    +  }
    +}
    +
    +

    drawing

    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

    diff --git a/docs/_site/index.md b/docs/_site/index.md index 2450c92f..4b450544 100644 --- a/docs/_site/index.md +++ b/docs/_site/index.md @@ -58,6 +58,19 @@ 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 version.tf file +If a .tf file with the terraform constrain is included in the current directory, it should automatically download or switch to that terraform version. For example, the following should automatically switch terraform to version `0.12.24`: +``` +terraform { + required_version = ">= 0.12.9" + + required_providers { + aws = ">= 2.52.0" + kubernetes = ">= 1.11.1" + } +} +``` +drawing ### 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 diff --git a/docs/index.md b/docs/index.md index 2450c92f..4b450544 100644 --- a/docs/index.md +++ b/docs/index.md @@ -58,6 +58,19 @@ 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 version.tf file +If a .tf file with the terraform constrain is included in the current directory, it should automatically download or switch to that terraform version. For example, the following should automatically switch terraform to version `0.12.24`: +``` +terraform { + required_version = ">= 0.12.9" + + required_providers { + aws = ">= 2.52.0" + kubernetes = ">= 1.11.1" + } +} +``` +drawing ### 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 diff --git a/main.go b/main.go index 149b610c..c8a63951 100644 --- a/main.go +++ b/main.go @@ -47,7 +47,7 @@ const ( tomlFilename = ".tfswitch.toml" ) -var version = "0.7.0\n" +var version = "0.8.0\n" func main() { @@ -130,41 +130,41 @@ func main() { os.Exit(1) } } - } else if module, _ := tfconfig.LoadModule(dir); len(module.RequiredCore) >= 1 && len(args) == 0 { //if there is a .tfswitchrc file, and no commmand line arguments - tfversion := "" + } else if module, _ := tfconfig.LoadModule(dir); len(module.RequiredCore) >= 1 && len(args) == 0 { //if there is a version.tf file, and no commmand line arguments - // we skip duplicated definitions and use only first one - tfconstraint := module.RequiredCore[0] - tflist, _ := lib.GetTFList(hashiURL, true) - fmt.Printf("Reading required version from terraform code, constraint: %s\n", tfconstraint) + tfversion := "" + tfconstraint := module.RequiredCore[0] //we skip duplicated definitions and use only first one + listAll := true //set list all true - all versions including beta and rc will be displayed + tflist, _ := lib.GetTFList(hashiURL, listAll) //get list of versions + fmt.Printf("Reading required version from terraform file, constraint: %s\n", tfconstraint) - c, err := semver.NewConstraint(tfconstraint) + constrains, err := semver.NewConstraint(tfconstraint) //NewConstraint returns a Constraints instance that a Version instance can be checked against if err != nil { - fmt.Println("Error parsing constraint:", err) + fmt.Printf("Error parsing constraint: %s\nPlease check constrain syntax on terraform file.\n", err) + fmt.Println() os.Exit(1) } - vs := make([]*semver.Version, len(tflist)) - for i, r := range tflist { - v, err := semver.NewVersion(r) + versions := make([]*semver.Version, len(tflist)) + for i, tfvals := range tflist { + version, err := semver.NewVersion(tfvals) //NewVersion parses a given version and returns an instance of Version or an error if unable to parse the version. if err != nil { fmt.Printf("Error parsing version: %s", err) os.Exit(1) } - vs[i] = v + versions[i] = version } - sort.Sort(sort.Reverse(semver.Collection(vs))) + sort.Sort(sort.Reverse(semver.Collection(versions))) - for _, element := range vs { - // Validate a version against a constraint. - if c.Check(element) { - tfversion = string(element.String()) - fmt.Printf("Matched version: %s\n", tfversion) + for _, element := range versions { - if lib.ValidVersionFormat(tfversion) { - lib.Install(string(tfversion), *custBinPath) - break + if constrains.Check(element) { // Validate a version against a constraint + tfversion = element.String() + + fmt.Printf("Matched version: %s\n", tfversion) + if lib.ValidVersionFormat(tfversion) { //check if version format is correct + lib.Install(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) @@ -172,7 +172,7 @@ func main() { } } - fmt.Println("No version found to match constraint.") + fmt.Println("No version found to match constraint. Follow the README.md instructions for setup. https://github.com/warrensbox/terraform-switcher/blob/master/README.md") os.Exit(1) } else if _, err := os.Stat(rcfile); err == nil && len(args) == 0 { //if there is a .tfswitchrc file, and no commmand line arguments diff --git a/version b/version index 37f085c6..524a135d 100644 --- a/version +++ b/version @@ -1 +1 @@ -RELEASE_VERSION=0.7 \ No newline at end of file +RELEASE_VERSION=0.8 \ No newline at end of file