mongo.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. package mongo
  2. import (
  3. "errors"
  4. "path/filepath"
  5. "strings"
  6. "github.com/spf13/cobra"
  7. "github.com/wuntsong-org/go-zero-plus/tools/goctlwt/config"
  8. "github.com/wuntsong-org/go-zero-plus/tools/goctlwt/model/mongo/generate"
  9. file "github.com/wuntsong-org/go-zero-plus/tools/goctlwt/util"
  10. "github.com/wuntsong-org/go-zero-plus/tools/goctlwt/util/pathx"
  11. )
  12. var (
  13. // VarStringSliceType describes a golang data structure name for mongo.
  14. VarStringSliceType []string
  15. // VarStringDir describes an output directory.
  16. VarStringDir string
  17. // VarBoolCache describes whether cache is enabled.
  18. VarBoolCache bool
  19. // VarBoolEasy describes whether to generate Collection Name in the code for easy declare.
  20. VarBoolEasy bool
  21. // VarStringStyle describes the style.
  22. VarStringStyle string
  23. // VarStringHome describes the goctl home.
  24. VarStringHome string
  25. // VarStringRemote describes the remote git repository.
  26. VarStringRemote string
  27. // VarStringBranch describes the git branch.
  28. VarStringBranch string
  29. )
  30. // Action provides the entry for goctl mongo code generation.
  31. func Action(_ *cobra.Command, _ []string) error {
  32. tp := VarStringSliceType
  33. c := VarBoolCache
  34. easy := VarBoolEasy
  35. o := strings.TrimSpace(VarStringDir)
  36. s := VarStringStyle
  37. home := VarStringHome
  38. remote := VarStringRemote
  39. branch := VarStringBranch
  40. if len(remote) > 0 {
  41. repo, _ := file.CloneIntoGitHome(remote, branch)
  42. if len(repo) > 0 {
  43. home = repo
  44. }
  45. }
  46. if len(home) > 0 {
  47. pathx.RegisterGoctlHome(home)
  48. }
  49. if len(tp) == 0 {
  50. return errors.New("missing type")
  51. }
  52. cfg, err := config.NewConfig(s)
  53. if err != nil {
  54. return err
  55. }
  56. a, err := filepath.Abs(o)
  57. if err != nil {
  58. return err
  59. }
  60. if err = pathx.MkdirIfNotExist(a); err != nil {
  61. return err
  62. }
  63. return generate.Do(&generate.Context{
  64. Types: tp,
  65. Cache: c,
  66. Easy: easy,
  67. Output: a,
  68. Cfg: cfg,
  69. })
  70. }