check.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. package env
  2. import (
  3. "fmt"
  4. "strings"
  5. "time"
  6. "github.com/urfave/cli"
  7. "github.com/zeromicro/go-zero/tools/goctl/pkg/env"
  8. "github.com/zeromicro/go-zero/tools/goctl/pkg/protoc"
  9. "github.com/zeromicro/go-zero/tools/goctl/pkg/protocgengo"
  10. "github.com/zeromicro/go-zero/tools/goctl/pkg/protocgengogrpc"
  11. "github.com/zeromicro/go-zero/tools/goctl/util/console"
  12. )
  13. type bin struct {
  14. name string
  15. exists bool
  16. get func(cacheDir string) (string, error)
  17. }
  18. var bins = []bin{
  19. {
  20. name: "protoc",
  21. exists: protoc.Exists(),
  22. get: protoc.Install,
  23. },
  24. {
  25. name: "protoc-gen-go",
  26. exists: protocgengo.Exists(),
  27. get: protocgengo.Install,
  28. },
  29. {
  30. name: "protoc-gen-go-grpc",
  31. exists: protocgengogrpc.Exists(),
  32. get: protocgengogrpc.Install,
  33. },
  34. }
  35. func Check(ctx *cli.Context) error {
  36. install := ctx.Bool("install")
  37. force := ctx.Bool("force")
  38. verbose := ctx.Bool("verbose")
  39. return Prepare(install, force, verbose)
  40. }
  41. func Prepare(install, force, verbose bool) error {
  42. log := console.NewColorConsole(verbose)
  43. pending := true
  44. log.Info("[goctl-env]: preparing to check env")
  45. defer func() {
  46. if p := recover(); p != nil {
  47. log.Error("%+v", p)
  48. return
  49. }
  50. if pending {
  51. log.Success("\n[goctl-env]: congratulations! your goctl environment is ready!")
  52. } else {
  53. log.Error(`
  54. [goctl-env]: check env finish, some dependencies is not found in PATH, you can execute
  55. command 'goctl env check --install' to install it, for details, please execute command
  56. 'goctl env check --help'`)
  57. }
  58. }()
  59. for _, e := range bins {
  60. time.Sleep(200 * time.Millisecond)
  61. log.Info("")
  62. log.Info("[goctl-env]: looking up %q", e.name)
  63. if e.exists {
  64. log.Success("[goctl-env]: %q is installed", e.name)
  65. continue
  66. }
  67. log.Warning("[goctl-env]: %q is not found in PATH", e.name)
  68. if install {
  69. install := func() {
  70. log.Info("[goctl-env]: preparing to install %q", e.name)
  71. path, err := e.get(env.Get(env.GoctlCache))
  72. if err != nil {
  73. log.Error("[goctl-env]: an error interrupted the installation: %+v", err)
  74. pending = false
  75. } else {
  76. log.Success("[goctl-env]: %q is already installed in %q", e.name, path)
  77. }
  78. }
  79. if force {
  80. install()
  81. continue
  82. }
  83. log.Info("[goctl-env]: do you want to install %q [y: YES, n: No]", e.name)
  84. for {
  85. var in string
  86. fmt.Scanln(&in)
  87. var brk bool
  88. switch {
  89. case strings.EqualFold(in, "y"):
  90. install()
  91. brk = true
  92. case strings.EqualFold(in, "n"):
  93. pending = false
  94. log.Info("[goctl-env]: %q installation is ignored", e.name)
  95. brk = true
  96. default:
  97. log.Error("[goctl-env]: invalid input, input 'y' for yes, 'n' for no")
  98. }
  99. if brk {
  100. break
  101. }
  102. }
  103. } else {
  104. pending = false
  105. }
  106. }
  107. return nil
  108. }