func()

in astro/tvm/versionrepo.go [105:147]


func (r *VersionRepo) download(version string) (string, error) {
	url := fmt.Sprintf(terraformZipFileDownloadURL, version, version, r.platform, r.arch)

	// Temporary directory for downloading Terraform and extracting the zip file
	tmpDir, err := ioutil.TempDir("", "terraform")
	if err != nil {
		return "", err
	}
	defer os.RemoveAll(tmpDir)

	zipFilePath := path.Join(tmpDir, "terraform.zip")

	// Download Terraform zip file
	if err := downloadFile(url, zipFilePath); err != nil {
		return "", err
	}

	// Extract contents of zip file
	if err := unzip(zipFilePath, tmpDir); err != nil {
		return "", err
	}

	terraformBinaryPath := path.Join(tmpDir, "terraform")

	// Check the binary is there
	if !utils.FileExists(terraformBinaryPath) {
		return "", errors.New("Terraform binary missing from zip file")
	}

	targetDir := r.dir(version)

	// Make repo dir
	if err := os.MkdirAll(targetDir, os.ModePerm); err != nil {
		return "", err
	}

	// Move binary to repo path
	if err := os.Rename(terraformBinaryPath, path.Join(targetDir, "terraform")); err != nil {
		return "", err
	}

	return r.terraformPath(version), nil
}