gen.go 906 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. package javagen
  2. import (
  3. "errors"
  4. "fmt"
  5. "strings"
  6. "github.com/logrusorgru/aurora"
  7. "github.com/tal-tech/go-zero/core/logx"
  8. "github.com/tal-tech/go-zero/tools/goctl/api/parser"
  9. "github.com/tal-tech/go-zero/tools/goctl/util"
  10. "github.com/urfave/cli"
  11. )
  12. // JavaCommand the generate java code command entrance
  13. func JavaCommand(c *cli.Context) error {
  14. apiFile := c.String("api")
  15. dir := c.String("dir")
  16. if len(apiFile) == 0 {
  17. return errors.New("missing -api")
  18. }
  19. if len(dir) == 0 {
  20. return errors.New("missing -dir")
  21. }
  22. api, err := parser.Parse(apiFile)
  23. if err != nil {
  24. return err
  25. }
  26. packetName := api.Service.Name
  27. if strings.HasSuffix(packetName, "-api") {
  28. packetName = packetName[:len(packetName)-4]
  29. }
  30. logx.Must(util.MkdirIfNotExist(dir))
  31. logx.Must(genPacket(dir, packetName, api))
  32. logx.Must(genComponents(dir, packetName, api))
  33. fmt.Println(aurora.Green("Done."))
  34. return nil
  35. }