Skip to content

Commit

Permalink
feat: use the HashiCorp Releases API to fetch latest versions of the …
Browse files Browse the repository at this point in the history
…products

Signed-off-by: Johan Siebens <johan.siebens@gmail.com>
  • Loading branch information
jsiebens committed May 18, 2022
1 parent 3dbeade commit 660391e
Showing 1 changed file with 12 additions and 19 deletions.
31 changes: 12 additions & 19 deletions pkg/config/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,20 @@ import (
"io/ioutil"
"net/http"
"runtime"
"sort"
"time"

"github.com/Masterminds/semver"
)

type Versions struct {
Name string `json:"vault"`
Versions map[string]interface{} `json:"versions"`
type Product struct {
}

type Version struct {
Version string `json:"version"`
}

func GetLatestVersion(product string) (string, error) {
url := fmt.Sprintf("https://releases.hashicorp.com/%s/index.json", product)
url := fmt.Sprintf("https://api.releases.hashicorp.com/v1/releases/%s?license_class=oss", product)

client := http.Client{
Timeout: time.Second * 2,
Expand Down Expand Up @@ -46,28 +47,20 @@ func GetLatestVersion(product string) (string, error) {
if err != nil {
return "", err
}
result := Versions{}

err = json.Unmarshal(body, &result)
if err != nil {
var result []Version
if err := json.Unmarshal(body, &result); err != nil {
return "", err
}

vs := make([]*semver.Version, 0)
for i := range result.Versions {
v, err := semver.NewVersion(i)
for _, i := range result {
v, err := semver.NewVersion(i.Version)
if err == nil && len(v.Metadata()) == 0 && len(v.Prerelease()) == 0 {
vs = append(vs, v)
return i.Version, nil
}
}

if len(vs) == 0 {
return "", fmt.Errorf("unable to find default version of %s", product)
}

sort.Sort(sort.Reverse(semver.Collection(vs)))

return vs[0].String(), nil
return "", fmt.Errorf("unable to find latest version of %s", product)
}

func GetDownloadURL(product, arch string, version *semver.Version) string {
Expand Down

0 comments on commit 660391e

Please sign in to comment.