增加获取配置文件的方法

This commit is contained in:
白茶清欢 2022-06-09 14:58:01 +08:00
parent 2709cd41e7
commit 9377cd288f
4 changed files with 59 additions and 0 deletions

View File

@ -9,6 +9,8 @@ package mysql
import ( import (
"fmt" "fmt"
"path/filepath"
"strings"
"sync" "sync"
"git.zhangdeman.cn/zhangdeman/logger/wrapper" "git.zhangdeman.cn/zhangdeman/logger/wrapper"
@ -58,6 +60,41 @@ func (c *client) BatchAddWithConfigDir(cfgDir string) error {
return nil return nil
} }
// getMysqlCfgFileList 获取mysql配置文件列表
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 14:39 2022/6/9
func (c *client) getMysqlCfgFileList(cfgDir string) ([]cfgFile, error) {
filepathNames, _ := filepath.Glob(filepath.Join(cfgDir, "*"))
cfgFileList := make([]cfgFile, 0)
for i := range filepathNames {
fileArr := strings.Split(filepathNames[i], ".")
if len(fileArr) < 2 {
// 获取不到类型
continue
}
fileType := strings.ToLower(fileArr[len(fileArr)-1])
switch fileType {
case FileTypeYaml:
fallthrough
case FileTypeYml:
cfgFileList = append(cfgFileList, cfgFile{
Path: filepathNames[i],
Type: FileTypeYaml,
})
case FileTypeJson:
cfgFileList = append(cfgFileList, cfgFile{
Path: filepathNames[i],
Type: FileTypeJson,
})
default:
continue
}
}
return cfgFileList, nil
}
// GetDBClient 获取db client // GetDBClient 获取db client
// //
// Author : go_developer@163.com<白茶清欢> // Author : go_developer@163.com<白茶清欢>

View File

@ -51,3 +51,22 @@ type LogConfig struct {
TraceFieldName string TraceFieldName string
Skip int Skip int
} }
// cfgFile 配置文件定义
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 14:47 2022/6/9
type cfgFile struct {
Path string `json:"path"` // 配置文件路径
Type string `json:"type"` // 配置文件类型
}
const (
// FileTypeYml tml
FileTypeYml = "yml"
// FileTypeYaml yaml
FileTypeYaml = "yaml"
// FileTypeJson json
FileTypeJson = "json"
)

1
go.mod
View File

@ -14,6 +14,7 @@ require (
) )
require ( require (
git.zhangdeman.cn/zhangdeman/command v0.0.0-20220522141301-bced9bb50647 // indirect
github.com/gin-contrib/sse v0.1.0 // indirect github.com/gin-contrib/sse v0.1.0 // indirect
github.com/go-playground/locales v0.13.0 // indirect github.com/go-playground/locales v0.13.0 // indirect
github.com/go-playground/universal-translator v0.17.0 // indirect github.com/go-playground/universal-translator v0.17.0 // indirect

2
go.sum
View File

@ -1,3 +1,5 @@
git.zhangdeman.cn/zhangdeman/command v0.0.0-20220522141301-bced9bb50647 h1:TQhXffwAtgJIyu7FuPdmgX6F7OoCGIujW8aN3EFODpw=
git.zhangdeman.cn/zhangdeman/command v0.0.0-20220522141301-bced9bb50647/go.mod h1:oCRPD8pwzLidtrqKQXcHMMJocYs5WDXnh20SCu34cBo=
git.zhangdeman.cn/zhangdeman/logger v0.0.0-20220514052229-cf395d3dc4c3 h1:T41tE9F2Gy8eKVSKtTPhf7RaRT12qHEXTfx4IJuoIS4= git.zhangdeman.cn/zhangdeman/logger v0.0.0-20220514052229-cf395d3dc4c3 h1:T41tE9F2Gy8eKVSKtTPhf7RaRT12qHEXTfx4IJuoIS4=
git.zhangdeman.cn/zhangdeman/logger v0.0.0-20220514052229-cf395d3dc4c3/go.mod h1:0A5BV9pE31nuFE60TLbP7BIhhV/fcWoi+fHrcV2clJw= git.zhangdeman.cn/zhangdeman/logger v0.0.0-20220514052229-cf395d3dc4c3/go.mod h1:0A5BV9pE31nuFE60TLbP7BIhhV/fcWoi+fHrcV2clJw=
git.zhangdeman.cn/zhangdeman/util v0.0.0-20220514082633-1be4d9eab11f h1:1amgaCqOPn7gvcUEEX614cO8lkIz+G8W/YtGXLIdW1w= git.zhangdeman.cn/zhangdeman/util v0.0.0-20220514082633-1be4d9eab11f h1:1amgaCqOPn7gvcUEEX614cO8lkIz+G8W/YtGXLIdW1w=