/
config.go
50 lines (40 loc) · 955 Bytes
/
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
package gomodule
import (
"os"
"regexp"
"go.f110.dev/xerrors"
"gopkg.in/yaml.v3"
"go.f110.dev/mono/go/regexp/regexputil"
)
type ModuleSetting struct {
ModuleName string `yaml:"module_name"`
URLReplace string `yaml:"url_replace"`
match *regexp.Regexp
replaceRegexp *regexputil.RegexpLiteral
}
type Config []*ModuleSetting
func ReadConfig(path string) (Config, error) {
f, err := os.Open(path)
if err != nil {
return nil, xerrors.WithStack(err)
}
var conf Config
if err := yaml.NewDecoder(f).Decode(&conf); err != nil {
return nil, xerrors.WithStack(err)
}
for _, v := range conf {
re, err := regexp.Compile(v.ModuleName)
if err != nil {
return nil, xerrors.WithStack(err)
}
v.match = re
if v.URLReplace != "" {
regexpLiteral, err := regexputil.ParseRegexpLiteral(v.URLReplace)
if err != nil {
return nil, xerrors.WithStack(err)
}
v.replaceRegexp = regexpLiteral
}
}
return conf, nil
}