Compare commits

..

No commits in common. "master" and "1.2.2" have entirely different histories.

View File

@ -5,7 +5,6 @@ import (
"bufio"
"bytes"
"errors"
"fmt"
"io"
"io/ioutil"
"net/http"
@ -91,54 +90,51 @@ func (v *Version) InstallTerraformVersion() error {
if v.Version.IsLessThan(*minV) {
suffix = alternateSuffix
}
url := hashicorpUrl +
resp, err := http.Get(hashicorpUrl +
v.Version.ToString() +
"/" + terraformPrefix +
v.Version.ToString() +
suffix
resp, err := http.Get(url)
suffix)
if err != nil {
return fmt.Errorf("failed to download Terraform: %v", err)
return err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return fmt.Errorf("failed to read response body: %v", err)
return err
}
zipReader, err := zip.NewReader(bytes.NewReader(body), int64(len(body)))
if err != nil {
return fmt.Errorf("failed to create zip reader: %v", err)
return err
}
versionedFileName := homeDir + versionedTerraformFolder + "/" + terraformPrefix + v.Version.ToString()
versionedFile, err := os.OpenFile(versionedFileName, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0755)
versionedFile, err := os.OpenFile(versionedFileName, os.O_WRONLY, 0755)
if os.IsNotExist(err) {
versionedFile, err = os.OpenFile(versionedFileName, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0755)
if err != nil {
return fmt.Errorf("failed to create output file: %v", err)
return err
}
}
if err != nil {
return err
}
defer versionedFile.Close()
for _, zipFile := range zipReader.File {
if zipFile.Name != "terraform" {
continue
}
zr, err := zipFile.Open()
if err != nil {
return fmt.Errorf("failed to open zip file: %v", err)
return err
}
defer zr.Close()
unzippedFileBytes, _ := ioutil.ReadAll(zr)
_, err = io.Copy(versionedFile, zr)
_, err = versionedFile.Write(unzippedFileBytes)
if err != nil {
return fmt.Errorf("failed to write terraform binary: %v", err)
return err
}
break
zr.Close()
}
return nil
}