cli.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. package cli
  2. import (
  3. "errors"
  4. "fmt"
  5. "path/filepath"
  6. "git.i2edu.net/i2/go-zero/tools/goctl/rpc/generator"
  7. "github.com/urfave/cli"
  8. )
  9. // RPC is to generate rpc service code from a proto file by specifying a proto file using flag src,
  10. // you can specify a target folder for code generation, when the proto file has import, you can specify
  11. // the import search directory through the proto_path command, for specific usage, please refer to protoc -h
  12. func RPC(c *cli.Context) error {
  13. src := c.String("src")
  14. out := c.String("dir")
  15. style := c.String("style")
  16. protoImportPath := c.StringSlice("proto_path")
  17. if len(src) == 0 {
  18. return errors.New("missing -src")
  19. }
  20. if len(out) == 0 {
  21. return errors.New("missing -dir")
  22. }
  23. g, err := generator.NewDefaultRPCGenerator(style)
  24. if err != nil {
  25. return err
  26. }
  27. return g.Generate(src, out, protoImportPath)
  28. }
  29. // RPCNew is to generate rpc greet service, this greet service can speed
  30. // up your understanding of the zrpc service structure
  31. func RPCNew(c *cli.Context) error {
  32. rpcname := c.Args().First()
  33. ext := filepath.Ext(rpcname)
  34. if len(ext) > 0 {
  35. return fmt.Errorf("unexpected ext: %s", ext)
  36. }
  37. style := c.String("style")
  38. protoName := rpcname + ".proto"
  39. filename := filepath.Join(".", rpcname, protoName)
  40. src, err := filepath.Abs(filename)
  41. if err != nil {
  42. return err
  43. }
  44. err = generator.ProtoTmpl(src)
  45. if err != nil {
  46. return err
  47. }
  48. g, err := generator.NewDefaultRPCGenerator(style)
  49. if err != nil {
  50. return err
  51. }
  52. return g.Generate(src, filepath.Dir(src), nil)
  53. }
  54. // RPCTemplate is the entry for generate rpc template
  55. func RPCTemplate(c *cli.Context) error {
  56. protoFile := c.String("o")
  57. if len(protoFile) == 0 {
  58. return errors.New("missing -o")
  59. }
  60. return generator.ProtoTmpl(protoFile)
  61. }