biz.go 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. package cmds
  2. import (
  3. "github.com/spf13/cobra"
  4. "go_server/base/core"
  5. "go_server/service/system"
  6. )
  7. const (
  8. generateBiz = "generate"
  9. rollbackBiz = "rollback"
  10. upgradeModel = "model"
  11. )
  12. // 需要配置正确业务库后执行该命令
  13. // 可增加子命令
  14. var bizCmd = &cobra.Command{
  15. Use: "biz",
  16. Short: "根据biz数据库配置注入服务路由模型 可用命令 biz [generate|rollback|model] alias",
  17. Run: func(cmd *cobra.Command, args []string) {
  18. if err := cmd.Help(); err != nil {
  19. panic(err)
  20. }
  21. core.Log.Infof("args:%+v", args)
  22. if len(args) != 3 {
  23. core.Log.Infof("args:%+v 必须输入库名+表名 biz [generate|rollback|upgrade] dbAlias tableName[需同步所有请输入all]", args)
  24. return
  25. }
  26. server := &system.AutoService{}
  27. switch args[0] {
  28. case generateBiz:
  29. // 自动生成所有服务 -- 危险操作 demo: go run main.go biz generate app all
  30. if args[2] == "all" {
  31. if err := server.AutoServerCodeWithAlias(args[1], ""); err != nil {
  32. panic(err)
  33. }
  34. } else {
  35. // 指定表名 生成服务demo: go run main.go biz generate app user
  36. if err := server.AutoServerCodeWithAlias(args[1], args[2]); err != nil {
  37. panic(err)
  38. }
  39. }
  40. break
  41. case rollbackBiz:
  42. // 回滚表所有管理接口、路由、服务 demo: go run main.go biz rollback app all
  43. if args[2] == "all" {
  44. if err := server.RollbackWithAlias(args[1], ""); err != nil {
  45. panic(err)
  46. }
  47. } else {
  48. // 回滚表所有管理接口、路由、服务 -- 危险操作
  49. // demo: go run main.go biz rollback app user
  50. if err := server.RollbackWithAlias(args[1], args[2]); err != nil {
  51. panic(err)
  52. }
  53. }
  54. break
  55. case upgradeModel:
  56. // demo: go run main.go biz model app all // 同步所有表结构到代码
  57. // demo: go run main.go biz model app sys_config // 同步指定表结构
  58. if args[2] == "all" {
  59. if err := server.ModelAutoForCmd(args[1], ""); err != nil {
  60. panic(err)
  61. }
  62. } else {
  63. if err := server.ModelAutoForCmd(args[1], args[2]); err != nil {
  64. panic(err)
  65. }
  66. }
  67. break
  68. default:
  69. core.Log.Infof("未知命令:%s", args[0])
  70. break
  71. }
  72. // todo:根据传参数 执行不同命令
  73. },
  74. }