-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.go
318 lines (265 loc) · 7.26 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
package main
import (
"bytes"
"errors"
"fmt"
"github.com/unity26org/wheel/commons/notify"
"github.com/unity26org/wheel/generator"
"github.com/unity26org/wheel/help"
"github.com/unity26org/wheel/version"
"gopkg.in/yaml.v2"
"io/ioutil"
"os"
"os/exec"
"regexp"
"strings"
)
func IsGoInstalled() bool {
var out bytes.Buffer
cmd := exec.Command("go", "version")
cmd.Stdout = &out
err := cmd.Run()
return err == nil
}
func CheckDependences(database string) error {
var out bytes.Buffer
var hasDependence bool
notify.Simpleln("Checking dependences...")
cmd := exec.Command("go", "list", "...")
cmd.Stdout = &out
cmd.Run()
// if err := cmd.Run(); err != nil {
// fmt.Println("error:", err)
// }
installedDependences := strings.Split(out.String(), "\n")
requiredDependences := []string{"github.com/jinzhu/gorm", "gopkg.in/yaml.v2", "github.com/gorilla/mux", "github.com/dgrijalva/jwt-go", "github.com/satori/go.uuid", "golang.org/x/crypto/bcrypt", "github.com/adilsonchacon/sargo"}
if database == "postgres" {
requiredDependences = append(requiredDependences, "github.com/lib/pq")
} else if database == "mysql" {
requiredDependences = append(requiredDependences, "github.com/go-sql-driver/mysql")
}
for _, requiredDependence := range requiredDependences {
hasDependence = false
for _, installedDependence := range installedDependences {
hasDependence = (requiredDependence == installedDependence)
if hasDependence {
break
}
}
if !hasDependence {
notify.Simple(fmt.Sprintf(" package %s was not found, installing...", requiredDependence))
cmd := exec.Command("go", "get", requiredDependence)
cmd.Stdout = &out
err := cmd.Run()
if err != nil {
return err
}
notify.Simpleln(fmt.Sprintf(" package %s was successfully installed", requiredDependence))
} else {
notify.Simpleln(fmt.Sprintf(" package %s was found", requiredDependence))
}
}
return nil
}
func checkGitIgnore(args []string) bool {
b := false
regexpSkipGit := regexp.MustCompile(`\A(\-G)|(\-\-skip\-git)\z`)
for index, value := range args {
if index > 2 && regexpSkipGit.MatchString(value) {
b = true
break
}
}
return b
}
func checkDatabase(args []string) (string, error) {
database := ""
getNext := false
regexpDatabase := regexp.MustCompile(`\A(\-d)|(\-\-database)`)
regexpDatabaseAndEqual := regexp.MustCompile(`\A(\-d\=)|(\-\-database\=)`)
regexpDbAvailable := regexp.MustCompile(`(?i)\A(postgres)|(mysql)\z`)
for index, value := range args {
if index > 2 {
if getNext {
database = value
break
} else if regexpDatabaseAndEqual.MatchString(value) {
database = regexpDatabaseAndEqual.ReplaceAllString(value, "")
break
} else if regexpDatabase.MatchString(value) {
getNext = true
}
}
}
fmt.Println("Database:", database)
if !regexpDbAvailable.MatchString(database) {
return "", errors.New("invalid option. Run \"wheel --help\" for details")
} else {
return database, nil
}
}
func handleNewApp(args []string) error {
var options = make(map[string]interface{})
database, err := checkDatabase(args)
if err != nil {
return err
}
err = checkIsGoInstalled(database)
if err != nil {
return err
}
preOptions := strings.Split(os.Args[2], "/")
options["app_name"] = preOptions[len(preOptions)-1]
options["app_repository"] = os.Args[2]
options["git_ignore"] = checkGitIgnore(os.Args)
options["database"] = database
notify.Simpleln("Generating new app...")
return generator.NewApp(options)
}
func isResourceNameValid(name string) bool {
var regexpInvalidChar = regexp.MustCompile(`[^\w]`)
return !regexpInvalidChar.MatchString(name)
}
func buildGenerateOptions(args []string) (map[string]bool, error) {
var options = make(map[string]bool)
var subject string
var err error
if len(args) > 2 {
subject = args[2]
} else {
subject = "invalid_subject"
}
options["model"] = false
options["entity"] = false
options["view"] = false
options["handler"] = false
options["routes"] = false
options["migrate"] = false
options["authorize"] = false
switch subject {
case "scaffold":
options["model"] = true
options["entity"] = true
options["view"] = true
options["handler"] = true
options["routes"] = true
options["migrate"] = true
options["authorize"] = true
if len(args) < 4 || !isResourceNameValid(args[3]) {
err = errors.New("invalid scaffold name")
}
case "model":
options["model"] = true
options["entity"] = true
options["migrate"] = true
if len(args) < 4 || !isResourceNameValid(args[3]) {
err = errors.New("invalid model name")
}
case "handler":
options["handler"] = true
options["routes"] = true
options["authorize"] = true
if len(args) < 4 || !isResourceNameValid(args[3]) {
err = errors.New("invalid handler name")
}
case "entity":
options["entity"] = true
options["migrate"] = true
if len(args) < 4 || !isResourceNameValid(args[3]) {
err = errors.New("invalid entity name")
}
case "migration":
// wheel g migration add_total_to_users total:integer
// wheel g migration remove_total_from_users
options["migrate"] = true
if len(args) < 4 || !isResourceNameValid(args[3]) {
err = errors.New("invalid migration name")
}
default:
err = errors.New("invalid generate subject. Run \"wheel --help\" for details")
}
return options, err
}
func handleGenerateNewCrud(args []string, options map[string]bool) error {
var columns []string
for index, value := range args {
if index <= 3 {
continue
} else {
columns = append(columns, value)
}
}
notify.Simpleln("Generating new CRUD...")
return generator.NewCrud(args[3], columns, options)
}
func handleGenerate(args []string) error {
var options map[string]bool
config, err := loadDatabaseConfigFile()
if err != nil {
return err
}
err = checkIsGoInstalled(config["adapter"])
if err != nil {
return err
}
options, err = buildGenerateOptions(args)
if err != nil {
return err
}
return handleGenerateNewCrud(args, options)
}
func handleHelp() {
notify.Simpleln(help.Content)
}
func handleVersion() {
notify.Simpleln(version.Content)
}
func checkIsGoInstalled(database string) error {
if !IsGoInstalled() {
return errors.New("\"Go\" seems not installed")
} else {
notify.Simpleln("\"Go\" seems installed")
return CheckDependences(database)
}
}
func loadDatabaseConfigFile() (map[string]string, error) {
config := make(map[string]string)
data, err := readDatabaseConfigFile()
if err != nil {
return nil, err
}
err = yaml.Unmarshal(data, &config)
if err != nil {
return nil, err
}
return config, nil
}
func readDatabaseConfigFile() ([]byte, error) {
data, err := ioutil.ReadFile("./config/database.yml")
if err != nil {
return []byte{}, err
}
return data, nil
}
func main() {
command := ""
if len(os.Args) >= 2 {
command = os.Args[1]
}
if command == "new" || command == "n" {
if err := handleNewApp(os.Args); err != nil {
notify.Error(err)
}
} else if command == "generate" || command == "g" {
if err := handleGenerate(os.Args); err != nil {
notify.Error(err)
}
} else if command == "--help" || command == "-h" {
handleHelp()
} else if command == "--version" || command == "-v" {
handleVersion()
} else {
notify.ErrorJustified("invalid argument. Use \"new\" or \"generate\". Run \"wheel --help\" for details", 0)
handleHelp()
}
}