Skip to content

Commit

Permalink
If StatusCode return != 200 exit program immediately
Browse files Browse the repository at this point in the history
  • Loading branch information
warrensbox committed May 21, 2019
1 parent a55e6bf commit f628fc9
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 20 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,14 @@ 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-v4.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

1. Create a `.tfswitchrc` file containing the desired version
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
12 changes: 9 additions & 3 deletions lib/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ func init() {
func Install(tfversion string) {

if !ValidVersionFormat(tfversion) {
fmt.Printf("The provided terraform version does not exist - %s. Try `tfswitch -l` to see all available versions.\n", tfversion)
os.Exit(0)
fmt.Printf("The provided terraform version format does not exist - %s. Try `tfswitch -l` to see all available versions.\n", tfversion)
os.Exit(1)
}

goarch := runtime.GOARCH
Expand Down Expand Up @@ -84,7 +84,12 @@ func Install(tfversion string) {
/* if selected version already exist, */
/* proceed to download it from the hashicorp release page */
url := hashiURL + tfversion + "/" + installVersion + tfversion + "_" + goos + "_" + goarch + ".zip"
zipFile, _ := DownloadFromURL(installLocation, url)
zipFile, errDownload := DownloadFromURL(installLocation, url)

if errDownload != nil {
fmt.Println(errDownload)
os.Exit(1)
}

/* unzip the downloaded zipfile */
_, errUnzip := Unzip(zipFile, installLocation)
Expand All @@ -110,6 +115,7 @@ func Install(tfversion string) {
/* set symlink to desired version */
CreateSymlink(installLocation+installVersion+tfversion, installedBinPath)
fmt.Printf("Switched terraform to version %q \n", tfversion)
AddRecent(tfversion) //add to recent file for faster lookup
os.Exit(0)
}

Expand Down
17 changes: 7 additions & 10 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package main

/*
* Version 0.5.0
* Version 0.6.0
* Compatible with Mac OS X ONLY
*/

Expand Down Expand Up @@ -33,7 +33,7 @@ const (
hashiURL = "https://releases.hashicorp.com/terraform/"
)

var version = "0.5.0\n"
var version = "0.6.0\n"

func main() {
listAllFlag := getopt.BoolLong("list-all", 'l', "list all versions of terraform - including beta and rc")
Expand Down Expand Up @@ -70,14 +70,13 @@ func main() {
exist := lib.VersionExist(requestedVersion, tflist) //check if version exist before downloading it

if exist {
lib.AddRecent(requestedVersion) //add to recent file for faster lookup
lib.Install(requestedVersion)
} else {
log.Println("The provided terraform version does not exist. Try `tfswitch -l` to see all available versions.")
fmt.Println("The provided terraform version does not exist. Try `tfswitch -l` to see all available versions.")
}

} else {
log.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")
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")
fmt.Println("Args must be a valid terraform version")
usageMessage()
}
Expand All @@ -87,17 +86,16 @@ func main() {

fileContents, err := ioutil.ReadFile(rcfile)
if err != nil {
log.Println("Failed to read .tfswitchrc file. Follow the README.md instructions for setup. https://github.com/warrensbox/terraform-switcher/blob/master/README.md")
log.Printf("Error: %s\n", err)
fmt.Println("Failed to read .tfswitchrc file. Follow the README.md instructions for setup. https://github.com/warrensbox/terraform-switcher/blob/master/README.md")
fmt.Printf("Error: %s\n", err)
os.Exit(1)
}
tfversion := strings.TrimSuffix(string(fileContents), "\n")

if lib.ValidVersionFormat(tfversion) { //check if version is correct
lib.AddRecent(string(tfversion)) //add to RECENT file for faster lookup
lib.Install(string(tfversion))
} else {
log.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")
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)
}
} else if len(args) == 0 { //if there are no commmand line arguments
Expand Down Expand Up @@ -141,6 +139,5 @@ func installOption(listAll bool) {
os.Exit(1)
}

lib.AddRecent(tfversion) //add to recent file for faster lookup
lib.Install(tfversion)
}

0 comments on commit f628fc9

Please sign in to comment.