cli.go 3.0 KB

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