-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.go
318 lines (252 loc) · 6.5 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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
/*
gonesis
Create Golang project template ready to be pushed on GitHub with a single command
https://github.com/edoardottt/gonesis
edoardottt, https://www.edoardoottavianelli.it
Under GNU-GPL3: https://github.com/edoardottt/gonesis/blob/main/LICENSE
*/
package main
import (
"bufio"
"errors"
"fmt"
"io/ioutil"
"log"
"os"
"os/exec"
"regexp"
"strings"
)
const (
Permission0755 = 0755
Permission0775 = 0775
MDBashInit = "```bash"
Version = "1.0.1"
Banner = "gonesis v" + Version + "\n\thttps://github.com/edoardottt/gonesis\n\n"
)
var (
ErrProjectName = errors.New("the project name can contains only alphanumeric characters, _ and -")
)
func main() {
fmt.Print(Banner)
projectName, err := ProjectName()
if err != nil {
log.Fatal(err)
}
rootDir := "." + string(os.PathSeparator) + projectName
err = CreateDir(".", projectName)
if err != nil {
log.Fatal(err)
}
// description.
description := Description()
// cmd.
err = CreateDir(rootDir, "cmd")
if err != nil {
log.Fatal(err)
}
// main.
CreateMain(rootDir, projectName)
// go.mod.
name := GithubHandle()
cmd := exec.Command("go", "mod", "init", "github.com/"+name+"/"+projectName)
err = cmd.Run()
if err != nil {
log.Fatal("go.mod already exists in this folder?")
}
oldLocation := "./go.mod"
newLocation := rootDir + string(os.PathSeparator) + "go.mod"
err = os.Rename(oldLocation, newLocation)
if err != nil {
log.Fatal(err)
}
// pkg.
err = CreateDir(rootDir, "pkg")
if err != nil {
log.Fatal(err)
}
// docs.
err = CreateDir(rootDir, "docs")
if err != nil {
log.Fatal(err)
}
// internal.
err = CreateDir(rootDir, "internal")
if err != nil {
log.Fatal(err)
}
// examples.
err = CreateDir(rootDir, "examples")
if err != nil {
log.Fatal(err)
}
// api.
if AskUser("Will you need APIs?") {
err = CreateDir(rootDir, "api")
if err != nil {
log.Fatal(err)
}
}
// server.
if AskUser("Will you need a server?") {
err = CreateDir(rootDir, "server")
if err != nil {
log.Fatal(err)
}
}
// db.
if AskUser("Will you need a database?") {
err = CreateDir(rootDir, "db")
if err != nil {
log.Fatal(err)
}
}
// README.md.
Readme(rootDir, projectName, description, name)
// .gitignore.
Gitignore(rootDir)
}
// ProjectName takes as input from stdin the name of the
// project and checks if it's a valid name.
func ProjectName() (string, error) {
reader := bufio.NewReader(os.Stdin)
fmt.Print("Name of the project: ")
project, _ := reader.ReadString('\n')
if len(project) > 0 && project[len(project)-1] == '\n' {
project = project[:len(project)-1]
}
isProjectNameOkay := regexp.MustCompile(`^[a-zA-Z0-9-_]+$`)
if !isProjectNameOkay.Match([]byte(project)) {
return project, fmt.Errorf("%w", ErrProjectName)
}
return project, nil
}
// CreateMain creates the main.go file.
func CreateMain(rootDir string, projectName string) {
err := CreateFile(rootDir+string(os.PathSeparator)+"cmd", projectName+".go")
if err != nil {
log.Fatal(err)
}
main := `package main
import (
"fmt"
)
func main() {
fmt.Println("Hello, World!")
}
`
err = WriteFile(rootDir+string(os.PathSeparator)+"cmd"+string(os.PathSeparator)+projectName+".go", main)
if err != nil {
log.Fatal(err)
}
}
// GithubHandle takes as input from stdin the github profile name.
func GithubHandle() string {
reader := bufio.NewReader(os.Stdin)
fmt.Print("Github username: ")
name, _ := reader.ReadString('\n')
if len(name) > 0 && name[len(name)-1] == '\n' {
name = name[:len(name)-1]
}
return name
}
// Description takes as input from stdin the project description.
func Description() string {
reader := bufio.NewReader(os.Stdin)
fmt.Print("Project description: ")
desc, _ := reader.ReadString('\n')
if len(desc) > 0 && desc[len(desc)-1] == '\n' {
desc = desc[:len(desc)-1]
}
return desc
}
// AskUser prints the question taken as input and if
// the input is y/Y or yes/Yes/YES etc. returns true,
// false otherwise.
func AskUser(question string) bool {
reader := bufio.NewReader(os.Stdin)
fmt.Print("[ ? ] " + question + " ")
answer, _ := reader.ReadString('\n')
if len(answer) > 0 && answer[len(answer)-1] == '\n' {
answer = answer[:len(answer)-1]
}
answer = strings.ToLower(answer)
if answer == "y" || answer == "yes" {
return true
}
return false
}
// Gitignore creates the .gitignore file.
func Gitignore(rootDir string) {
err := CreateFile(rootDir, ".gitignore")
if err != nil {
log.Fatal(err)
}
gitignore := `# Binaries for programs and plugins
*.exe
*.exe~
*.dll
*.so
*.dylib
# Test binary, built with "go test -c"
*.test
# Output of the go coverage tool, specifically when used with LiteIDE
*.out
# Dependency directories (remove the comment below to include it)
# vendor/
# Go workspace file
go.work`
err = WriteFile(rootDir+string(os.PathSeparator)+".gitignore", gitignore)
if err != nil {
log.Fatal(err)
}
}
// Readme creates the README.md file.
func Readme(rootDir string, projectName string, description string, name string) {
err := CreateFile(rootDir, "README.md")
if err != nil {
log.Fatal(err)
}
readme := "# " + projectName
readme += "\n" + description
readme += "\n\nInstallation 📡\n"
readme += "-------\n"
readme += "**Go 1.17+**\n"
readme += MDBashInit + "\n"
readme += "go install -v github.com/" + name + "/" + projectName + "@latest\n"
readme += "```\n"
readme += "**otherwise**\n"
readme += MDBashInit + "\n"
readme += "go get -v github.com/" + name + "/" + projectName + "\n"
readme += "```\n\n"
readme += "Usage 💻\n"
readme += "-------\n"
readme += MDBashInit + "\n"
readme += projectName + "\n"
readme += "```\n\n"
readme += "Created with [gonesis](https://github.com/edoardottt/gonesis)❤️"
err = WriteFile(rootDir+string(os.PathSeparator)+"README.md", readme)
if err != nil {
log.Fatal(err)
}
}
//----------------------------------------
//---------------- helpers ---------------
//----------------------------------------
// CreateDir creates the directory with the name
// taken as input.
func CreateDir(path string, name string) error {
err := os.MkdirAll(path+string(os.PathSeparator)+name, Permission0775)
return err
}
// CreateFile creates the file with the name
// taken as input.
func CreateFile(path string, name string) error {
err := ioutil.WriteFile(path+string(os.PathSeparator)+name, []byte(""), Permission0755)
return err
}
// WriteFile writes the content string into the file taken as input.
func WriteFile(filename string, content string) error {
err := ioutil.WriteFile(filename, []byte(content), Permission0755)
return err
}