forked from YaoApp/yao
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
275 lines (248 loc) · 6.85 KB
/
utils.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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
package share
import (
"fmt"
"io/ioutil"
"os"
"path"
"path/filepath"
"strings"
"github.com/yaoapp/kun/exception"
"github.com/yaoapp/kun/log"
"github.com/yaoapp/yao/data"
)
// Walk 遍历应用目录,读取文件列表
func Walk(root string, typeName string, cb func(root, filename string)) error {
root = strings.TrimPrefix(root, "fs://")
root = strings.TrimPrefix(root, "file://")
root = path.Join(root, "/")
err := filepath.Walk(root, func(filename string, info os.FileInfo, err error) error {
if err != nil {
log.With(log.F{"root": root, "type": typeName, "filename": filename}).Error(err.Error())
return err
}
if strings.HasSuffix(filename, typeName) {
cb(root, filename)
}
return nil
})
return err
}
// ID parse unique name root: "/tests/apis" file: "/tests/apis/foo/bar.http.json"
func ID(root string, file string) string {
return SpecName(root, file)
}
// File ID to file
func File(id string, ext string) string {
ext = strings.TrimLeft(ext, ".")
file := strings.ReplaceAll(id, ".", string(os.PathSeparator))
return fmt.Sprintf("%s.%s", file, ext)
}
// SpecName 解析名称 root: "/tests/apis" file: "/tests/apis/foo/bar.http.json"
func SpecName(root string, file string) string {
filename := strings.TrimPrefix(file, root+"/") // "foo/bar.http.json"
namer := strings.Split(filename, ".") // ["foo/bar", "http", "json"]
nametypes := strings.Split(namer[0], "/") // ["foo", "bar"]
name := strings.Join(nametypes, ".") // "foo.bar"
return name
}
// ScriptName 解析数据处理脚本名称
func ScriptName(filename string) string {
filename = strings.TrimSuffix(filename, ".js")
namer := strings.Split(filename, ".") // ["foo/bar", "http", "json"]
if len(namer) < 2 {
return namer[0]
}
return namer[len(namer)-1]
}
// ReadFile 读取文件
func ReadFile(filename string) []byte {
file, err := os.Open(filename)
if err != nil {
exception.Err(err, 500).Throw()
}
defer file.Close()
content, err := ioutil.ReadAll(file)
if err != nil {
exception.Err(err, 500).Throw()
}
return content
}
// DirNotExists 校验目录是否存在
func DirNotExists(dir string) bool {
dir = strings.TrimPrefix(dir, "fs://")
dir = strings.TrimPrefix(dir, "file://")
if _, err := os.Stat(dir); os.IsNotExist(err) {
return true
}
return false
}
// DirAbs 文件绝对路径
func DirAbs(dir string) string {
dir = strings.TrimPrefix(dir, "fs://")
dir = strings.TrimPrefix(dir, "file://")
dirAbs, err := filepath.Abs(dir)
if err != nil {
log.Panic("获取绝对路径错误 %s %s", dir, err)
}
return dirAbs
}
// ************************************************
// 警告: 以下函数将被弃用
// ************************************************
// GetAppPlugins 遍历应用目录,读取文件列表
func GetAppPlugins(root string, typ string) []Script {
files := []Script{}
root = path.Join(root, "/")
filepath.Walk(root, func(file string, info os.FileInfo, err error) error {
if err != nil {
exception.Err(err, 500).Throw()
return err
}
if strings.HasSuffix(file, typ) {
files = append(files, GetAppPluginFile(root, file))
}
return nil
})
return files
}
// GetAppPluginFile 读取文件
func GetAppPluginFile(root string, file string) Script {
name := GetAppPluginFileName(root, file)
return Script{
Name: name,
Type: "plugin",
File: file,
}
}
// GetAppPluginFileName 读取文件
func GetAppPluginFileName(root string, file string) string {
filename := strings.TrimPrefix(file, root+"/")
namer := strings.Split(filename, ".")
nametypes := strings.Split(namer[0], "/")
name := strings.Join(nametypes, ".")
return name
}
// GetAppFilesFS 遍历应用目录,读取文件列表
func GetAppFilesFS(root string, typ string) []Script {
files := []Script{}
root = path.Join(root, "/")
filepath.Walk(root, func(filepath string, info os.FileInfo, err error) error {
if err != nil {
exception.Err(err, 500).Throw()
return err
}
if strings.HasSuffix(filepath, typ) {
files = append(files, GetAppFile(root, filepath))
}
return nil
})
return files
}
// GetAppFile 读取文件
func GetAppFile(root string, filepath string) Script {
name := GetAppFileName(root, filepath)
file, err := os.Open(filepath)
if err != nil {
exception.Err(err, 500).Throw()
}
defer file.Close()
content, err := ioutil.ReadAll(file)
if err != nil {
exception.Err(err, 500).Throw()
}
return Script{
Name: name,
Type: "app",
Content: content,
}
}
// GetAppFileName 读取文件
func GetAppFileName(root string, file string) string {
filename := strings.TrimPrefix(file, root+"/")
namer := strings.Split(filename, ".")
nametypes := strings.Split(namer[0], "/")
name := strings.Join(nametypes, ".")
return name
}
// GetAppFileBaseName 读取文件base
func GetAppFileBaseName(root string, file string) string {
filename := strings.TrimPrefix(file, root+"/")
namer := strings.Split(filename, ".")
return filepath.Join(root, namer[0])
}
// GetFilesFS 遍历目录,读取文件列表
func GetFilesFS(root string, typ string) []Script {
files := []Script{}
root = path.Join(root, "/")
filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
if err != nil {
exception.Err(err, 500).Throw()
return err
}
if strings.HasSuffix(path, typ) {
files = append(files, GetFile(root, path))
}
return nil
})
return files
}
// GetFile 读取文件
func GetFile(root string, path string) Script {
filename := strings.TrimPrefix(path, root+"/")
name, typ := GetTypeName(filename)
file, err := os.Open(path)
if err != nil {
exception.Err(err, 500).Throw()
}
defer file.Close()
content, err := ioutil.ReadAll(file)
if err != nil {
exception.Err(err, 500).Throw()
}
return Script{
Name: name,
Type: typ,
Content: content,
}
}
// GetFileName 读取文件
func GetFileName(root string, file string) string {
filename := strings.TrimPrefix(file, root+"/")
name, _ := GetTypeName(filename)
return name
}
// GetFileBaseName 读取文件base
func GetFileBaseName(root string, file string) string {
filename := strings.TrimPrefix(file, root+"/")
namer := strings.Split(filename, ".")
return filepath.Join(root, namer[0])
}
// GetFilesBin 从 bindata 中读取文件列表
func GetFilesBin(root string, typ string) []Script {
files := []Script{}
binfiles := data.AssetNames()
for _, path := range binfiles {
if strings.HasSuffix(path, typ) {
file := strings.TrimPrefix(path, root+"/")
name, typ := GetTypeName(file)
content, err := data.Asset(path)
if err != nil {
exception.Err(err, 500).Throw()
}
files = append(files, Script{
Name: name,
Type: typ,
Content: content,
})
}
}
return files
}
// GetTypeName 读取类型
func GetTypeName(path string) (name string, typ string) {
namer := strings.Split(path, ".")
nametypes := strings.Split(namer[0], "/")
name = strings.Join(nametypes[1:], ".")
typ = nametypes[0]
return name, typ
}