Skip to content

Commit

Permalink
Merge pull request #45 from warrensbox/master
Browse files Browse the repository at this point in the history
Adding new feature
  • Loading branch information
warrensbox authored May 22, 2019
2 parents c7ef74a + 093f9fb commit bcdfdee
Show file tree
Hide file tree
Showing 16 changed files with 304 additions and 112 deletions.
4 changes: 2 additions & 2 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ brew:
skip_upload: false

# Packages that conflict with your package.
# conflicts:
# - terraform
conflicts:
- terraform

# So you can `brew test` your formula.
# Default is empty.
Expand Down
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,16 @@ 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.

### See all versions including beta, alpha and release candidates(rc)
<img src="https://s3.us-east-2.amazonaws.com/kepler-images/warrensbox/tfswitch/tfswitch-v5.gif" alt="drawing" style="width: 170px;"/>

1. Display all versions including beta, alpha and release candidates(rc).
2. For example, `tfswitch -l` or `tfswitch --list-all` to see all versions.
3. Hit **Enter** to select the desired version.


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

1. Create a `.tfswitchrc` file containing the desired version
2. For example, `echo "0.10.5" >> .tfswitchrc` for version 0.10.5 of terraform
Expand Down
14 changes: 6 additions & 8 deletions docs/_site/additional.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,12 @@
<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":"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>
{"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>
<!-- End Jekyll SEO tag -->

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


<ul>
<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/download/0.5.642/terraform-switcher_0.5.642_darwin_amd64.tar.gz">Download <strong>MacOS</strong></a></li>
<li><a href="https://github.com/warrensbox/terraform-switcher/releases/download/0.5.642/terraform-switcher_0.5.642_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
30 changes: 19 additions & 11 deletions docs/_site/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,12 @@
<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":"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>
{"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>
<!-- End Jekyll SEO tag -->

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


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

<h3 id="see-all-versions-including-beta-alpha-and-release-candidatesrc">See all versions including beta, alpha and release candidates(rc)</h3>
<p><img src="https://s3.us-east-2.amazonaws.com/kepler-images/warrensbox/tfswitch/tfswitch-v5.gif" alt="drawing" style="width: 490px;" /></p>

<ol>
<li>Display all versions including beta, alpha and release candidates(rc).</li>
<li>For example, <code class="highlighter-rouge">tfswitch -l</code> or <code class="highlighter-rouge">tfswitch --list-all</code> to see all versions.</li>
<li>Hit <strong>Enter</strong> to select the desired version.</li>
</ol>

<h3 id="use-tfswitchrc-file">Use .tfswitchrc file</h3>
<p><img src="https://s3.us-east-2.amazonaws.com/kepler-images/warrensbox/tfswitch/tfswitch-v6.gif" alt="drawing" style="width: 490px;" /></p>

<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>
<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>
Expand Down
15 changes: 12 additions & 3 deletions docs/_site/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,20 @@ 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.

### See all versions including beta, alpha and release candidates(rc)
<img src="https://s3.us-east-2.amazonaws.com/kepler-images/warrensbox/tfswitch/tfswitch-v5.gif" alt="drawing" style="width: 490px;"/>

1. Display all versions including beta, alpha and release candidates(rc).
2. For example, `tfswitch -l` or `tfswitch --list-all` to see all versions.
3. Hit **Enter** to select the desired version.


### Use .tfswitchrc file
<img src="https://s3.us-east-2.amazonaws.com/kepler-images/warrensbox/tfswitch/tfswitch-v6.gif" alt="drawing" style="width: 490px;"/>

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`
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**
Expand Down
15 changes: 12 additions & 3 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,20 @@ 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.

### See all versions including beta, alpha and release candidates(rc)
<img src="https://s3.us-east-2.amazonaws.com/kepler-images/warrensbox/tfswitch/tfswitch-v5.gif" alt="drawing" style="width: 490px;"/>

1. Display all versions including beta, alpha and release candidates(rc).
2. For example, `tfswitch -l` or `tfswitch --list-all` to see all versions.
3. Hit **Enter** to select the desired version.


### Use .tfswitchrc file
<img src="https://s3.us-east-2.amazonaws.com/kepler-images/warrensbox/tfswitch/tfswitch-v6.gif" alt="drawing" style="width: 490px;"/>

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`
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**
Expand Down
4 changes: 2 additions & 2 deletions lib/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ func createFile(path string) {

func createDirIfNotExist(dir string) {
if _, err := os.Stat(dir); os.IsNotExist(err) {
log.Printf("Creating directory for teraform: %v", dir)
log.Printf("Creating directory for terraform: %v", dir)
err = os.MkdirAll(dir, 0755)
if err != nil {
fmt.Printf("Unable to create directory for teraform: %v", dir)
fmt.Printf("Unable to create directory for terraform: %v", dir)
panic(err)
}
}
Expand Down
21 changes: 14 additions & 7 deletions lib/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,28 @@ func DownloadFromURL(installLocation string, url string) (string, error) {
fileName := tokens[len(tokens)-1]
fmt.Println("Downloading", url, "to", fileName)
fmt.Println("Downloading ...")
// TODO: check file existence first with io.IsExist
output, err := os.Create(installLocation + fileName)
if err != nil {
fmt.Println("Error while creating", installLocation+fileName, "-", err)
return "", err
}
defer output.Close()

response, err := http.Get(url)

if err != nil {
fmt.Println("Error while downloading", url, "-", err)
return "", err
}
defer response.Body.Close()

if response.StatusCode != 200 {
//Sometimes hashicorp terraform file names are not consistent
//For example 0.12.0-alpha4 naming convention in the release repo is not consistent
return "", fmt.Errorf("Unable to download from %s\nPlease download manually from https://releases.hashicorp.com/terraform/", url)
}

output, err := os.Create(installLocation + fileName)
if err != nil {
fmt.Println("Error while creating", installLocation+fileName, "-", err)
return "", err
}
defer output.Close()

n, err := io.Copy(output, response.Body)
if err != nil {
fmt.Println("Error while downloading", url, "-", err)
Expand Down
89 changes: 75 additions & 14 deletions lib/download_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,25 @@ func TestDownloadFromURL_FileNameMatch(t *testing.T) {

// create /.terraform.versions_test/ directory to store code
if _, err := os.Stat(installLocation); os.IsNotExist(err) {
log.Printf("Creating directory for teraform: %v", installLocation)
log.Printf("Creating directory for terraform: %v", installLocation)
err = os.MkdirAll(installLocation, 0755)
if err != nil {
fmt.Printf("Unable to create directory for teraform: %v", installLocation)
fmt.Printf("Unable to create directory for terraform: %v", installLocation)
panic(err)
}
}

/* test download lowest terraform version */
lowestVersion := "0.0.1"
lowestVersion := "0.1.0"

url := hashiURL + lowestVersion + "/" + installVersion + lowestVersion + macOS
expectedFile := usr.HomeDir + installPath + installVersion + lowestVersion + macOS
installedFile, _ := lib.DownloadFromURL(installLocation, url)
installedFile, errDownload := lib.DownloadFromURL(installLocation, url)

if errDownload != nil {
t.Logf("Expected file name %v to be downloaded", expectedFile)
t.Error("Download not possible (unexpected)")
}

if installedFile == expectedFile {
t.Logf("Expected file %v", expectedFile)
Expand All @@ -52,15 +57,20 @@ func TestDownloadFromURL_FileNameMatch(t *testing.T) {
} else {
t.Logf("Expected file %v", expectedFile)
t.Logf("Downloaded file %v", installedFile)
t.Error("Download file mismatches expected file")
t.Error("Download file mismatches expected file (unexpected)")
}

/* test download latest terraform version */
latestVersion := "0.11.7"

url = hashiURL + latestVersion + "/" + installVersion + latestVersion + macOS
expectedFile = usr.HomeDir + installPath + installVersion + latestVersion + macOS
installedFile, _ = lib.DownloadFromURL(installLocation, url)
installedFile, errDownload = lib.DownloadFromURL(installLocation, url)

if errDownload != nil {
t.Logf("Expected file name %v to be downloaded", expectedFile)
t.Error("Download not possible (unexpected)")
}

if installedFile == expectedFile {
t.Logf("Expected file name %v", expectedFile)
Expand All @@ -69,7 +79,7 @@ func TestDownloadFromURL_FileNameMatch(t *testing.T) {
} else {
t.Logf("Expected file name %v", expectedFile)
t.Logf("Downloaded file name %v", installedFile)
t.Error("Downoad file name mismatches expected file")
t.Error("Dowload file name mismatches expected file (unexpected)")
}

cleanUp(installLocation)
Expand All @@ -95,20 +105,25 @@ func TestDownloadFromURL_FileExist(t *testing.T) {

// create /.terraform.versions_test/ directory to store code
if _, err := os.Stat(installLocation); os.IsNotExist(err) {
log.Printf("Creating directory for teraform: %v", installLocation)
log.Printf("Creating directory for terraform: %v", installLocation)
err = os.MkdirAll(installLocation, 0755)
if err != nil {
fmt.Printf("Unable to create directory for teraform: %v", installLocation)
fmt.Printf("Unable to create directory for terraform: %v", installLocation)
panic(err)
}
}

/* test download lowest terraform version */
lowestVersion := "0.0.1"
lowestVersion := "0.1.0"

url := hashiURL + lowestVersion + "/" + installVersion + lowestVersion + macOS
expectedFile := usr.HomeDir + installPath + installVersion + lowestVersion + macOS
installedFile, _ := lib.DownloadFromURL(installLocation, url)
installedFile, errDownload := lib.DownloadFromURL(installLocation, url)

if errDownload != nil {
t.Logf("Expected file name %v to be downloaded", expectedFile)
t.Error("Download not possible (unexpected)")
}

if checkFileExist(expectedFile) {
t.Logf("Expected file %v", expectedFile)
Expand All @@ -117,15 +132,20 @@ func TestDownloadFromURL_FileExist(t *testing.T) {
} else {
t.Logf("Expected file %v", expectedFile)
t.Logf("Downloaded file %v", installedFile)
t.Error("Downoad file mismatches expected file")
t.Error("Download file mismatches expected file (unexpected)")
}

/* test download latest terraform version */
latestVersion := "0.11.7"

url = hashiURL + latestVersion + "/" + installVersion + latestVersion + macOS
expectedFile = usr.HomeDir + installPath + installVersion + latestVersion + macOS
installFile, _ = lib.DownloadFromURL(installLocation, url)
installFile, errDownload = lib.DownloadFromURL(installLocation, url)

if errDownload != nil {
t.Logf("Expected file name %v to be downloaded", expectedFile)
t.Error("Download not possible (unexpected)")
}

if checkFileExist(expectedFile) {
t.Logf("Expected file %v", expectedFile)
Expand All @@ -134,12 +154,53 @@ func TestDownloadFromURL_FileExist(t *testing.T) {
} else {
t.Logf("Expected file %v", expectedFile)
t.Logf("Downloaded file %v", installFile)
t.Error("Downoad file mismatches expected file")
t.Error("Download file mismatches expected file (unexpected)")
}

cleanUp(installLocation)
}

// TestInvalidURL : Invalid url should throw an error
func TestInvalidURL(t *testing.T) {

hashiURL := "https://releases.hashicorp.com/terraform/"
installVersion := "terraform_"
installPath := "/.terraform.versions_test/"
macOS := "_darwin_amd64.zip"
invalidVersion := "0.11.7-nonexistent"

// get current user
usr, errCurr := user.Current()
if errCurr != nil {
log.Fatal(errCurr)
}

fmt.Printf("Current user: %v \n", usr.HomeDir)
installLocation := usr.HomeDir + installPath

// create /.terraform.versions_test/ directory to store code
if _, err := os.Stat(installLocation); os.IsNotExist(err) {
log.Printf("Creating directory for terraform: %v\n", installLocation)
err = os.MkdirAll(installLocation, 0755)
if err != nil {
fmt.Printf("Unable to create directory for terraform: %v\n", installLocation)
panic(err)
}
}

url := hashiURL + invalidVersion + "/" + installVersion + invalidVersion + macOS
//expectedFile := usr.HomeDir + installPath + installVersion + invalidVersion + macOS
_, errDownload := lib.DownloadFromURL(installLocation, url)

if errDownload != nil {
t.Logf("Unable to download from %s - invalid url or version (expected)\n", url)
t.Logf("Download not possible (expected)")
}

cleanUp(installLocation)
}

// TestDownloadFromURL_Valid : Test if https://releases.hashicorp.com/terraform/ is still valid
func TestDownloadFromURL_Valid(t *testing.T) {

hashiURL := "https://releases.hashicorp.com/terraform/"
Expand Down
Loading

0 comments on commit bcdfdee

Please sign in to comment.