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.*?)(?::(?P.*))?@)?` + // [user[:password]@] `(?:(?P[^\(]*)(?:\((?P[^\)]*)\))?)?` + // [net[(addr)]] `\/(?P.*?)` + // /dbname `(?:\?(?P[^\?]*))?$`) // [?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 "" }