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 }