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