-
Notifications
You must be signed in to change notification settings - Fork 0
/
conf.go
82 lines (67 loc) · 1.45 KB
/
conf.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
package fasthttpunit
import (
"errors"
"fmt"
"io/ioutil"
"github.com/jhq0113/fasthttpunit/internal"
jsoniter "github.com/json-iterator/go"
"gopkg.in/yaml.v2"
)
type Conf struct {
ApiList []*Api `json:"apiList" yaml:"apiList"`
Delay uint64 `json:"delay" yaml:"delay"`
}
func newConf() *Conf {
return &Conf{
Delay: 1,
}
}
func LoadConf(fileName string) (c *Conf, err error) {
data, err := ioutil.ReadFile(fileName)
if err != nil {
return nil, err
}
conf := newConf()
if internal.IsYaml(fileName) {
err = yaml.Unmarshal(data, conf)
if err != nil {
return nil, err
}
} else if internal.IsJson(fileName) {
err = jsoniter.Unmarshal(data, conf)
if err != nil {
return nil, err
}
} else {
return nil, ErrUnsupportedFileType
}
if len(conf.ApiList) < 1 {
return nil, ErrNotFoundApi
}
for index, _ := range conf.ApiList {
conf.ApiList[index].load()
}
return conf, nil
}
func LoadConfByPath(basePath string) (c *Conf, err error) {
fileList, err := ioutil.ReadDir(basePath)
if err != nil {
return nil, err
}
conf := newConf()
conf.ApiList = make([]*Api, 0, len(fileList))
for _, f := range fileList {
if f.IsDir() {
continue
}
api, err := loadApi(basePath + "/" + f.Name())
if err != nil {
return nil, errors.New(fmt.Sprintf("file %s format error:%s", f.Name(), err.Error()))
}
conf.ApiList = append(conf.ApiList, api)
}
if len(conf.ApiList) < 1 {
return nil, ErrNotFoundApi
}
return conf, nil
}