-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
250 lines (233 loc) · 7.71 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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
// This file is not part of the project structure. This file is just a generator
// for `ayapingping-go` (this go module), and will not be generated when creating new project.
package main
import (
"bufio"
"bytes"
"fmt"
"io/fs"
"os"
"os/exec"
"path"
"path/filepath"
"regexp"
"runtime"
"strings"
)
type input struct {
text string
textLess string
val string
}
type option struct {
welcomeMessage *input
projectName *input
goModulePath *input
summary *input
confirmation *input
}
var version = "v3.0.2"
var baseModulePath = "github.com/dalikewara/ayapingping-go/v3"
var opt = &option{
welcomeMessage: &input{
text: fmt.Sprintf("Welcome to AyaPingPing (Go) %s. Let's create a new project!", version),
},
projectName: &input{
text: "Project name (ex: my-project)",
textLess: "Project name",
},
goModulePath: &input{
text: "Go module path (ex: my-project, or example.com/user_example/my-project)",
textLess: "Go module path",
},
summary: &input{
text: "We'll generate new project based on these options:",
},
confirmation: &input{
text: "Is that ok?",
},
}
func main() {
reader := bufio.NewReader(os.Stdin)
fmt.Println(opt.welcomeMessage.text)
opt.projectName.val = readStatementInput(reader, opt.projectName.text, false)
opt.goModulePath.val = readStatementInput(reader, opt.goModulePath.text, false)
fmt.Println(fmt.Sprintf("\n%s\n", opt.summary.text))
fmt.Println(fmt.Sprintf("%s: %s", opt.projectName.textLess, opt.projectName.val))
fmt.Println(fmt.Sprintf("%s: %s", opt.goModulePath.textLess, opt.goModulePath.val))
fmt.Println("")
opt.confirmation.val = readQuestionInput(reader, opt.confirmation.text, false)
if opt.confirmation.val == "y" {
generator(opt)
} else {
fmt.Println("You just refuse to confirm, so we'll not generate your project")
}
}
func readStatementInput(reader *bufio.Reader, text string, isWrong bool) string {
if !isWrong {
fmt.Print(fmt.Sprintf("%s... ", text))
} else {
fmt.Print(fmt.Sprintf("Wrong, %s%s... ", strings.ToLower(text[0:1]), text[1:]))
}
val, err := reader.ReadString('\n')
val = strings.TrimSpace(val)
if err != nil {
panic(err)
}
if val == "" {
return readStatementInput(reader, text, true)
}
return val
}
func readQuestionInput(reader *bufio.Reader, text string, isWrong bool) string {
if !isWrong {
fmt.Print(fmt.Sprintf("%s (y/n)... ", text))
} else {
fmt.Print(fmt.Sprintf("Wrong, %s%s (y/n)... ", strings.ToLower(text[0:1]), text[1:]))
}
val, err := reader.ReadString('\n')
val = strings.TrimSpace(val)
if err != nil {
panic(err)
}
if val != "n" && val != "y" {
return readQuestionInput(reader, text, true)
}
return val
}
func generator(option *option) {
fmt.Println("")
_, baseFile, _, runtimeOk := runtime.Caller(0)
if !runtimeOk {
panic("No package runtime found")
return
}
baseDir := filepath.Dir(baseFile)
projectName := option.projectName.val
goModulePath := option.goModulePath.val
if _, err := os.Stat(projectName); !os.IsNotExist(err) {
fmt.Println(fmt.Sprintf("We can't create the project because a folder with name `%s` already exists", projectName))
return
}
if err := os.Mkdir(projectName, os.ModePerm); err != nil {
panic(err)
}
if err := filepath.Walk(baseDir, func(path string, info fs.FileInfo, err error) error {
if err != nil {
panic(err)
}
runtimePath := strings.TrimPrefix(strings.TrimPrefix(path, baseDir), string(os.PathSeparator))
projectPath := filepath.Join(projectName, runtimePath)
projectPath = strings.ReplaceAll(projectPath, "/structure", "")
walkCreateDir(info, "infra", runtimePath, projectPath)
walkCreateFileAndReplaceModule(info, "infra", path, projectPath, "")
walkCreateDir(info, "migration", runtimePath, projectPath)
walkCreateFileAndReplaceModule(info, "migration", path, projectPath, "")
walkCreateDir(info, "src", runtimePath, projectPath)
walkCreateFileAndReplaceModule(info, "src", path, projectPath, goModulePath)
walkWriteFile(info, "structure/.dockerignore", runtimePath, path, projectPath, goModulePath)
walkWriteFile(info, "structure/Dockerfile", runtimePath, path, projectPath, goModulePath)
walkWriteFile(info, "structure/Makefile", runtimePath, path, projectPath, goModulePath)
walkWriteFile(info, "structure/main.go", runtimePath, path, projectPath, goModulePath)
walkWriteFile(info, "structure/.env.example", runtimePath, path, projectPath, goModulePath)
walkWriteEnvFromExample("structure/.env.example", runtimePath, path, projectPath)
walkWriteFile(info, ".gitignore", runtimePath, path, projectPath, goModulePath)
walkWriteFile(info, "LICENSE", runtimePath, path, projectPath, goModulePath)
walkWriteFile(info, "README.md", runtimePath, path, projectPath, goModulePath)
return nil
}); err != nil {
panic(err)
}
execGoModInit(projectName, goModulePath)
execGoModTidy(projectName)
execGoModVendor(projectName)
fmt.Println("Done.")
}
func execGoModInit(projectName, goModulePath string) {
cmd := exec.Command("go", "mod", "init", goModulePath)
cmd.Dir = projectName
if err := cmd.Run(); err != nil {
fmt.Println("Something went wrong when executing `go mod init`: ", err.Error())
return
}
fmt.Println("Execute `go mod init`... [ok]")
}
func execGoModTidy(projectName string) {
fmt.Println("Executing `go mod tidy`. Please wait...")
cmd := exec.Command("go", "mod", "tidy")
cmd.Dir = projectName
if err := cmd.Run(); err != nil {
fmt.Println("Something went wrong when executing `go mod tidy`: ", err.Error())
return
}
fmt.Println("Execute `go mod tidy`... [ok]")
}
func execGoModVendor(projectName string) {
fmt.Println("Executing `go mod vendor`. Please wait...")
cmd := exec.Command("go", "mod", "vendor")
cmd.Dir = projectName
if err := cmd.Run(); err != nil {
fmt.Println("Something went wrong when executing `go mod vendor`: ", err.Error())
return
}
fmt.Println("Execute `go mod vendor`... [ok]")
}
func writeFile(sourcePath, destinationPath, newModulePath string) {
fData, err := os.ReadFile(sourcePath)
if err != nil {
panic(err)
}
if newModulePath != "" {
fData = bytes.Replace(fData, []byte(baseModulePath+"/structure"), []byte(newModulePath), -1)
}
if err = os.WriteFile(destinationPath, fData, 0666); err != nil {
panic(err)
}
}
func walkCreateDir(fileInfo fs.FileInfo, folderName, fileName, destinationPath string) {
if folderName == fileName && fileInfo.IsDir() {
if err := os.MkdirAll(destinationPath, os.ModePerm); err != nil {
panic(err)
}
fmt.Println(fmt.Sprintf("Create %s... [ok]", destinationPath))
}
}
func walkCreateFileAndReplaceModule(fileInfo fs.FileInfo, fileName, sourcePath, destinationPath,
newModulePath string) {
matched, err := regexp.MatchString(fileName, sourcePath)
gitKeepMatched, err2 := regexp.MatchString(".gitkeep", sourcePath)
if err != nil || err2 != nil {
panic(err)
}
if matched {
if fileInfo.IsDir() {
if err = os.MkdirAll(destinationPath, os.ModePerm); err != nil {
panic(err)
}
} else if !gitKeepMatched {
writeFile(sourcePath, destinationPath, newModulePath)
}
fmt.Println(fmt.Sprintf("Create %s... [ok]", destinationPath))
}
}
func walkWriteFile(fileInfo fs.FileInfo, folderName, fileName, sourcePath, destinationPath,
newModulePath string) {
if folderName == fileName && !fileInfo.IsDir() {
writeFile(sourcePath, destinationPath, newModulePath)
fmt.Println(fmt.Sprintf("Create %s... [ok]", destinationPath))
}
}
func walkWriteEnvFromExample(folderName, fileName, sourcePath, destinationPath string) {
if folderName == fileName {
fData, err := os.ReadFile(sourcePath)
if err != nil {
panic(err)
}
newDestDir, _ := path.Split(destinationPath)
newDest := path.Join(newDestDir, ".env")
if err = os.WriteFile(newDest, fData, 0666); err != nil {
panic(err)
}
fmt.Println(fmt.Sprintf("Create %s... [ok]", newDest))
}
}