cli.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. 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. if _, err := env.LookUpProtocGenGo(); err != nil {
  51. return err
  52. }
  53. return nil
  54. }
  55. // RPCNew is to generate rpc greet service, this greet service can speed
  56. // up your understanding of the zrpc service structure
  57. func RPCNew(c *cli.Context) error {
  58. rpcname := c.Args().First()
  59. ext := filepath.Ext(rpcname)
  60. if len(ext) > 0 {
  61. return fmt.Errorf("unexpected ext: %s", ext)
  62. }
  63. style := c.String("style")
  64. home := c.String("home")
  65. if len(home) > 0 {
  66. util.RegisterGoctlHome(home)
  67. }
  68. protoName := rpcname + ".proto"
  69. filename := filepath.Join(".", rpcname, protoName)
  70. src, err := filepath.Abs(filename)
  71. if err != nil {
  72. return err
  73. }
  74. err = generator.ProtoTmpl(src)
  75. if err != nil {
  76. return err
  77. }
  78. g, err := generator.NewDefaultRPCGenerator(style)
  79. if err != nil {
  80. return err
  81. }
  82. return g.Generate(src, filepath.Dir(src), nil)
  83. }
  84. // RPCTemplate is the entry for generate rpc template
  85. func RPCTemplate(c *cli.Context) error {
  86. protoFile := c.String("o")
  87. home := c.String("home")
  88. if len(home) > 0 {
  89. util.RegisterGoctlHome(home)
  90. }
  91. if len(protoFile) == 0 {
  92. return errors.New("missing -o")
  93. }
  94. return generator.ProtoTmpl(protoFile)
  95. }