cli.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. if len(remote) > 0 {
  32. repo, _ := util.CloneIntoGitHome(remote)
  33. if len(repo) > 0 {
  34. home = repo
  35. }
  36. }
  37. if len(home) > 0 {
  38. pathx.RegisterGoctlHome(home)
  39. }
  40. if len(src) == 0 {
  41. return errors.New("missing -src")
  42. }
  43. if len(out) == 0 {
  44. return errors.New("missing -dir")
  45. }
  46. g, err := generator.NewDefaultRPCGenerator(style)
  47. if err != nil {
  48. return err
  49. }
  50. return g.Generate(src, out, protoImportPath, goOptions...)
  51. }
  52. func prepare() error {
  53. if !env.CanExec() {
  54. return fmt.Errorf("%s: can not start new processes using os.StartProcess or exec.Command", runtime.GOOS)
  55. }
  56. if _, err := env.LookUpGo(); err != nil {
  57. return err
  58. }
  59. if _, err := env.LookUpProtoc(); err != nil {
  60. return err
  61. }
  62. if _, err := env.LookUpProtocGenGo(); err != nil {
  63. return err
  64. }
  65. return nil
  66. }
  67. // RPCNew is to generate rpc greet service, this greet service can speed
  68. // up your understanding of the zrpc service structure
  69. func RPCNew(c *cli.Context) error {
  70. console.Warning("deprecated: it will be removed in the feature, zrpc code generation please use %q instead",
  71. "goctl rpc protoc")
  72. rpcname := c.Args().First()
  73. ext := filepath.Ext(rpcname)
  74. if len(ext) > 0 {
  75. return fmt.Errorf("unexpected ext: %s", ext)
  76. }
  77. style := c.String("style")
  78. home := c.String("home")
  79. remote := c.String("remote")
  80. if len(remote) > 0 {
  81. repo, _ := util.CloneIntoGitHome(remote)
  82. if len(repo) > 0 {
  83. home = repo
  84. }
  85. }
  86. if len(home) > 0 {
  87. pathx.RegisterGoctlHome(home)
  88. }
  89. protoName := rpcname + ".proto"
  90. filename := filepath.Join(".", rpcname, protoName)
  91. src, err := filepath.Abs(filename)
  92. if err != nil {
  93. return err
  94. }
  95. err = generator.ProtoTmpl(src)
  96. if err != nil {
  97. return err
  98. }
  99. g, err := generator.NewDefaultRPCGenerator(style)
  100. if err != nil {
  101. return err
  102. }
  103. return g.Generate(src, filepath.Dir(src), nil)
  104. }
  105. // RPCTemplate is the entry for generate rpc template
  106. func RPCTemplate(c *cli.Context) error {
  107. protoFile := c.String("o")
  108. home := c.String("home")
  109. remote := c.String("remote")
  110. if len(remote) > 0 {
  111. repo, _ := util.CloneIntoGitHome(remote)
  112. if len(repo) > 0 {
  113. home = repo
  114. }
  115. }
  116. if len(home) > 0 {
  117. pathx.RegisterGoctlHome(home)
  118. }
  119. if len(protoFile) == 0 {
  120. return errors.New("missing -o")
  121. }
  122. return generator.ProtoTmpl(protoFile)
  123. }