ctx.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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/stringx"
  11. "github.com/urfave/cli"
  12. )
  13. const (
  14. flagSrc = "src"
  15. flagDir = "dir"
  16. flagShared = "shared"
  17. flagService = "service"
  18. flagIdea = "idea"
  19. )
  20. type (
  21. 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. SharedDir string
  31. console.Console
  32. }
  33. )
  34. func MustCreateRpcContext(protoSrc, targetDir, sharedDir, serviceName string, idea bool) *RpcContext {
  35. log := console.NewConsole(idea)
  36. info, err := prepare(log)
  37. log.Must(err)
  38. if stringx.From(protoSrc).IsEmptyOrSpace() {
  39. log.Fatalln("expected proto source, but nothing found")
  40. }
  41. srcFp, err := filepath.Abs(protoSrc)
  42. log.Must(err)
  43. if !util.FileExists(srcFp) {
  44. log.Fatalln("%s is not exists", srcFp)
  45. }
  46. current := filepath.Dir(srcFp)
  47. if stringx.From(targetDir).IsEmptyOrSpace() {
  48. targetDir = current
  49. }
  50. if stringx.From(sharedDir).IsEmptyOrSpace() {
  51. sharedDir = filepath.Join(current, "shared")
  52. }
  53. targetDirFp, err := filepath.Abs(targetDir)
  54. log.Must(err)
  55. sharedFp, err := filepath.Abs(sharedDir)
  56. log.Must(err)
  57. if stringx.From(serviceName).IsEmptyOrSpace() {
  58. serviceName = getServiceFromRpcStructure(targetDirFp)
  59. }
  60. serviceNameString := stringx.From(serviceName)
  61. if serviceNameString.IsEmptyOrSpace() {
  62. log.Fatalln("service name is not found")
  63. }
  64. return &RpcContext{
  65. ProjectPath: info.Path,
  66. ProjectName: stringx.From(info.Name),
  67. ServiceName: serviceNameString,
  68. CurrentPath: current,
  69. Module: info.GoMod.Module,
  70. ProtoFileSrc: srcFp,
  71. ProtoSource: filepath.Base(srcFp),
  72. TargetDir: targetDirFp,
  73. SharedDir: sharedFp,
  74. Console: log,
  75. }
  76. }
  77. func MustCreateRpcContextFromCli(ctx *cli.Context) *RpcContext {
  78. os := runtime.GOOS
  79. switch os {
  80. case "darwin", "windows":
  81. default:
  82. logx.Must(fmt.Errorf("unexpected os: %s", os))
  83. }
  84. protoSrc := ctx.String(flagSrc)
  85. targetDir := ctx.String(flagDir)
  86. sharedDir := ctx.String(flagShared)
  87. serviceName := ctx.String(flagService)
  88. idea := ctx.Bool(flagIdea)
  89. return MustCreateRpcContext(protoSrc, targetDir, sharedDir, serviceName, idea)
  90. }
  91. func getServiceFromRpcStructure(targetDir string) string {
  92. targetDir = filepath.Clean(targetDir)
  93. suffix := filepath.Join("cmd", "rpc")
  94. return filepath.Base(strings.TrimSuffix(targetDir, suffix))
  95. }