-
Notifications
You must be signed in to change notification settings - Fork 15
/
config.go
101 lines (85 loc) · 1.82 KB
/
config.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
package main
import (
"encoding/json"
"io"
"io/ioutil"
"path"
"sort"
)
type config struct {
Domain string `json:"domain"`
DocsDomain string `json:"docsDomain"`
Index bool `json:"index"`
Repositories []repository `json:"repositories"`
}
type repository struct {
Prefix string `json:"prefix"`
Subs []sub `json:"subs"`
Type string `json:"type"`
URL string `json:"url"`
Main bool `json:"main"`
Hidden bool `json:"hidden"`
SourceURLs sourceURLs `json:"source"`
Website website `json:"website"`
}
func (r repository) PrefixPath() string {
if r.Prefix == "" {
return ""
} else {
return "/" + r.Prefix
}
}
func (r repository) Packages() []string {
pkgs := []string{r.Prefix}
for i := range r.Subs {
pkgs = append(pkgs, r.SubPath(i))
}
return pkgs
}
func (r repository) SubPath(i int) string {
return path.Join(r.Prefix, r.Subs[i].Name)
}
type sub struct {
Name string
Hidden bool
}
func (s *sub) UnmarshalJSON(raw []byte) error {
*s = sub{}
err := json.Unmarshal(raw, &s.Name)
if err == nil {
return nil
}
subWithTags := struct {
Name string `json:"name"`
Hidden bool `json:"hidden"`
}{}
err = json.Unmarshal(raw, &subWithTags)
if err != nil {
return err
}
*s = sub(subWithTags)
return nil
}
type sourceURLs struct {
Home string `json:"home"`
Dir string `json:"dir"`
File string `json:"file"`
}
type website struct {
URL string `json:"url"`
}
func parseConfig(r io.Reader) (config, error) {
bytes, err := ioutil.ReadAll(r)
if err != nil {
return config{}, err
}
var c config
err = json.Unmarshal(bytes, &c)
if err != nil {
return config{}, err
}
sort.Slice(c.Repositories, func(i, j int) bool {
return c.Repositories[i].Prefix < c.Repositories[j].Prefix
})
return c, nil
}