server.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. package app
  2. import (
  3. "bytes"
  4. "errors"
  5. "fmt"
  6. "github.com/go-admin-team/go-admin-core/sdk/pkg"
  7. "github.com/go-admin-team/go-admin-core/sdk/pkg/utils"
  8. "github.com/spf13/cobra"
  9. "text/template"
  10. )
  11. var (
  12. appName string
  13. StartCmd = &cobra.Command{
  14. Use: "app",
  15. Short: "Create a new app",
  16. Long: "Use when you need to create a new app",
  17. Example: "go-admin app -n admin",
  18. Run: func(cmd *cobra.Command, args []string) {
  19. run()
  20. },
  21. }
  22. )
  23. func init() {
  24. StartCmd.PersistentFlags().StringVarP(&appName, "name", "n", "", "Start server with provided configuration file")
  25. }
  26. func run() {
  27. fmt.Println(`start init`)
  28. //1. 读取配置
  29. fmt.Println(`generate migration file`)
  30. _ = genFile()
  31. }
  32. func genFile() error {
  33. if appName == "" {
  34. return errors.New("arg `name` invalid :name is empty")
  35. }
  36. path := "app/"
  37. appPath := path + appName
  38. err := utils.IsNotExistMkDir(appPath)
  39. if err != nil {
  40. return err
  41. }
  42. apiPath := appPath + "/apis/"
  43. err = utils.IsNotExistMkDir(apiPath)
  44. if err != nil {
  45. return err
  46. }
  47. modelsPath := appPath + "/models/"
  48. err = utils.IsNotExistMkDir(modelsPath)
  49. if err != nil {
  50. return err
  51. }
  52. routerPath := appPath + "/router/"
  53. err = utils.IsNotExistMkDir(routerPath)
  54. if err != nil {
  55. return err
  56. }
  57. servicePath := appPath + "/service/"
  58. err = utils.IsNotExistMkDir(servicePath)
  59. if err != nil {
  60. return err
  61. }
  62. dtoPath := appPath + "/service/dto/"
  63. err = utils.IsNotExistMkDir(dtoPath)
  64. if err != nil {
  65. return err
  66. }
  67. t1, err := template.ParseFiles("template/cmd_api.template")
  68. if err != nil {
  69. return err
  70. }
  71. m := map[string]string{}
  72. m["appName"] = appName
  73. var b1 bytes.Buffer
  74. err = t1.Execute(&b1, m)
  75. pkg.FileCreate(b1, "./cmd/api/"+appName+".go")
  76. t2, err := template.ParseFiles("template/router.template")
  77. var b2 bytes.Buffer
  78. err = t2.Execute(&b2, nil)
  79. pkg.FileCreate(b2, appPath+"/router/router.go")
  80. return nil
  81. }