quickstart.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. package quickstart
  2. import (
  3. "fmt"
  4. "os"
  5. "path/filepath"
  6. "github.com/spf13/cobra"
  7. "github.com/zeromicro/go-zero/core/logx"
  8. "github.com/zeromicro/go-zero/tools/goctl/util/console"
  9. "github.com/zeromicro/go-zero/tools/goctl/util/ctx"
  10. "github.com/zeromicro/go-zero/tools/goctl/util/pathx"
  11. )
  12. const baseDir = "greet"
  13. var (
  14. log = console.NewColorConsole(true)
  15. projectDir string
  16. )
  17. func cleanWorkSpace(projectDir string) {
  18. var command string
  19. var breakeState bool
  20. fmt.Printf("Detected that the %q already exists, do you clean up?"+
  21. " [y: YES, n: NO]: ", projectDir)
  22. for {
  23. fmt.Scanln(&command)
  24. switch command {
  25. case "y":
  26. log.Debug("Clean workspace...")
  27. _ = os.RemoveAll(projectDir)
  28. breakeState = true
  29. break
  30. case "n":
  31. log.Error("User canceled")
  32. os.Exit(1)
  33. default:
  34. fmt.Println("Invalid command, try again...")
  35. }
  36. if breakeState {
  37. break
  38. }
  39. }
  40. }
  41. func initProject() {
  42. wd, err := os.Getwd()
  43. logx.Must(err)
  44. projectDir = filepath.Join(wd, baseDir)
  45. if exists := pathx.FileExists(projectDir); exists {
  46. cleanWorkSpace(projectDir)
  47. }
  48. log.Must(pathx.MkdirIfNotExist(projectDir))
  49. if hasGoMod, _ := ctx.IsGoMod(projectDir); hasGoMod {
  50. return
  51. }
  52. if exitCode := execCommand(projectDir, "go mod init "+baseDir); exitCode != 0 {
  53. log.Fatalln("Init process exit")
  54. }
  55. }
  56. func run(_ *cobra.Command, _ []string) error {
  57. initProject()
  58. switch varStringServiceType {
  59. case serviceTypeMono:
  60. newMonoService(false).start()
  61. case serviceTypeMicro:
  62. newMicroService().start()
  63. default:
  64. return fmt.Errorf("invalid service type, expected %s | %s",
  65. serviceTypeMono, serviceTypeMicro)
  66. }
  67. return nil
  68. }