Skip to content

Commit

Permalink
Merge pull request #37 from warrensbox/develop
Browse files Browse the repository at this point in the history
Develop - New feature
-Option to automatically switch to desired Terraform version by reading .tfswitchrc file. Similar idea to nvm. (by @MarcusNoble ) .  
-Unit test for validating correct terraform version .  
-Update symlink error message to be more intuitive
  • Loading branch information
warrensbox authored Apr 8, 2019
2 parents 151d44e + 1611f13 commit d4385dd
Show file tree
Hide file tree
Showing 13 changed files with 290 additions and 66 deletions.
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,8 @@ docs/vendor/**

docs/.bundle/**

.sass-cache
.sass-cache

.tfswitchrc

tfswitch
29 changes: 21 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,25 @@ The most recently selected versions are presented at the top of the dropdown.
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 bash**

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

**Automatically switch with zsh**

Add the following to the end of your `~/.zshrc` file:

```
load-tfswitch() {
local tfswitchrc_path=".tfswitchrc"
Expand All @@ -78,18 +94,15 @@ add-zsh-hook chpwd load-tfswitch
load-tfswitch
```

**Automatically switch with bash**

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

## Additional Info
Expand Down
4 changes: 2 additions & 2 deletions docs/_layouts/default.html
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,12 @@

{{ content }}
{% if site.github.is_project_page %}
<p><small>VERSION: {{ site.github.releases[0].tag_name }}. This project is maintained by <a href="{{ site.github.owner_url }}">{{ site.github.owner_name }}</a></small></p>
<p><small>This project is maintained by <a href="{{ site.github.owner_url }}">{{ site.github.owner_name }}</a></small></p>
{% endif %}
</section>
<footer>
<!-- <p><small>View other products &mdash; by <a href="http://warren.veerasingam.com/#about_me" target="_blank">Warren Veerasingam</a></small></p> -->
<p><small>VERSION: {{ site.github.releases[0].tag_name }}. This project is maintained by <a href="{{ site.github.owner_url }}">{{ site.github.owner_name }}</a></small></p>
<p><small>This project is maintained by <a href="{{ site.github.owner_url }}">{{ site.github.owner_name }}</a></small></p>
</footer>
</div>
<script src="{{ "/assets/js/scale.fix.js" | relative_url }}"></script>
Expand Down
18 changes: 10 additions & 8 deletions docs/_site/additional.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,14 @@
<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" />
<link rel="canonical" href="http://localhost:4000/additional.html" />
<meta property="og:url" content="http://localhost:4000/additional.html" />
<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":"Manage terraform versions - the tfswitch command line tool lets you switch between different versions of terraform","@type":"WebPage","url":"http://localhost:4000/additional.html","publisher":{"@type":"Organization","logo":{"@type":"ImageObject","url":"http://localhost:4000/assets/img/logo.png"}},"headline":"tfswitch","@context":"http://schema.org"}</script>
<!-- End Jekyll SEO tag -->

<link rel="stylesheet" href="/assets/css/style.css?v=3113bfa1fc6d5c3ee7ee24ef78c5e90c3232615c">
<link rel="stylesheet" href="/assets/css/style.css?v=1ead8e0ef849a39a64a66a55ecab86069398dcfd">
<!--[if lt IE 9]>
<script src="//cdnjs.cloudflare.com/ajax/libs/html5shiv/3.7.3/html5shiv.min.js"></script>
<![endif]-->
Expand All @@ -41,8 +43,8 @@
<body>
<div class="wrapper">
<header>
<!-- <h1 style="font-size:25px;"><a href="/">tfswitch</a></h1> -->
<!-- <h1 style="font-size:25px; text-align: center"><a href="/">tfswitch</a></h1> -->
<!-- <h1 style="font-size:25px;"><a href="http://localhost:4000/">tfswitch</a></h1> -->
<!-- <h1 style="font-size:25px; text-align: center"><a href="http://localhost:4000/">tfswitch</a></h1> -->

<p align="center">
<img align="middle" src="https://s3.us-east-2.amazonaws.com/kepler-images/warrensbox/tfswitch/logo.png" alt="Logo" />
Expand All @@ -57,8 +59,8 @@


<ul>
<li><a href="https://github.com/warrensbox/terraform-switcher/releases/download/0.4.595/terraform-switcher_0.4.595_darwin_amd64.tar.gz">Download <strong>MacOS</strong></a></li>
<li><a href="https://github.com/warrensbox/terraform-switcher/releases/download/0.4.595/terraform-switcher_0.4.595_linux_amd64.tar.gz">Download <strong>Linux</strong></a></li>
<li><a href="https://github.com/warrensbox/terraform-switcher/releases/download/0.4.611/terraform-switcher_0.4.611_darwin_amd64.tar.gz">Download <strong>MacOS</strong></a></li>
<li><a href="https://github.com/warrensbox/terraform-switcher/releases/download/0.4.611/terraform-switcher_0.4.611_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 @@ -111,12 +113,12 @@ <h2 id="troubleshoot">Troubleshoot:</h2>
<a href="index">Back to main</a></p>


<p><small>VERSION: 0.4.595. This project is maintained by <a href="http://github.com/warrensbox">warrensbox</a></small></p>
<p><small>This project is maintained by <a href="http://github.com/warrensbox">warrensbox</a></small></p>

</section>
<footer>
<!-- <p><small>View other products &mdash; by <a href="http://warren.veerasingam.com/#about_me" target="_blank">Warren Veerasingam</a></small></p> -->
<p><small>VERSION: 0.4.595. This project is maintained by <a href="http://github.com/warrensbox">warrensbox</a></small></p>
<p><small>This project is maintained by <a href="http://github.com/warrensbox">warrensbox</a></small></p>
</footer>
</div>
<script src="/assets/js/scale.fix.js"></script>
Expand Down
64 changes: 56 additions & 8 deletions docs/_site/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,14 @@
<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" />
<link rel="canonical" href="http://localhost:4000/" />
<meta property="og:url" content="http://localhost:4000/" />
<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":"Manage terraform versions - the tfswitch command line tool lets you switch between different versions of terraform","@type":"WebSite","url":"http://localhost:4000/","publisher":{"@type":"Organization","logo":{"@type":"ImageObject","url":"http://localhost:4000/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=3113bfa1fc6d5c3ee7ee24ef78c5e90c3232615c">
<link rel="stylesheet" href="/assets/css/style.css?v=1ead8e0ef849a39a64a66a55ecab86069398dcfd">
<!--[if lt IE 9]>
<script src="//cdnjs.cloudflare.com/ajax/libs/html5shiv/3.7.3/html5shiv.min.js"></script>
<![endif]-->
Expand All @@ -41,8 +43,8 @@
<body>
<div class="wrapper">
<header>
<!-- <h1 style="font-size:25px;"><a href="/">tfswitch</a></h1> -->
<!-- <h1 style="font-size:25px; text-align: center"><a href="/">tfswitch</a></h1> -->
<!-- <h1 style="font-size:25px;"><a href="http://localhost:4000/">tfswitch</a></h1> -->
<!-- <h1 style="font-size:25px; text-align: center"><a href="http://localhost:4000/">tfswitch</a></h1> -->

<p align="center">
<img align="middle" src="https://s3.us-east-2.amazonaws.com/kepler-images/warrensbox/tfswitch/logo.png" alt="Logo" />
Expand All @@ -57,8 +59,8 @@


<ul>
<li><a href="https://github.com/warrensbox/terraform-switcher/releases/download/0.4.595/terraform-switcher_0.4.595_darwin_amd64.tar.gz">Download <strong>MacOS</strong></a></li>
<li><a href="https://github.com/warrensbox/terraform-switcher/releases/download/0.4.595/terraform-switcher_0.4.595_linux_amd64.tar.gz">Download <strong>Linux</strong></a></li>
<li><a href="https://github.com/warrensbox/terraform-switcher/releases/download/0.4.611/terraform-switcher_0.4.611_darwin_amd64.tar.gz">Download <strong>MacOS</strong></a></li>
<li><a href="https://github.com/warrensbox/terraform-switcher/releases/download/0.4.611/terraform-switcher_0.4.611_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 @@ -119,6 +121,52 @@ <h3 id="supply-version-on-command-line">Supply version on command line</h3>
<li>Hit <strong>Enter</strong> to switch.</li>
</ol>

<h3 id="use-tfswitchrc-file">Use .tfswitchrc file</h3>

<ol>
<li>Create a <code class="highlighter-rouge">.tfswitchrc</code> file containing the desired version</li>
<li>For example, <code class="highlighter-rouge">echo "0.10.5" &gt;&gt; .tfswitchrc</code> for version 0.10.5 of terraform</li>
<li>Run the command <code class="highlighter-rouge">tfswitch</code> in the same directory as your <code class="highlighter-rouge">.tfswitchrc</code></li>
</ol>

<p><strong>Automatically switch with bash</strong></p>

<p>Add the following to the end of your <code class="highlighter-rouge">~/.bashrc</code> file:</p>
<div class="highlighter-rouge"><div class="highlight"><pre class="highlight"><code>cdtfswitch(){
builtin cd "$@";
cdir=$PWD;
if [ -f "$cdir/.tfswitchrc" ]; then
tfswitch
fi
}
alias cd='cdtfswitch'
</code></pre></div></div>

<p><strong>Automatically switch with zsh</strong></p>

<p>Add the following to the end of your <code class="highlighter-rouge">~/.zshrc</code> file:</p>

<div class="highlighter-rouge"><div class="highlight"><pre class="highlight"><code>load-tfswitch() {
local tfswitchrc_path=".tfswitchrc"

if [ -f "$tfswitchrc_path" ]; then
tfswitch
fi
}
add-zsh-hook chpwd load-tfswitch
load-tfswitch
</code></pre></div></div>

<p><em>older version of zsh</em></p>
<div class="highlighter-rouge"><div class="highlight"><pre class="highlight"><code>cd(){
builtin cd "$@";
cdir=$PWD;
if [ -f "$cdir/.tfswitchrc" ]; then
tfswitch
fi
}
</code></pre></div></div>

<hr />

<h2 id="issues">Issues</h2>
Expand All @@ -131,12 +179,12 @@ <h2 id="issues">Issues</h2>
<a href="additional">Additional Info</a></p>


<p><small>VERSION: 0.4.595. This project is maintained by <a href="http://github.com/warrensbox">warrensbox</a></small></p>
<p><small>This project is maintained by <a href="http://github.com/warrensbox">warrensbox</a></small></p>

</section>
<footer>
<!-- <p><small>View other products &mdash; by <a href="http://warren.veerasingam.com/#about_me" target="_blank">Warren Veerasingam</a></small></p> -->
<p><small>VERSION: 0.4.595. This project is maintained by <a href="http://github.com/warrensbox">warrensbox</a></small></p>
<p><small>This project is maintained by <a href="http://github.com/warrensbox">warrensbox</a></small></p>
</footer>
</div>
<script src="/assets/js/scale.fix.js"></script>
Expand Down
48 changes: 48 additions & 0 deletions docs/_site/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,54 @@ 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 bash**

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

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

*older version of zsh*
```
cd(){
builtin cd "$@";
cdir=$PWD;
if [ -f "$cdir/.tfswitchrc" ]; then
tfswitch
fi
}
```

<hr>

## Issues
Expand Down
48 changes: 48 additions & 0 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,54 @@ 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 bash**

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

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

*older version of zsh*
```
cd(){
builtin cd "$@";
cdir=$PWD;
if [ -f "$cdir/.tfswitchrc" ]; then
tfswitch
fi
}
```

<hr>

## Issues
Expand Down
10 changes: 3 additions & 7 deletions lib/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"log"
"os"
"os/user"
"regexp"
"runtime"
)

Expand Down Expand Up @@ -112,8 +111,6 @@ func Install(tfversion string) {
// AddRecent : add to recent file
func AddRecent(requestedVersion string) {

semverRegex := regexp.MustCompile(`\A\d+(\.\d+){2}\z`)

fileExist := CheckFileExist(installLocation + recentFile)
if fileExist {
lines, errRead := ReadLines(installLocation + recentFile)
Expand All @@ -124,8 +121,8 @@ func AddRecent(requestedVersion string) {
}

for _, line := range lines {
if !semverRegex.MatchString(line) {
fmt.Println("file corrupted")
if !ValidVersionFormat(line) {
fmt.Println("File dirty. Recreating cache file.")
RemoveFiles(installLocation + recentFile)
CreateRecentFile(requestedVersion)
return
Expand Down Expand Up @@ -156,7 +153,6 @@ func GetRecentVersions() ([]string, error) {

fileExist := CheckFileExist(installLocation + recentFile)
if fileExist {
semverRegex := regexp.MustCompile(`\A\d+(\.\d+){2}\z`)

lines, errRead := ReadLines(installLocation + recentFile)

Expand All @@ -166,7 +162,7 @@ func GetRecentVersions() ([]string, error) {
}

for _, line := range lines {
if !semverRegex.MatchString(line) {
if !ValidVersionFormat(line) {
RemoveFiles(installLocation + recentFile)
return nil, errRead
}
Expand Down
Loading

0 comments on commit d4385dd

Please sign in to comment.