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
|
package main
import (
_ "embed"
"fmt"
"net/url"
"os"
"path"
"strings"
"sigs.k8s.io/yaml"
)
type Upstream struct {
URLs []string `json:"urls"`
Name string `json:"name"`
Desc string `json:"desc"`
}
func ReadUpstreams(filename string) ([]Upstream, error) {
bs, err := os.ReadFile(filename)
if err != nil {
return nil, fmt.Errorf("upstreams: %q: %w", filename, err)
}
var ret []Upstream
if err := yaml.UnmarshalStrict(bs, &ret); err != nil {
return nil, fmt.Errorf("upstreams: %q: %w", filename, err)
}
for i := range ret {
upstream := ret[i]
if err := upstream.Fill(); err != nil {
return nil, fmt.Errorf("upstreams: %q: %w", filename, err)
}
ret[i] = upstream
}
return ret, nil
}
func (upstream *Upstream) Fill() error {
if upstream.Name == "" {
u, err := url.Parse(upstream.URLs[0])
if err != nil {
return err
}
_, upstream.Name = path.Split(strings.TrimSuffix(path.Clean(u.Path), ".git"))
}
return nil
}
|