server.go 1019 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. package migrate
  2. import (
  3. "github.com/spf13/cobra"
  4. "cold-delivery/db"
  5. "gogs.baozhida.cn/zoie/OAuth-core/config/source/file"
  6. log "gogs.baozhida.cn/zoie/OAuth-core/logger"
  7. "gogs.baozhida.cn/zoie/OAuth-core/sdk/config"
  8. )
  9. var (
  10. configYml string
  11. StartCmd = &cobra.Command{
  12. Use: "migrate",
  13. Short: "初始化数据库基础数据",
  14. Example: "cold-delivery migrate -c conf/settings.yml",
  15. PreRunE: func(cmd *cobra.Command, args []string) error {
  16. //1. 读取配置
  17. config.Setup(
  18. file.NewSource(file.WithPath(configYml)),
  19. db.Setup,
  20. )
  21. db.AutoMigrateDB()
  22. return nil
  23. },
  24. RunE: func(cmd *cobra.Command, args []string) error {
  25. if err := InitDb(db.DB); err != nil {
  26. log.Error("数据库基础数据初始化失败", err.Error())
  27. } else {
  28. log.Info("数据库基础数据初始化成功")
  29. }
  30. return nil
  31. },
  32. }
  33. )
  34. func init() {
  35. StartCmd.PersistentFlags().StringVarP(&configYml, "config", "c", "config/settings.yml", "Start server with provided configuration file")
  36. }