-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapi.go
More file actions
131 lines (111 loc) · 3.13 KB
/
Copy pathapi.go
File metadata and controls
131 lines (111 loc) · 3.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package main
import (
"encoding/json"
"fmt"
"net/http"
"runtime"
"sort"
)
const (
goDevAPI = "https://go.dev/dl/?mode=json"
goDevAPIAll = "https://go.dev/dl/?mode=json&include=all"
goDevDL = "https://go.dev/dl/"
)
// GoRelease represents a Go version release from the official API
type GoRelease struct {
Version string `json:"version"`
Stable bool `json:"stable"`
Files []GoFile `json:"files"`
}
// GoFile represents a downloadable file for a Go release
type GoFile struct {
Filename string `json:"filename"`
OS string `json:"os"`
Arch string `json:"arch"`
SHA256 string `json:"sha256"`
Size int64 `json:"size"`
Kind string `json:"kind"` // "source", "archive", "installer"
}
// FetchReleases fetches Go releases from the official API
func FetchReleases(client *http.Client, includeAll bool) ([]GoRelease, error) {
url := goDevAPI
if includeAll {
url = goDevAPIAll
}
resp, err := client.Get(url)
if err != nil {
return nil, fmt.Errorf("failed to fetch releases: %w", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("API returned status %d", resp.StatusCode)
}
var releases []GoRelease
if err := json.NewDecoder(resp.Body).Decode(&releases); err != nil {
return nil, fmt.Errorf("failed to parse releases: %w", err)
}
return releases, nil
}
// FindMatchingFile finds the appropriate file for the current OS/Arch
func (r *GoRelease) FindMatchingFile() *GoFile {
for i := range r.Files {
f := &r.Files[i]
if f.OS == runtime.GOOS && f.Arch == runtime.GOARCH && f.Kind == "archive" {
return f
}
}
return nil
}
// DownloadURL returns the full download URL for a file
func (f *GoFile) DownloadURL() string {
return goDevDL + f.Filename
}
// ToPackage converts a GoFile to a Package for compatibility
func (f *GoFile) ToPackage(version string) *Package {
return &Package{
Name: f.Filename,
Tag: version,
URL: f.DownloadURL(),
Kind: f.Kind,
OS: f.OS,
Arch: f.Arch,
Checksum: f.SHA256,
Algorithm: "SHA256",
}
}
// GetLatestVersion returns the latest stable Go version
func GetLatestVersion(client *http.Client) (string, error) {
releases, err := FetchReleases(client, false)
if err != nil {
return "", err
}
if len(releases) == 0 {
return "", fmt.Errorf("no releases found")
}
// Sort by version (newest first)
sort.Slice(releases, func(i, j int) bool {
return versionCompare(releases[i].Version) > versionCompare(releases[j].Version)
})
return releases[0].Version, nil
}
// FindRelease finds a specific release by version tag
func FindRelease(releases []GoRelease, tag string) *GoRelease {
normalized := normalizeVersionTag(tag)
for i := range releases {
if releases[i].Version == normalized {
return &releases[i]
}
}
return nil
}
// GetVersionList returns a sorted list of version strings
func GetVersionList(releases []GoRelease) []string {
versions := make([]string, len(releases))
for i, r := range releases {
versions[i] = r.Version
}
sort.Slice(versions, func(i, j int) bool {
return versionCompare(versions[i]) > versionCompare(versions[j])
})
return versions
}