cli.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. package cli
  2. import (
  3. "errors"
  4. "fmt"
  5. "path/filepath"
  6. "runtime"
  7. "github.com/urfave/cli"
  8. "github.com/tal-tech/go-zero/tools/goctl/rpc/generator"
  9. "github.com/tal-tech/go-zero/tools/goctl/util"
  10. "github.com/tal-tech/go-zero/tools/goctl/util/env"
  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. if len(home) > 0 {
  26. util.RegisterGoctlHome(home)
  27. }
  28. if len(src) == 0 {
  29. return errors.New("missing -src")
  30. }
  31. if len(out) == 0 {
  32. return errors.New("missing -dir")
  33. }
  34. g, err := generator.NewDefaultRPCGenerator(style)
  35. if err != nil {
  36. return err
  37. }
  38. return g.Generate(src, out, protoImportPath, goOptions...)
  39. }
  40. func prepare() error {
  41. if !env.CanExec() {
  42. return fmt.Errorf("%s: can not start new processes using os.StartProcess or exec.Command", runtime.GOOS)
  43. }
  44. if _, err := env.LookUpGo(); err != nil {
  45. return err
  46. }
  47. if _, err := env.LookUpProtoc(); err != nil {
  48. return err
  49. }
  50. return nil
  51. }
  52. // RPCNew is to generate rpc greet service, this greet service can speed
  53. // up your understanding of the zrpc service structure
  54. func RPCNew(c *cli.Context) error {
  55. rpcname := c.Args().First()
  56. ext := filepath.Ext(rpcname)
  57. if len(ext) > 0 {
  58. return fmt.Errorf("unexpected ext: %s", ext)
  59. }
  60. style := c.String("style")
  61. home := c.String("home")
  62. if len(home) > 0 {
  63. util.RegisterGoctlHome(home)
  64. }
  65. protoName := rpcname + ".proto"
  66. filename := filepath.Join(".", rpcname, protoName)
  67. src, err := filepath.Abs(filename)
  68. if err != nil {
  69. return err
  70. }
  71. err = generator.ProtoTmpl(src)
  72. if err != nil {
  73. return err
  74. }
  75. g, err := generator.NewDefaultRPCGenerator(style)
  76. if err != nil {
  77. return err
  78. }
  79. return g.Generate(src, filepath.Dir(src), nil)
  80. }
  81. // RPCTemplate is the entry for generate rpc template
  82. func RPCTemplate(c *cli.Context) error {
  83. protoFile := c.String("o")
  84. home := c.String("home")
  85. if len(home) > 0 {
  86. util.RegisterGoctlHome(home)
  87. }
  88. if len(protoFile) == 0 {
  89. return errors.New("missing -o")
  90. }
  91. return generator.ProtoTmpl(protoFile)
  92. }