ctx.go 2.4 KB

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