cli.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. package cli
  2. import (
  3. "errors"
  4. "fmt"
  5. "path/filepath"
  6. "runtime"
  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/env"
  12. "github.com/zeromicro/go-zero/tools/goctl/util/pathx"
  13. )
  14. // Deprecated: use ZRPC instead.
  15. // RPC is to generate rpc service code from a proto file by specifying a proto file using flag src,
  16. // you can specify a target folder for code generation, when the proto file has import, you can specify
  17. // the import search directory through the proto_path command, for specific usage, please refer to protoc -h
  18. func RPC(c *cli.Context) error {
  19. console.Warning("deprecated: use %q instead, for the details see %q",
  20. "goctl rpc protoc", "goctl rpc protoc --help")
  21. if err := prepare(); err != nil {
  22. return err
  23. }
  24. src := c.String("src")
  25. out := c.String("dir")
  26. style := c.String("style")
  27. protoImportPath := c.StringSlice("proto_path")
  28. goOptions := c.StringSlice("go_opt")
  29. home := c.String("home")
  30. remote := c.String("remote")
  31. branch := c.String("branch")
  32. if len(remote) > 0 {
  33. repo, _ := util.CloneIntoGitHome(remote, branch)
  34. if len(repo) > 0 {
  35. home = repo
  36. }
  37. }
  38. if len(home) > 0 {
  39. pathx.RegisterGoctlHome(home)
  40. }
  41. if len(src) == 0 {
  42. return errors.New("missing -src")
  43. }
  44. if len(out) == 0 {
  45. return errors.New("missing -dir")
  46. }
  47. g, err := generator.NewDefaultRPCGenerator(style)
  48. if err != nil {
  49. return err
  50. }
  51. return g.Generate(src, out, protoImportPath, goOptions...)
  52. }
  53. func prepare() error {
  54. if !env.CanExec() {
  55. return fmt.Errorf("%s: can not start new processes using os.StartProcess or exec.Command", runtime.GOOS)
  56. }
  57. if _, err := env.LookUpGo(); err != nil {
  58. return err
  59. }
  60. if _, err := env.LookUpProtoc(); err != nil {
  61. return err
  62. }
  63. if _, err := env.LookUpProtocGenGo(); err != nil {
  64. return err
  65. }
  66. return nil
  67. }
  68. // RPCNew is to generate rpc greet service, this greet service can speed
  69. // up your understanding of the zrpc service structure
  70. func RPCNew(c *cli.Context) error {
  71. console.Warning("deprecated: it will be removed in the feature, zrpc code generation please use %q instead",
  72. "goctl rpc protoc")
  73. rpcname := c.Args().First()
  74. ext := filepath.Ext(rpcname)
  75. if len(ext) > 0 {
  76. return fmt.Errorf("unexpected ext: %s", ext)
  77. }
  78. style := c.String("style")
  79. home := c.String("home")
  80. remote := c.String("remote")
  81. branch := c.String("branch")
  82. if len(remote) > 0 {
  83. repo, _ := util.CloneIntoGitHome(remote, branch)
  84. if len(repo) > 0 {
  85. home = repo
  86. }
  87. }
  88. if len(home) > 0 {
  89. pathx.RegisterGoctlHome(home)
  90. }
  91. protoName := rpcname + ".proto"
  92. filename := filepath.Join(".", rpcname, protoName)
  93. src, err := filepath.Abs(filename)
  94. if err != nil {
  95. return err
  96. }
  97. err = generator.ProtoTmpl(src)
  98. if err != nil {
  99. return err
  100. }
  101. g, err := generator.NewDefaultRPCGenerator(style)
  102. if err != nil {
  103. return err
  104. }
  105. return g.Generate(src, filepath.Dir(src), nil)
  106. }
  107. // RPCTemplate is the entry for generate rpc template
  108. func RPCTemplate(c *cli.Context) error {
  109. protoFile := c.String("o")
  110. home := c.String("home")
  111. remote := c.String("remote")
  112. branch := c.String("branch")
  113. if len(remote) > 0 {
  114. repo, _ := util.CloneIntoGitHome(remote, branch)
  115. if len(repo) > 0 {
  116. home = repo
  117. }
  118. }
  119. if len(home) > 0 {
  120. pathx.RegisterGoctlHome(home)
  121. }
  122. if len(protoFile) == 0 {
  123. return errors.New("missing -o")
  124. }
  125. return generator.ProtoTmpl(protoFile)
  126. }