ctx.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. package ctx
  2. import (
  3. "fmt"
  4. "path/filepath"
  5. "runtime"
  6. "strings"
  7. "github.com/tal-tech/go-zero/core/logx"
  8. "github.com/tal-tech/go-zero/tools/goctl/util"
  9. "github.com/tal-tech/go-zero/tools/goctl/util/console"
  10. "github.com/tal-tech/go-zero/tools/goctl/util/project"
  11. "github.com/tal-tech/go-zero/tools/goctl/util/stringx"
  12. "github.com/tal-tech/go-zero/tools/goctl/vars"
  13. "github.com/urfave/cli"
  14. )
  15. const (
  16. flagSrc = "src"
  17. flagDir = "dir"
  18. flagService = "service"
  19. flagIdea = "idea"
  20. )
  21. type RpcContext struct {
  22. ProjectPath string
  23. ProjectName stringx.String
  24. ServiceName stringx.String
  25. CurrentPath string
  26. Module string
  27. ProtoFileSrc string
  28. ProtoSource string
  29. TargetDir string
  30. IsInGoEnv bool
  31. console.Console
  32. }
  33. func MustCreateRpcContext(protoSrc, targetDir, serviceName string, idea bool) *RpcContext {
  34. log := console.NewConsole(idea)
  35. if stringx.From(protoSrc).IsEmptyOrSpace() {
  36. log.Fatalln("expected proto source, but nothing found")
  37. }
  38. srcFp, err := filepath.Abs(protoSrc)
  39. log.Must(err)
  40. if !util.FileExists(srcFp) {
  41. log.Fatalln("%s is not exists", srcFp)
  42. }
  43. current := filepath.Dir(srcFp)
  44. if stringx.From(targetDir).IsEmptyOrSpace() {
  45. targetDir = current
  46. }
  47. targetDirFp, err := filepath.Abs(targetDir)
  48. log.Must(err)
  49. if stringx.From(serviceName).IsEmptyOrSpace() {
  50. serviceName = getServiceFromRpcStructure(targetDirFp)
  51. }
  52. serviceNameString := stringx.From(serviceName)
  53. if serviceNameString.IsEmptyOrSpace() {
  54. log.Fatalln("service name not found")
  55. }
  56. info, err := project.Prepare(targetDir, true)
  57. log.Must(err)
  58. return &RpcContext{
  59. ProjectPath: info.Path,
  60. ProjectName: stringx.From(info.Name),
  61. ServiceName: serviceNameString,
  62. CurrentPath: current,
  63. Module: info.GoMod.Module,
  64. ProtoFileSrc: srcFp,
  65. ProtoSource: filepath.Base(srcFp),
  66. TargetDir: targetDirFp,
  67. IsInGoEnv: info.IsInGoEnv,
  68. Console: log,
  69. }
  70. }
  71. func MustCreateRpcContextFromCli(ctx *cli.Context) *RpcContext {
  72. os := runtime.GOOS
  73. switch os {
  74. case vars.OsMac, vars.OsLinux, vars.OsWindows:
  75. default:
  76. logx.Must(fmt.Errorf("unexpected os: %s", os))
  77. }
  78. protoSrc := ctx.String(flagSrc)
  79. targetDir := ctx.String(flagDir)
  80. serviceName := ctx.String(flagService)
  81. idea := ctx.Bool(flagIdea)
  82. return MustCreateRpcContext(protoSrc, targetDir, serviceName, idea)
  83. }
  84. func getServiceFromRpcStructure(targetDir string) string {
  85. targetDir = filepath.Clean(targetDir)
  86. suffix := filepath.Join("cmd", "rpc")
  87. return filepath.Base(strings.TrimSuffix(targetDir, suffix))
  88. }