check.go 2.7 KB

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