mongo.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. package mongo
  2. import (
  3. "errors"
  4. "path/filepath"
  5. "strings"
  6. "github.com/spf13/cobra"
  7. "github.com/zeromicro/go-zero/tools/goctl/config"
  8. "github.com/zeromicro/go-zero/tools/goctl/model/mongo/generate"
  9. file "github.com/zeromicro/go-zero/tools/goctl/util"
  10. "github.com/zeromicro/go-zero/tools/goctl/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. // VarStringStyle describes the style.
  20. VarStringStyle string
  21. // VarStringHome describes the goctl home.
  22. VarStringHome string
  23. // VarStringRemote describes the remote git repository.
  24. VarStringRemote string
  25. // VarStringBranch describes the git branch.
  26. VarStringBranch string
  27. )
  28. // Action provides the entry for goctl mongo code generation.
  29. func Action(_ *cobra.Command, _ []string) error {
  30. tp := VarStringSliceType
  31. c := VarBoolCache
  32. o := strings.TrimSpace(VarStringDir)
  33. s := VarStringStyle
  34. home := VarStringHome
  35. remote := VarStringRemote
  36. branch := VarStringBranch
  37. if len(remote) > 0 {
  38. repo, _ := file.CloneIntoGitHome(remote, branch)
  39. if len(repo) > 0 {
  40. home = repo
  41. }
  42. }
  43. if len(home) > 0 {
  44. pathx.RegisterGoctlHome(home)
  45. }
  46. if len(tp) == 0 {
  47. return errors.New("missing type")
  48. }
  49. cfg, err := config.NewConfig(s)
  50. if err != nil {
  51. return err
  52. }
  53. a, err := filepath.Abs(o)
  54. if err != nil {
  55. return err
  56. }
  57. return generate.Do(&generate.Context{
  58. Types: tp,
  59. Cache: c,
  60. Output: a,
  61. Cfg: cfg,
  62. })
  63. }