-
Notifications
You must be signed in to change notification settings - Fork 469
/
loader.go
executable file
·209 lines (183 loc) · 5.19 KB
/
loader.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
package schema
import (
"errors"
"fmt"
"github.com/ServiceComb/go-chassis/pkg/util/fileutil"
"io/ioutil"
"os"
"path/filepath"
"regexp"
"strings"
)
// MicroserviceMeta is the struct for micro service meta
type MicroserviceMeta struct {
MicroserviceName string
SchemaIDs []string
}
// NewMicroserviceMeta gives the object of MicroserviceMeta
func NewMicroserviceMeta(microserviceName string) *MicroserviceMeta {
return &MicroserviceMeta{
MicroserviceName: microserviceName,
SchemaIDs: make([]string, 0),
}
}
// defaultMicroserviceMetaMgr default micro-service meta-manager
var defaultMicroserviceMetaMgr map[string]*MicroserviceMeta
// DefaultSchemaIDsMap default schema schema IDs map
var DefaultSchemaIDsMap map[string]string
// defaultMicroServiceNames default micro-service names
var defaultMicroServiceNames = make([]string, 0)
// LoadSchema to load the schema files and micro-service information under the conf directory
func LoadSchema(path string, schemaEnv bool) error {
/*
conf/
├── chassis.yaml
├── microservice1
│ └── schema
│ ├── schema1.yaml
└── microservice2
└── schema
├── schema2.yaml
*/
schemaNames, err := getSchemaNames(path)
if err != nil {
return err
}
for _, msName := range schemaNames {
var (
microsvcMeta *MicroserviceMeta
schemaError error
)
if schemaEnv {
microsvcMeta, schemaError = loadMicroserviceMeta(path + "/" + msName + "/schema")
} else {
microsvcMeta, schemaError = loadMicroserviceMeta(fileutil.SchemaDir(msName))
}
if schemaError != nil {
return schemaError
}
defaultMicroserviceMetaMgr[msName] = microsvcMeta
}
return nil
}
// getSchemaNames 目录名为服务名
func getSchemaNames(confDir string) ([]string, error) {
schemaNames := make([]string, 0)
// 遍历confDir下的microservice文件夹
err := filepath.Walk(confDir,
func(path string, info os.FileInfo, err error) error {
if info == nil {
return err
}
// 仅读取负一级目录
if !info.IsDir() || filepath.Dir(path) != confDir {
return nil
}
schemaNames = append(schemaNames, info.Name())
return nil
})
return schemaNames, err
}
// SetMicroServiceNames set micro service names
func SetMicroServiceNames(confDir string) error {
fileFormatName := `microservice(\.yaml|\.yml)$`
err := filepath.Walk(confDir,
func(path string, info os.FileInfo, err error) error {
if info == nil {
return err
}
// 仅读取负一级目录
if !info.IsDir() || filepath.Dir(path) != confDir {
return nil
}
filesExist, err := getFiles(filepath.Join(confDir, info.Name()))
if err != nil {
return err
}
for _, name := range filesExist {
ret, _ := regexp.MatchString(fileFormatName, name)
if ret {
defaultMicroServiceNames = append(defaultMicroServiceNames, info.Name())
}
}
return nil
})
return err
}
// loadMicroserviceMeta load micro-service meta
func loadMicroserviceMeta(schemaPath string) (*MicroserviceMeta, error) {
microserviceMeta := NewMicroserviceMeta(filepath.Base(schemaPath))
schemaFiles, err := getFiles(schemaPath)
if err != nil {
return microserviceMeta, err
}
for _, fullPath := range schemaFiles {
schemaFile := filepath.Base(fullPath)
dat, err := ioutil.ReadFile(fullPath)
if err != nil {
e := fmt.Sprintf("The system cannot find the schema file")
return nil, errors.New(e)
}
schemaID := strings.TrimSuffix(schemaFile, filepath.Ext(schemaFile))
microserviceMeta.SchemaIDs = append(microserviceMeta.SchemaIDs, schemaID)
DefaultSchemaIDsMap[schemaID] = string(dat)
}
return microserviceMeta, nil
}
// getFiles get files
func getFiles(fPath string) ([]string, error) {
files := make([]string, 0)
_, err := os.Stat(fPath)
if os.IsNotExist(err) {
return files, nil
}
// schema文件名规则
pat := `^.+(\.yaml|\.yml)$`
// 遍历schemaPath下的schema文件
err = filepath.Walk(fPath,
func(path string, info os.FileInfo, err error) error {
if info == nil {
return err
}
// 仅读取负一级文件
if info.IsDir() || filepath.Dir(path) != fPath {
return nil
}
ret, _ := regexp.MatchString(pat, info.Name())
if !ret {
return nil
}
files = append(files, path)
return nil
})
return files, err
}
// GetMicroserviceNamesBySchemas get micro-service names by schemas
func GetMicroserviceNamesBySchemas() []string {
names := make([]string, 0)
for k := range defaultMicroserviceMetaMgr {
names = append(names, k)
}
return names
}
// GetMicroserviceNames get micro-service names
func GetMicroserviceNames() []string {
return defaultMicroServiceNames
}
// GetSchemaIDs get schema IDs
func GetSchemaIDs(microserviceName string) ([]string, error) {
microsvcMeta, ok := defaultMicroserviceMetaMgr[microserviceName]
if !ok {
return nil, fmt.Errorf("microservice %s not found", microserviceName)
}
schemaIDs := make([]string, 0)
for _, v := range microsvcMeta.SchemaIDs {
schemaIDs = append(schemaIDs, v)
}
return schemaIDs, nil
}
// init is for to initialize the defaultMicroserviceMetaMgr, and DefaultSchemaIDsMap
func init() {
defaultMicroserviceMetaMgr = make(map[string]*MicroserviceMeta)
DefaultSchemaIDsMap = make(map[string]string)
}