cli.go 2.9 KB

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