mongo.go 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. package mongo
  2. import (
  3. "errors"
  4. "path/filepath"
  5. "strings"
  6. "github.com/urfave/cli"
  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. // Action provides the entry for goctl mongo code generation.
  13. func Action(ctx *cli.Context) error {
  14. tp := ctx.StringSlice("type")
  15. c := ctx.Bool("cache")
  16. o := strings.TrimSpace(ctx.String("dir"))
  17. s := ctx.String("style")
  18. home := ctx.String("home")
  19. remote := ctx.String("remote")
  20. branch := ctx.String("branch")
  21. if len(remote) > 0 {
  22. repo, _ := file.CloneIntoGitHome(remote, branch)
  23. if len(repo) > 0 {
  24. home = repo
  25. }
  26. }
  27. if len(home) > 0 {
  28. pathx.RegisterGoctlHome(home)
  29. }
  30. if len(tp) == 0 {
  31. return errors.New("missing type")
  32. }
  33. cfg, err := config.NewConfig(s)
  34. if err != nil {
  35. return err
  36. }
  37. a, err := filepath.Abs(o)
  38. if err != nil {
  39. return err
  40. }
  41. return generate.Do(&generate.Context{
  42. Types: tp,
  43. Cache: c,
  44. Output: a,
  45. Cfg: cfg,
  46. })
  47. }