50 lines
1.0 KiB
Go
50 lines
1.0 KiB
Go
package dl
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"net/http"
|
|
)
|
|
|
|
var dlURL = "https://golang.org/dl/?mode=json"
|
|
|
|
// Version is a Go version
|
|
type Version struct {
|
|
Version string `json:"version"`
|
|
Stable bool `json:"stable"`
|
|
Files []File `json:"files"`
|
|
}
|
|
|
|
// File is a dl file
|
|
type File struct {
|
|
Filename string `json:"filename"`
|
|
OS string `json:"os"`
|
|
Arch string `json:"arch"`
|
|
Version string `json:"version"`
|
|
Sha256 string `json:"sha256"`
|
|
Size int `json:"size"`
|
|
Kind string `json:"kind"`
|
|
}
|
|
|
|
// Versions gets the latest Version(s) from dl
|
|
func Versions(ctx context.Context) ([]Version, error) {
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodGet, dlURL, nil)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
req.Header.Set("User-Agent", "git.jojodev.com/golang/dl")
|
|
|
|
res, err := http.DefaultClient.Do(req)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer res.Body.Close()
|
|
|
|
var versions []Version
|
|
if err := json.NewDecoder(res.Body).Decode(&versions); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return versions, nil
|
|
}
|