-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
90 lines (74 loc) · 1.63 KB
/
main.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
80
81
82
83
84
85
86
87
88
89
90
package main
import (
"errors"
"fmt"
"os"
"github.com/manifoldco/promptui"
"github.com/hongminhcbg/mint-gen/service"
)
func getUserChoice() (templateName, projectName, goModule string, err error) {
dirs, err := os.ReadDir("templates")
if err != nil {
return
}
tmpls := make([]string, 0)
for i := 0; i < len(dirs); i++ {
tmpls = append(tmpls, dirs[i].Name())
}
prompt := promptui.Select{
Label: "Select project template",
Items: tmpls,
}
_, result, err := prompt.Run()
if err != nil {
fmt.Printf("Prompt failed %v\n", err)
return
}
fmt.Printf("You choose %q\n", result)
templateName = result
validate := func(input string) error {
if len(input) == 0 {
return errors.New("don't allow empty")
}
return nil
}
promptProjectName := promptui.Prompt{
Label: "ProjectName",
Validate: validate,
}
projectName, err = promptProjectName.Run()
if err != nil {
fmt.Printf("Prompt failed %v\n", err)
return
}
promptGomod := promptui.Prompt{
Label: "Go module",
Validate: validate,
}
goModule, err = promptGomod.Run()
if err != nil {
fmt.Printf("Prompt failed %v\n", err)
return
}
return
}
func gen(tmplName, project, gomod string) {
err := os.Mkdir(project, 0777)
if err != nil {
panic(err)
}
gen := service.New("templates/"+tmplName, project, gomod)
err = gen.Gen(project)
if err != nil {
panic(err)
}
}
func main() {
fmt.Println("Only for unix")
tmplName, project, goModule, err := getUserChoice()
if err != nil {
panic(err)
}
fmt.Printf("starting generate project: '%s' with template '%s' and go module '%s' ", project, tmplName, goModule)
gen(tmplName, project, goModule)
}