cli.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. package cli
  2. import (
  3. "errors"
  4. "fmt"
  5. "path/filepath"
  6. "strings"
  7. "github.com/urfave/cli"
  8. "github.com/zeromicro/go-zero/tools/goctl/rpc/generator"
  9. "github.com/zeromicro/go-zero/tools/goctl/util"
  10. "github.com/zeromicro/go-zero/tools/goctl/util/console"
  11. "github.com/zeromicro/go-zero/tools/goctl/util/pathx"
  12. )
  13. // RPCNew is to generate rpc greet service, this greet service can speed
  14. // up your understanding of the zrpc service structure
  15. func RPCNew(c *cli.Context) error {
  16. if c.NArg() == 0 {
  17. cli.ShowCommandHelpAndExit(c, "new", 1)
  18. }
  19. rpcname := c.Args().First()
  20. ext := filepath.Ext(rpcname)
  21. if len(ext) > 0 {
  22. return fmt.Errorf("unexpected ext: %s", ext)
  23. }
  24. style := c.String("style")
  25. home := c.String("home")
  26. remote := c.String("remote")
  27. branch := c.String("branch")
  28. verbose := c.Bool("verbose")
  29. if len(remote) > 0 {
  30. repo, _ := util.CloneIntoGitHome(remote, branch)
  31. if len(repo) > 0 {
  32. home = repo
  33. }
  34. }
  35. if len(home) > 0 {
  36. pathx.RegisterGoctlHome(home)
  37. }
  38. protoName := rpcname + ".proto"
  39. filename := filepath.Join(".", rpcname, protoName)
  40. src, err := filepath.Abs(filename)
  41. if err != nil {
  42. return err
  43. }
  44. err = generator.ProtoTmpl(src)
  45. if err != nil {
  46. return err
  47. }
  48. var ctx generator.ZRpcContext
  49. ctx.Src = src
  50. ctx.GoOutput = filepath.Dir(src)
  51. ctx.GrpcOutput = filepath.Dir(src)
  52. ctx.IsGooglePlugin = true
  53. ctx.Output = filepath.Dir(src)
  54. ctx.ProtocCmd = fmt.Sprintf("protoc -I=%s %s --go_out=%s --go-grpc_out=%s", filepath.Dir(src), filepath.Base(src), filepath.Dir(src), filepath.Dir(src))
  55. grpcOptList := c.StringSlice("go-grpc_opt")
  56. if len(grpcOptList) > 0 {
  57. ctx.ProtocCmd += " --go-grpc_opt=" + strings.Join(grpcOptList, ",")
  58. }
  59. goOptList := c.StringSlice("go_opt")
  60. if len(goOptList) > 0 {
  61. ctx.ProtocCmd += " --go_opt=" + strings.Join(goOptList, ",")
  62. }
  63. g := generator.NewGenerator(style, verbose)
  64. return g.Generate(&ctx)
  65. }
  66. // RPCTemplate is the entry for generate rpc template
  67. func RPCTemplate(c *cli.Context) error {
  68. console.Warning("deprecated: goctl rpc template -o is deprecated and will be removed in the future, use goctl rpc -o instead")
  69. if c.NumFlags() == 0 {
  70. cli.ShowCommandHelpAndExit(c, "template", 1)
  71. }
  72. protoFile := c.String("o")
  73. home := c.String("home")
  74. remote := c.String("remote")
  75. branch := c.String("branch")
  76. if len(remote) > 0 {
  77. repo, _ := util.CloneIntoGitHome(remote, branch)
  78. if len(repo) > 0 {
  79. home = repo
  80. }
  81. }
  82. if len(home) > 0 {
  83. pathx.RegisterGoctlHome(home)
  84. }
  85. if len(protoFile) == 0 {
  86. return errors.New("missing -o")
  87. }
  88. return generator.ProtoTmpl(protoFile)
  89. }