Skip to content

Commit

Permalink
Improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisTerBeke committed Mar 12, 2024
1 parent f7acc02 commit d2e1c39
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
18 changes: 12 additions & 6 deletions pkg/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,14 @@ import (
"fmt"
"net/http"

"github.com/tfversion/tfversion/pkg/download"
"github.com/tfversion/tfversion/pkg/helpers"
)

const (
// TerraformReleasesApiUrl is the URL to list available Terraform releases.
TerraformReleasesApiUrl = "https://api.releases.hashicorp.com/v1/releases/terraform"
)

type Build struct {
Arch string `json:"arch"`
Os string `json:"os"`
Expand All @@ -35,7 +39,7 @@ type Release struct {
// Best is to repeat this to get everything, then filter client side on pre-release and limit to maxResults
// since the API does not support filtering on pre-release and other properties
func ListVersions(maxResults int) []string {
url := fmt.Sprintf("%s?limit=%v", download.TerraformReleasesApiUrl, maxResults)
url := fmt.Sprintf("%s?limit=%v", TerraformReleasesApiUrl, maxResults)
resp, err := http.Get(url)
if err != nil {
helpers.ExitWithError("getting Terraform releases from API", err)
Expand All @@ -55,9 +59,10 @@ func ListVersions(maxResults int) []string {
return availableVersions
}

// TODO: use this method to validate a specific version before downloading/installing
func GetVersion(version string) Release {
resp, err := http.Get(download.TerraformReleasesApiUrl + "/" + version)
// TODO: use this method to validate a specific version before downloading and return builds[0].url for downloading

Check failure on line 62 in pkg/api/api.go

View workflow job for this annotation

GitHub Actions / build

pkg/api/api.go:62: Line contains TODO/BUG/FIXME: "TODO: use this method to validate a spec..." (godox)
func GetVersion(version string) string {
url := fmt.Sprintf("%s/%s", TerraformReleasesApiUrl, version)
resp, err := http.Get(url)
if err != nil {
helpers.ExitWithError("getting Terraform release from API", err)
}
Expand All @@ -68,5 +73,6 @@ func GetVersion(version string) Release {
helpers.ExitWithError("parsing Terraform release", err)
}

return release
// TODO: check if URL exists

Check failure on line 76 in pkg/api/api.go

View workflow job for this annotation

GitHub Actions / build

pkg/api/api.go:76: Line contains TODO/BUG/FIXME: "TODO: check if URL exists" (godox)
return release.Builds[0].Url
}
2 changes: 0 additions & 2 deletions pkg/download/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ package download
const (
// TerraformReleasesUrl is the URL used to download Terraform releases from.
TerraformReleasesUrl = "https://releases.hashicorp.com/terraform"
// TerraformReleasesApiUrl is the URL to list available Terraform releases.
TerraformReleasesApiUrl = "https://api.releases.hashicorp.com/v1/releases/terraform"
// MaxRetries is the maximum number of retries for a download.
MaxRetries = 3
// RetryTimeInSeconds is the time to wait before retrying a download.
Expand Down

0 comments on commit d2e1c39

Please sign in to comment.