mirror of
https://gitee.com/shikong-sk/gofiber-study
synced 2025-02-24 15:52:15 +08:00
72 lines
1.4 KiB
Go
72 lines
1.4 KiB
Go
package utils
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"fmt"
|
|
"gofiber.study.skcks.cn/common/model/xorm/types"
|
|
"gopkg.in/yaml.v3"
|
|
"os"
|
|
"regexp"
|
|
"xorm.io/xorm"
|
|
)
|
|
|
|
func SyncStructs(configPath string, structs interface{}) error {
|
|
c, err := ReadModelConfig(configPath)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
eg, err := xorm.NewEngineGroup(c.Source.Database, []string{c.Source.Conn})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
err = eg.Sync2(structs)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func ReadModelConfig(configPath string) (conf *types.Config, err error) {
|
|
conf = new(types.Config)
|
|
f, err := os.ReadFile(configPath)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
err = yaml.Unmarshal(f, conf)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
j, _ := json.Marshal(conf)
|
|
var out bytes.Buffer
|
|
_ = json.Indent(&out, j, "", " ")
|
|
fmt.Printf("%s\n", out.String())
|
|
|
|
return
|
|
}
|
|
|
|
func ParseSqlConnDBName(conn string) string {
|
|
pattern := regexp.MustCompile(
|
|
`^(?:(?P<user>.*?)(?::(?P<passwd>.*))?@)?` + // [user[:password]@]
|
|
`(?:(?P<net>[^\(]*)(?:\((?P<addr>[^\)]*)\))?)?` + // [net[(addr)]]
|
|
`\/(?P<dbname>.*?)` + // /dbname
|
|
`(?:\?(?P<params>[^\?]*))?$`) // [?param1=value1¶mN=valueN]
|
|
matches := pattern.FindStringSubmatch(conn)
|
|
// tlsConfigRegister := make(map[string]*tls.Config)
|
|
names := pattern.SubexpNames()
|
|
|
|
for i, match := range matches {
|
|
switch names[i] {
|
|
case "dbname":
|
|
return match
|
|
}
|
|
}
|
|
|
|
return ""
|
|
}
|