-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrepoprovider.go
163 lines (142 loc) · 3.24 KB
/
repoprovider.go
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package main
import (
"cmp"
"encoding/json"
"errors"
"fmt"
"net/http"
"regexp"
"runtime"
"slices"
"strings"
)
var (
ErrNoRelease = errors.New("no release found")
ErrNoAsset = errors.New("no asset found")
ErrMultipleMaxWeightAsset = errors.New("multiple max weight assets")
)
type Asset struct {
Name string
URL string
containsOS bool
containsArch bool
matchesOS bool
matchesArch bool
supportedArchiveFormat bool
libc int
}
func NewAsset(name, url string) *Asset {
return newAsset(name, url, runtime.GOOS, runtime.GOARCH, isMusl)
}
func newAsset(name, url, goos, goarch string, isMusl bool) *Asset {
var libc int
if !isMusl {
if !containsMusl(name) {
libc = 1
} else {
libc = 0
}
} else {
if containsMusl(name) {
libc = 1
} else {
libc = 0
}
}
return &Asset{
Name: name,
URL: url,
containsOS: containsOS(name),
containsArch: containsArch(name),
matchesOS: matchesOS(name, goos),
matchesArch: matchesArch(name, goarch),
supportedArchiveFormat: isSupportedArchiveFormat(name),
libc: libc,
}
}
func (a Asset) Weight() int {
var sum int
for _, b := range []bool{
a.containsOS,
a.containsArch,
a.matchesOS,
a.matchesArch,
a.supportedArchiveFormat,
} {
sum += boolToInt(b)
}
sum += a.libc
return sum
}
type Assets []Asset
func (as Assets) FindMaxWeightAsset() (Asset, error) {
if len(as) == 0 {
return Asset{}, ErrNoAsset
}
// desc
slices.SortStableFunc(as, func(a, b Asset) int {
return cmp.Compare(b.Weight(), a.Weight())
})
maxWeight := as[0].Weight()
for i := 1; i < len(as); i++ {
if as[i].Weight() == maxWeight {
var maxAs Assets
for _, v := range as {
if v.Weight() == maxWeight {
maxAs = append(maxAs, v)
} else {
break
}
}
return Asset{}, fmt.Errorf("%w: %s", ErrMultipleMaxWeightAsset, maxAs.JoinNameWithWeight())
} else {
break
}
}
return as[0], nil
}
func (as Assets) JoinName() string {
return as.joinName(false)
}
func (as Assets) JoinNameWithWeight() string {
return as.joinName(true)
}
func (as Assets) joinName(withWeight bool) string {
names := make([]string, len(as))
for i, asset := range as {
if withWeight {
names[i] = fmt.Sprintf("%s: %d", asset.Name, asset.Weight())
} else {
names[i] = asset.Name
}
}
return strings.Join(names, ", ")
}
type Release struct {
Name string
TagName string
Assets []Asset
AuthHeaders map[string]string
AssetPattern *regexp.Regexp
}
type RepoProvider interface {
GetLatestRelease() (Release, error)
GetTaggedRelease(tag string) (Release, error)
}
func GetRelease(url string, headers map[string]string, target interface{}) error {
resp, err := httpGet(url, headers)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
if resp.StatusCode == http.StatusNotFound {
return ErrNoRelease
}
return fmt.Errorf("failed to fetch release, status code: %d, url: %s", resp.StatusCode, url)
}
if err := json.NewDecoder(resp.Body).Decode(target); err != nil {
return err
}
return nil
}