Skip to content

Commit

Permalink
Merge pull request #74 from warrensbox/develop
Browse files Browse the repository at this point in the history
Changes to allow tfswitch to read version.tf file
  • Loading branch information
warrensbox committed Apr 16, 2020
2 parents b5b3321 + 63eae29 commit bd1e075
Show file tree
Hide file tree
Showing 7 changed files with 93 additions and 41 deletions.
18 changes: 16 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
```
<img src="https://s3.us-east-2.amazonaws.com/kepler-images/warrensbox/tfswitch/versiontf.gif" alt="drawing" style="width: 170px;"/>


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

<img src="https://s3.us-east-2.amazonaws.com/kepler-images/warrensbox/tfswitch/tfswitch-v7.gif" alt="drawing" style="width: 170px;"/>
<img src="https://s3.us-east-2.amazonaws.com/kepler-images/warrensbox/tfswitch/tfswitch-v7.gif" alt="drawing" style="width: 170px;"/>
<img src="https://s3.us-east-2.amazonaws.com/kepler-images/warrensbox/tfswitch/tfswitch-v8.gif" alt="drawing" style="width: 170px;"/>

1. Create a custom binary path. Ex: `mkdir /Users/warrenveerasingam/bin` (replace warrenveerasingam with your username)
Expand All @@ -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**

Expand Down
16 changes: 8 additions & 8 deletions docs/_site/additional.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,18 @@
<link rel="icon" type="image/png" href="https://s3.us-east-2.amazonaws.com/kepler-images/warrensbox/tfswitch/favicon_tfswitch_48.png" sizes="48x48">

<!-- Begin Jekyll SEO tag v2.5.0 -->
<title>tfswitch | Manage terraform versions - the tfswitch command line tool lets you switch between different versions of terraform</title>
<title>tfswitch | A command line tool to switch between different versions of terraform (with homebrew and more)</title>
<meta name="generator" content="Jekyll v3.7.4" />
<meta property="og:title" content="tfswitch" />
<meta property="og:locale" content="en_US" />
<meta name="description" content="Manage terraform versions - the tfswitch command line tool lets you switch between different versions of terraform" />
<meta property="og:description" content="Manage terraform versions - the tfswitch command line tool lets you switch between different versions of terraform" />
<meta name="description" content="A command line tool to switch between different versions of terraform (with homebrew and more)" />
<meta property="og:description" content="A command line tool to switch between different versions of terraform (with homebrew and more)" />
<meta property="og:site_name" content="tfswitch" />
<script type="application/ld+json">
{"description":"Manage terraform versions - the tfswitch command line tool lets you switch between different versions of terraform","@type":"WebPage","url":"/additional.html","publisher":{"@type":"Organization","logo":{"@type":"ImageObject","url":"/assets/img/logo.png"}},"headline":"tfswitch","@context":"http://schema.org"}</script>
{"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"}</script>
<!-- End Jekyll SEO tag -->

<link rel="stylesheet" href="/assets/css/style.css?v=0b7be9932efc2fc12b75f7de015e535bb13fda26">
<link rel="stylesheet" href="/assets/css/style.css?v=b5b33214feb3a399d34b298c9f042c338c11df4f">
<!--[if lt IE 9]>
<script src="//cdnjs.cloudflare.com/ajax/libs/html5shiv/3.7.3/html5shiv.min.js"></script>
<![endif]-->
Expand Down Expand Up @@ -51,7 +51,7 @@
<img align="middle" src="https://s3.us-east-2.amazonaws.com/kepler-images/warrensbox/tfswitch/logo.png" alt="Logo" />
</p>

<p style="text-align: center">Manage terraform versions - the tfswitch command line tool lets you switch between different versions of terraform</p>
<p style="text-align: center">A command line tool to switch between different versions of terraform (with homebrew and more)</p>
<!-- <p>Written in go</p> -->
<img src="https://travis-ci.org/warrensbox/terraform-switcher.svg?branch=master" alt="Travis" />
<img src="https://goreportcard.com/badge/github.com/warrensbox/terraform-switcher" alt="Go Report" />
Expand All @@ -60,8 +60,8 @@


<ul>
<li><a href="https://github.com/warrensbox/terraform-switcher/releases/download/0.7.737/terraform-switcher_0.7.737_darwin_amd64.tar.gz">Download <strong>MacOS</strong></a></li>
<li><a href="https://github.com/warrensbox/terraform-switcher/releases/download/0.7.737/terraform-switcher_0.7.737_linux_amd64.tar.gz">Download <strong>Linux</strong></a></li>
<li><a href="https://github.com/warrensbox/terraform-switcher/releases/download/0.7.817/terraform-switcher_0.7.817_darwin_amd64.tar.gz">Download <strong>MacOS</strong></a></li>
<li><a href="https://github.com/warrensbox/terraform-switcher/releases/download/0.7.817/terraform-switcher_0.7.817_linux_amd64.tar.gz">Download <strong>Linux</strong></a></li>
<li><a href="https://github.com/warrensbox/terraform-switcher/releases">Releases On <strong>GitHub</strong></a></li>
</ul>

Expand Down
26 changes: 19 additions & 7 deletions docs/_site/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@
<meta name="generator" content="Jekyll v3.7.4" />
<meta property="og:title" content="Terraform Switcher" />
<meta property="og:locale" content="en_US" />
<meta name="description" content="Manage terraform versions - the tfswitch command line tool lets you switch between different versions of terraform" />
<meta property="og:description" content="Manage terraform versions - the tfswitch command line tool lets you switch between different versions of terraform" />
<meta name="description" content="A command line tool to switch between different versions of terraform (with homebrew and more)" />
<meta property="og:description" content="A command line tool to switch between different versions of terraform (with homebrew and more)" />
<meta property="og:site_name" content="tfswitch" />
<script type="application/ld+json">
{"name":"tfswitch","description":"Manage terraform versions - the tfswitch command line tool lets you switch between different versions of terraform","@type":"WebSite","url":"/","publisher":{"@type":"Organization","logo":{"@type":"ImageObject","url":"/assets/img/logo.png"}},"headline":"Terraform Switcher","@context":"http://schema.org"}</script>
{"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"}</script>
<!-- End Jekyll SEO tag -->

<link rel="stylesheet" href="/assets/css/style.css?v=0b7be9932efc2fc12b75f7de015e535bb13fda26">
<link rel="stylesheet" href="/assets/css/style.css?v=b5b33214feb3a399d34b298c9f042c338c11df4f">
<!--[if lt IE 9]>
<script src="//cdnjs.cloudflare.com/ajax/libs/html5shiv/3.7.3/html5shiv.min.js"></script>
<![endif]-->
Expand Down Expand Up @@ -51,7 +51,7 @@
<img align="middle" src="https://s3.us-east-2.amazonaws.com/kepler-images/warrensbox/tfswitch/logo.png" alt="Logo" />
</p>

<p style="text-align: center">Manage terraform versions - the tfswitch command line tool lets you switch between different versions of terraform</p>
<p style="text-align: center">A command line tool to switch between different versions of terraform (with homebrew and more)</p>
<!-- <p>Written in go</p> -->
<img src="https://travis-ci.org/warrensbox/terraform-switcher.svg?branch=master" alt="Travis" />
<img src="https://goreportcard.com/badge/github.com/warrensbox/terraform-switcher" alt="Go Report" />
Expand All @@ -60,8 +60,8 @@


<ul>
<li><a href="https://github.com/warrensbox/terraform-switcher/releases/download/0.7.737/terraform-switcher_0.7.737_darwin_amd64.tar.gz">Download <strong>MacOS</strong></a></li>
<li><a href="https://github.com/warrensbox/terraform-switcher/releases/download/0.7.737/terraform-switcher_0.7.737_linux_amd64.tar.gz">Download <strong>Linux</strong></a></li>
<li><a href="https://github.com/warrensbox/terraform-switcher/releases/download/0.7.817/terraform-switcher_0.7.817_darwin_amd64.tar.gz">Download <strong>MacOS</strong></a></li>
<li><a href="https://github.com/warrensbox/terraform-switcher/releases/download/0.7.817/terraform-switcher_0.7.817_linux_amd64.tar.gz">Download <strong>Linux</strong></a></li>
<li><a href="https://github.com/warrensbox/terraform-switcher/releases">Releases On <strong>GitHub</strong></a></li>
</ul>

Expand Down Expand Up @@ -134,6 +134,18 @@ <h3 id="see-all-versions-including-beta-alpha-and-release-candidatesrc">See all
<li>Hit <strong>Enter</strong> to select the desired version.</li>
</ol>

<h3 id="use-versiontf-file">Use version.tf file</h3>
<p>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 <code class="highlighter-rouge">0.12.24</code>:</p>
<div class="highlighter-rouge"><div class="highlight"><pre class="highlight"><code>terraform {
required_version = "&gt;= 0.12.9"

required_providers {
aws = "&gt;= 2.52.0"
kubernetes = "&gt;= 1.11.1"
}
}
</code></pre></div></div>
<p><img src="https://s3.us-east-2.amazonaws.com/kepler-images/warrensbox/tfswitch/versiontf.gif" alt="drawing" style="width: 170px;" /></p>
<h3 id="use-tfswitchtoml-file--for-non-admin---users-with-limited-privilege-on-their-computers">Use .tfswitch.toml file (For non-admin - users with limited privilege on their computers)</h3>
<p>This is similiar to using a .tfswitchrc file, but you can specify a custom binary path for your terraform installation</p>

Expand Down
13 changes: 13 additions & 0 deletions docs/_site/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
```
<img src="https://s3.us-east-2.amazonaws.com/kepler-images/warrensbox/tfswitch/versiontf.gif" alt="drawing" style="width: 170px;"/>
### 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 Down
13 changes: 13 additions & 0 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
```
<img src="https://s3.us-east-2.amazonaws.com/kepler-images/warrensbox/tfswitch/versiontf.gif" alt="drawing" style="width: 170px;"/>
### 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 Down
46 changes: 23 additions & 23 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ const (
tomlFilename = ".tfswitch.toml"
)

var version = "0.7.0\n"
var version = "0.8.0\n"

func main() {

Expand Down Expand Up @@ -130,49 +130,49 @@ 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)
}
}
}

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
Expand Down
2 changes: 1 addition & 1 deletion version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
RELEASE_VERSION=0.7
RELEASE_VERSION=0.8

0 comments on commit bd1e075

Please sign in to comment.