Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Develop #42

Merged
merged 11 commits into from
May 22, 2019
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=a476ee38cf2aae2f2729d25873970bf2743f832b">
<!--[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
14 changes: 6 additions & 8 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=a476ee38cf2aae2f2729d25873970bf2743f832b">
<!--[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
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
81 changes: 71 additions & 10 deletions lib/download_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,16 @@ func TestDownloadFromURL_FileNameMatch(t *testing.T) {
}

/* 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 Down Expand Up @@ -104,11 +114,16 @@ func TestDownloadFromURL_FileExist(t *testing.T) {
}

/* 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
9 changes: 5 additions & 4 deletions lib/files_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,8 @@ func TestWriteLines(t *testing.T) {

installPath := "/.terraform.versions_test/"
recentFile := "RECENT"
semverRegex := regexp.MustCompile(`\A\d+(\.\d+){2}\z`)
semverRegex := regexp.MustCompile(`\A\d+(\.\d+){2}(-\w+\d*)?\z`)
//semverRegex := regexp.MustCompile(`\A\d+(\.\d+){2}\z`)

usr, errCurr := user.Current()
if errCurr != nil {
Expand All @@ -184,7 +185,7 @@ func TestWriteLines(t *testing.T) {

createDirIfNotExist(installLocation)

test_array := []string{"0.0.1", "0.0.2", "0.0.3"}
test_array := []string{"0.1.1", "0.0.2", "0.0.3", "0.12.0-rc1", "0.12.0-beta1"}

errWrite := lib.WriteLines(test_array, installLocation+recentFile)

Expand Down Expand Up @@ -243,7 +244,7 @@ func TestWriteLines(t *testing.T) {
func TestReadLines(t *testing.T) {
installPath := "/.terraform.versions_test/"
recentFile := "RECENT"
semverRegex := regexp.MustCompile(`\A\d+(\.\d+){2}\z`)
semverRegex := regexp.MustCompile(`\A\d+(\.\d+){2}(-\w+\d*)?\z`)

usr, errCurr := user.Current()
if errCurr != nil {
Expand All @@ -253,7 +254,7 @@ func TestReadLines(t *testing.T) {

createDirIfNotExist(installLocation)

test_array := []string{"0.0.1", "0.0.2", "0.0.3"}
test_array := []string{"0.0.1", "0.0.2", "0.0.3", "0.12.0-rc1", "0.12.0-beta1"}

var (
file *os.File
Expand Down
Loading