-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgithub_utils.go
More file actions
79 lines (66 loc) · 2.09 KB
/
github_utils.go
File metadata and controls
79 lines (66 loc) · 2.09 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
package utils
import (
"encoding/json"
"fmt"
"io"
"net/http"
"runtime"
"strings"
)
type GitHubRelease struct {
TagName string `json:"tag_name"`
Assets []struct {
Name string `json:"name"`
BrowserDownloadURL string `json:"browser_download_url"`
} `json:"assets"`
}
func GetLatestRelease(repoURL string) (*GitHubRelease, error) {
// GitHub API URL for the latest release
apiURL := repoURL + "/releases/latest"
// Make the HTTP request
resp, err := http.Get(apiURL)
if err != nil {
return nil, fmt.Errorf("error making request: %v", err)
}
defer resp.Body.Close()
// Read the response body
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("error reading response body: %v", err)
}
// Unmarshal the JSON data into the GitHubRelease struct
var release GitHubRelease
if err := json.Unmarshal(body, &release); err != nil {
return nil, fmt.Errorf("error unmarshaling JSON: %v", err)
}
// Return the entire release struct
return &release, nil
}
func GetDownloadURLForCurrentOS(release *GitHubRelease) (string, error) {
if release == nil || len(release.Assets) == 0 {
return "", fmt.Errorf("no assets found in the release")
}
// Determine the current operating system and architecture
osName := runtime.GOOS
arch := runtime.GOARCH
// Define alternate names for architectures
archMappings := map[string][]string{
"amd64": {"amd64", "x86_64", "x64"},
"386": {"386", "x86"},
"arm64": {"arm64", "aarch64"},
"arm": {"arm"},
}
// Get the possible alternate names for the architecture
archAlternates, ok := archMappings[arch]
if !ok {
return "", fmt.Errorf("no alternate names found for architecture: %s", arch)
}
// Loop through the assets to find a match for the current OS and one of the architecture alternates
for _, asset := range release.Assets {
assetNameLower := strings.ToLower(asset.Name)
if strings.Contains(assetNameLower, osName) && ContainsAny(assetNameLower, archAlternates) {
return asset.BrowserDownloadURL, nil
}
}
return "", fmt.Errorf("no zip asset found for the OS: %s and architecture: %s", osName, arch)
}