-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
243 lines (201 loc) · 5.48 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
package main
import (
"errors"
"flag"
"fmt"
"os"
"path"
"regexp"
"strconv"
"strings"
packr "github.com/gobuffalo/packr/v2"
)
func createStructure(app *application) {
s := structure{
Box: packr.New("tplBox", "./templates"),
App: app,
Files: map[string]string{
fmt.Sprintf("%s%s", app.Path, "go.mod"): "go.mod.tmpl",
fmt.Sprintf("%s%s", app.Path, "Makefile"): "Makefile.tmpl",
},
Directories: make([]*dir, 0),
}
sep := string(os.PathSeparator)
s.Directories = append(s.Directories, &dir{
Name: fmt.Sprintf("%s%s%s%s", app.Path, "cmd", sep, app),
Files: map[string]string{
fmt.Sprintf("%s%s%s%s%s%s", app.Path, "cmd", sep, app, sep, fmt.Sprintf("%s.go", app)): "app.go.tmpl",
},
})
s.Directories = append(s.Directories, &dir{
Name: fmt.Sprintf("%s%s", app.Path, "config"),
Files: map[string]string{
fmt.Sprintf("%s%s%s%s", app.Path, "config", sep, "app.local.yml"): "config.yml.tmpl",
fmt.Sprintf("%s%s%s%s", app.Path, "config", sep, "app.dev.yml"): "config.yml.tmpl",
fmt.Sprintf("%s%s%s%s", app.Path, "config", sep, "app.yml"): "config.yml.tmpl",
},
})
s.Directories = append(s.Directories, &dir{
Name: fmt.Sprintf("%s%s", app.Path, "deploy"),
Files: map[string]string{
fmt.Sprintf("%s%s%s%s", app.Path, "deploy", sep, "Dockerfile"): "Dockerfile.tmpl",
fmt.Sprintf("%s%s%s%s", app.Path, "deploy", sep, "docker-compose.yml"): "docker-compose.yml.tmpl",
},
})
s.Directories = append(s.Directories, &dir{
Name: fmt.Sprintf("%s%s", app.Path, "internal"),
})
s.Directories = append(s.Directories, &dir{
Name: fmt.Sprintf("%s%s", app.Path, "vendor"),
})
s.Directories = append(s.Directories, &dir{
Name: fmt.Sprintf("%s%s", app.Path, "storage"),
})
s.Directories = append(s.Directories, &dir{
Name: fmt.Sprintf("%s%s%s%s", app.Path, "server", sep, "handlers"),
Files: map[string]string{
fmt.Sprintf("%s%s%s%s%s%s", app.Path, "server", sep, "handlers", sep, "ping.go"): "ping.go.tmpl",
fmt.Sprintf("%s%s%s%s%s%s", app.Path, "server", sep, "handlers", sep, "hello.go"): "hello.go.tmpl",
},
})
s.Directories = append(s.Directories, &dir{
Name: fmt.Sprintf("%s%s%s%s", app.Path, "server", sep, "middleware"),
Files: map[string]string{
fmt.Sprintf("%s%s%s%s%s%s", app.Path, "server", sep, "middleware", sep, "log.go"): "log.go.tmpl",
},
})
s.create()
}
func main() {
var pathToApp string
flag.StringVar(&pathToApp, "path", "", "Specify absolute path to app")
flag.Parse()
if len(pathToApp) == 0 {
printErr("--path is required")
os.Exit(1)
}
if !path.IsAbs(pathToApp) {
printErr("--path should be absolute")
os.Exit(1)
}
pathToApp = path.Clean(pathToApp)
stat, err := os.Stat(pathToApp)
if err != nil && !os.IsNotExist(err) {
printErr(err.Error())
os.Exit(1)
} else if os.IsNotExist(err) {
err := userAction(&action{
Question: fmt.Sprintf("Dir %s doesn't exist. Create? [y/N]:", pathToApp),
Validate: func(answer string) error {
a := strings.ToLower(answer)
if a != "y" && a != "n" {
return errors.New("Invalid option. Only [y/N] available:")
}
return nil
},
Action: func(answer string) error {
if strings.ToLower(answer) == "n" {
return errors.New("Can't continue without app dir")
}
return os.Mkdir(pathToApp, permMode)
},
})
if err != nil {
printErr(err.Error())
os.Exit(0)
}
printSuccess(fmt.Sprintf("Created %s", pathToApp))
stat, err = os.Stat(pathToApp)
if err != nil {
printErr(err.Error())
os.Exit(1)
}
}
if !stat.IsDir() {
printErr("--path should be a directory")
os.Exit(1)
}
app := new(application)
app.Path = fmt.Sprintf("%s%s", pathToApp, string(os.PathSeparator))
err = userAction(&action{
Question: "Enter app name [a-z0-9_]:",
Validate: func(answer string) error {
err := errors.New("App name should be in lower snake case [a-z0-9_]")
if len(answer) == 0 {
return err
}
r := regexp.MustCompile("^[a-z0-9_]*$")
if !r.MatchString(answer) {
return err
}
return nil
},
Action: func(answer string) error {
app.Name = answer
return nil
},
})
if err != nil {
printErr(err.Error())
os.Exit(1)
}
err = userAction(&action{
Question: "Select logger:\r\n[1]: github.com/Sirupsen/logrus\r\n[2]: github.com/uber-go/zap",
Validate: func(answer string) error {
i, err := strconv.Atoi(answer)
if err != nil {
return err
}
if i != 1 && i != 2 {
return errors.New("Invalid choice")
}
return nil
},
Action: func(answer string) error {
i, err := strconv.Atoi(answer)
if err != nil {
return err
}
switch i {
case 1:
app.Logger = "logrus"
app.LoggerPackage = "github.com/sirupsen/logrus"
case 2:
app.Logger = "zap"
app.LoggerPackage = "go.uber.org/zap"
}
return nil
},
})
err = userAction(&action{
Question: "Select router:\r\n[1]: github.com/gorilla/mux\r\n[2]: github.com/go-chi/chi",
Validate: func(answer string) error {
i, err := strconv.Atoi(answer)
if err != nil {
return err
}
if i != 1 && i != 2 {
return errors.New("Invalid choice")
}
return nil
},
Action: func(answer string) error {
i, err := strconv.Atoi(answer)
if err != nil {
return err
}
switch i {
case 1:
app.Router = "mux"
app.RouterPackage = "github.com/gorilla/mux"
case 2:
app.Router = "chi"
app.RouterPackage = "github.com/go-chi/chi"
}
return nil
},
})
printMsg(fmt.Sprintf("Creating %s application..", app))
createStructure(app)
printSuccess("Success!")
}