check.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. return check(install, force)
  39. }
  40. func check(install, force bool) error {
  41. pending := true
  42. console.Info("[goctl-env]: preparing to check env")
  43. defer func() {
  44. if p := recover(); p != nil {
  45. console.Error("%+v", p)
  46. return
  47. }
  48. if pending {
  49. console.Success("\n[goctl-env]: congratulations! your goctl environment is ready!")
  50. } else {
  51. console.Error(`
  52. [goctl-env]: check env finish, some dependencies is not found in PATH, you can execute
  53. command 'goctl env check --install' or 'goctl env install' to install it, for details,
  54. please see 'goctl env check --help' or 'goctl env install --help'`)
  55. }
  56. }()
  57. for _, e := range bins {
  58. time.Sleep(200 * time.Millisecond)
  59. console.Info("")
  60. console.Info("[goctl-env]: looking up %q", e.name)
  61. if e.exists {
  62. console.Success("[goctl-env]: %q is installed", e.name)
  63. continue
  64. }
  65. console.Warning("[goctl-env]: %q is not found in PATH", e.name)
  66. if install {
  67. install := func() {
  68. console.Info("[goctl-env]: preparing to install %q", e.name)
  69. path, err := e.get(env.Get(env.GoctlCache))
  70. if err != nil {
  71. console.Error("[goctl-env]: an error interrupted the installation: %+v", err)
  72. pending = false
  73. } else {
  74. console.Success("[goctl-env]: %q is already installed in %q", e.name, path)
  75. }
  76. }
  77. if force {
  78. install()
  79. continue
  80. }
  81. console.Info("[goctl-env]: do you want to install %q [y: YES, n: No]", e.name)
  82. for {
  83. var in string
  84. fmt.Scanln(&in)
  85. var brk bool
  86. switch {
  87. case strings.EqualFold(in, "y"):
  88. install()
  89. brk = true
  90. case strings.EqualFold(in, "n"):
  91. pending = false
  92. console.Info("[goctl-env]: %q installation is ignored", e.name)
  93. brk = true
  94. default:
  95. console.Error("[goctl-env]: invalid input, input 'y' for yes, 'n' for no")
  96. }
  97. if brk {
  98. break
  99. }
  100. }
  101. } else {
  102. pending = false
  103. }
  104. }
  105. return nil
  106. }