cli.go 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. package cli
  2. import (
  3. "errors"
  4. "fmt"
  5. "path/filepath"
  6. "github.com/urfave/cli"
  7. "github.com/zeromicro/go-zero/tools/goctl/rpc/generator"
  8. "github.com/zeromicro/go-zero/tools/goctl/util"
  9. "github.com/zeromicro/go-zero/tools/goctl/util/pathx"
  10. )
  11. // RPCNew is to generate rpc greet service, this greet service can speed
  12. // up your understanding of the zrpc service structure
  13. func RPCNew(c *cli.Context) error {
  14. rpcname := c.Args().First()
  15. ext := filepath.Ext(rpcname)
  16. if len(ext) > 0 {
  17. return fmt.Errorf("unexpected ext: %s", ext)
  18. }
  19. style := c.String("style")
  20. home := c.String("home")
  21. remote := c.String("remote")
  22. branch := c.String("branch")
  23. verbose := c.Bool("verbose")
  24. if len(remote) > 0 {
  25. repo, _ := util.CloneIntoGitHome(remote, branch)
  26. if len(repo) > 0 {
  27. home = repo
  28. }
  29. }
  30. if len(home) > 0 {
  31. pathx.RegisterGoctlHome(home)
  32. }
  33. protoName := rpcname + ".proto"
  34. filename := filepath.Join(".", rpcname, protoName)
  35. src, err := filepath.Abs(filename)
  36. if err != nil {
  37. return err
  38. }
  39. err = generator.ProtoTmpl(src)
  40. if err != nil {
  41. return err
  42. }
  43. var ctx generator.ZRpcContext
  44. ctx.Src = src
  45. ctx.GoOutput = filepath.Dir(src)
  46. ctx.GrpcOutput = filepath.Dir(src)
  47. ctx.IsGooglePlugin = true
  48. ctx.Output = filepath.Dir(src)
  49. ctx.ProtocCmd = fmt.Sprintf("protoc -I=%s %s --go_out=%s --go-grpc_out=%s", filepath.Dir(src), filepath.Base(src), filepath.Dir(src), filepath.Dir(src))
  50. g := generator.NewGenerator(style, verbose)
  51. return g.Generate(&ctx)
  52. }
  53. // RPCTemplate is the entry for generate rpc template
  54. func RPCTemplate(c *cli.Context) error {
  55. if c.NumFlags() == 0 {
  56. cli.ShowCommandHelpAndExit(c, "template", 1)
  57. }
  58. protoFile := c.String("o")
  59. home := c.String("home")
  60. remote := c.String("remote")
  61. branch := c.String("branch")
  62. if len(remote) > 0 {
  63. repo, _ := util.CloneIntoGitHome(remote, branch)
  64. if len(repo) > 0 {
  65. home = repo
  66. }
  67. }
  68. if len(home) > 0 {
  69. pathx.RegisterGoctlHome(home)
  70. }
  71. if len(protoFile) == 0 {
  72. return errors.New("missing -o")
  73. }
  74. return generator.ProtoTmpl(protoFile)
  75. }