feat: 移除冗余的数据库配置处理,简化模型生成命令逻辑
This commit is contained in:
108
cmd/gen_model.go
108
cmd/gen_model.go
@@ -1,16 +1,14 @@
|
|||||||
package cmd
|
package cmd
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"github.com/pkg/errors"
|
||||||
|
log "github.com/sirupsen/logrus"
|
||||||
"github.com/pkg/errors"
|
"github.com/spf13/cobra"
|
||||||
log "github.com/sirupsen/logrus"
|
apg "go.ipao.vip/atomctl/v2/pkg/postgres"
|
||||||
"github.com/spf13/cobra"
|
"go.ipao.vip/atomctl/v2/pkg/utils/gomod"
|
||||||
"github.com/spf13/viper"
|
"go.ipao.vip/gen"
|
||||||
"go.ipao.vip/atomctl/v2/pkg/utils/gomod"
|
"gorm.io/driver/postgres"
|
||||||
"go.ipao.vip/gen"
|
"gorm.io/gorm"
|
||||||
"gorm.io/driver/postgres"
|
|
||||||
"gorm.io/gorm"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func CommandGenModel(root *cobra.Command) {
|
func CommandGenModel(root *cobra.Command) {
|
||||||
@@ -38,83 +36,31 @@ func CommandGenModel(root *cobra.Command) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func commandGenModelE(cmd *cobra.Command, args []string) error {
|
func commandGenModelE(cmd *cobra.Command, args []string) error {
|
||||||
if err := gomod.Parse("go.mod"); err != nil {
|
if err := gomod.Parse("go.mod"); err != nil {
|
||||||
return errors.Wrap(err, "parse go.mod")
|
return errors.Wrap(err, "parse go.mod")
|
||||||
}
|
}
|
||||||
|
|
||||||
cfgFile := cmd.Flag("config").Value.String()
|
cfgFile := cmd.Flag("config").Value.String()
|
||||||
if cfgFile == "" {
|
if cfgFile == "" {
|
||||||
cfgFile = "config.toml"
|
cfgFile = "config.toml"
|
||||||
}
|
}
|
||||||
|
|
||||||
v := viper.New()
|
sqlDB, conf, err := apg.GetDB(cfgFile)
|
||||||
v.SetConfigType("toml")
|
if err != nil {
|
||||||
v.SetConfigFile(cfgFile)
|
return errors.Wrap(err, "load database config")
|
||||||
v.AddConfigPath(".")
|
}
|
||||||
if err := v.ReadInConfig(); err != nil {
|
defer sqlDB.Close()
|
||||||
return errors.Wrap(err, "read config")
|
|
||||||
}
|
|
||||||
|
|
||||||
var dbc modelDBConfig
|
dsn := conf.DSN()
|
||||||
if err := v.UnmarshalKey("Database", &dbc); err != nil {
|
log.Infof("parsed DSN: %s (schema=%s)", dsn, conf.Schema)
|
||||||
return errors.Wrap(err, "unmarshal Database config")
|
|
||||||
}
|
|
||||||
dsn := dbc.DSN()
|
|
||||||
|
|
||||||
log.Infof("parsed DSN: %s (schema=%s)", dsn, dbc.Schema)
|
db, err := gorm.Open(postgres.New(postgres.Config{DSN: dsn}))
|
||||||
|
if err != nil {
|
||||||
db, err := gorm.Open(postgres.New(postgres.Config{DSN: dsn}))
|
return errors.Wrapf(err, "open database with dsn: %s", dsn)
|
||||||
if err != nil {
|
}
|
||||||
return errors.Wrapf(err, "open database with dsn: %s", dsn)
|
|
||||||
}
|
|
||||||
|
|
||||||
// 默认同包同目录生成到 ./database
|
// 默认同包同目录生成到 ./database
|
||||||
gen.GenerateWithDefault(db, "./database/.transform.yaml")
|
gen.GenerateWithDefault(db, "./database/.transform.yaml")
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
|
||||||
|
|
||||||
// local config and helpers
|
|
||||||
|
|
||||||
type modelDBConfig struct {
|
|
||||||
Username string
|
|
||||||
Password string
|
|
||||||
Database string
|
|
||||||
Schema string
|
|
||||||
Host string
|
|
||||||
Port uint
|
|
||||||
SslMode string
|
|
||||||
TimeZone string
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *modelDBConfig) applyDefaults() {
|
|
||||||
if c.Username == "" {
|
|
||||||
c.Username = "postgres"
|
|
||||||
}
|
|
||||||
if c.SslMode == "" {
|
|
||||||
c.SslMode = "disable"
|
|
||||||
}
|
|
||||||
if c.TimeZone == "" {
|
|
||||||
c.TimeZone = "Asia/Shanghai"
|
|
||||||
}
|
|
||||||
if c.Port == 0 {
|
|
||||||
c.Port = 5432
|
|
||||||
}
|
|
||||||
if c.Schema == "" {
|
|
||||||
c.Schema = "public"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *modelDBConfig) DSN() string {
|
|
||||||
c.applyDefaults()
|
|
||||||
return fmt.Sprintf(
|
|
||||||
"host=%s user=%s password=%s dbname=%s port=%d sslmode=%s TimeZone=%s",
|
|
||||||
c.Host,
|
|
||||||
c.Username,
|
|
||||||
c.Password,
|
|
||||||
c.Database,
|
|
||||||
c.Port,
|
|
||||||
c.SslMode,
|
|
||||||
c.TimeZone,
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user