command.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. package command
  2. import (
  3. "fmt"
  4. "os"
  5. "path/filepath"
  6. "github.com/tal-tech/go-zero/tools/goctl/rpc/ctx"
  7. "github.com/tal-tech/go-zero/tools/goctl/rpc/gen"
  8. "github.com/tal-tech/go-zero/tools/goctl/util"
  9. "github.com/urfave/cli"
  10. )
  11. func Rpc(c *cli.Context) error {
  12. rpcCtx := ctx.MustCreateRpcContextFromCli(c)
  13. generator := gen.NewDefaultRpcGenerator(rpcCtx)
  14. rpcCtx.Must(generator.Generate())
  15. return nil
  16. }
  17. func RpcTemplate(c *cli.Context) error {
  18. out := c.String("out")
  19. idea := c.Bool("idea")
  20. generator := gen.NewRpcTemplate(out, idea)
  21. generator.MustGenerate(true)
  22. return nil
  23. }
  24. func RpcNew(c *cli.Context) error {
  25. idea := c.Bool("idea")
  26. arg := c.Args().First()
  27. if len(arg) == 0 {
  28. arg = "greet"
  29. }
  30. abs, err := filepath.Abs(arg)
  31. if err != nil {
  32. return err
  33. }
  34. _, err = os.Stat(abs)
  35. if err != nil {
  36. if !os.IsNotExist(err) {
  37. return err
  38. }
  39. err = util.MkdirIfNotExist(abs)
  40. if err != nil {
  41. return err
  42. }
  43. }
  44. dir := filepath.Base(filepath.Clean(abs))
  45. protoSrc := filepath.Join(abs, fmt.Sprintf("%v.proto", dir))
  46. templateGenerator := gen.NewRpcTemplate(protoSrc, idea)
  47. templateGenerator.MustGenerate(false)
  48. rpcCtx := ctx.MustCreateRpcContext(protoSrc, "", "", idea)
  49. generator := gen.NewDefaultRpcGenerator(rpcCtx)
  50. rpcCtx.Must(generator.Generate())
  51. return nil
  52. }