-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
93 lines (89 loc) · 2.31 KB
/
main.go
File metadata and controls
93 lines (89 loc) · 2.31 KB
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
package main
import (
"errors"
"flag"
"fmt"
"os"
"strings"
"github.com/aliforever/gomondel/utils"
"github.com/aliforever/gomondel/funcs"
"github.com/aliforever/gomondel/templates"
)
func main() {
var init, model, modelParent, path, fields string
flag.StringVar(&init, "init", "", "--init=database_name")
flag.StringVar(&path, "path", "", "--path=/home/go/src/project_name")
flag.StringVar(&model, "model", "", "--model=model_name[,int]")
flag.StringVar(&fields, "fields", "", "--fields=username,string-password,string")
flag.StringVar(&modelParent, "parent", "", "--parent=parent_model_name[,int]")
flag.Parse()
if init != "" && model != "" {
fmt.Println("You can't run init and model at the same time")
return
}
var err error
if path == "" {
path, err = utils.CurrentPath()
if err != nil {
fmt.Println("invalid path", path)
return
}
} else {
_, err = os.Stat(path)
if err != nil {
if os.IsNotExist(err) {
err = errors.New(fmt.Sprintf("Path %s does not exists", path))
}
return
}
}
if init != "" {
dbPath, err := funcs.InitDatabase(path, init)
if err != nil {
fmt.Println(err)
return
}
fmt.Println(fmt.Sprintf("Database %s init file added in %s, don't forget to call InitMongoDB() in your main function", init, dbPath))
return
}
if model != "" {
var parent *string
var keyType *string
var parentIdType *string
split := strings.Split(model, ",")
if len(split) == 2 {
keyType = &split[1]
}
model = split[0]
if modelParent != "" {
split := strings.Split(modelParent, ",")
if len(split) == 2 {
parentIdType = &split[1]
}
parent = &split[0]
}
var modelFields []templates.ModelField
if fields != "" {
splitFields := strings.Split(fields, "-")
m := map[string]string{}
for _, field := range splitFields {
splitFieldType := strings.Split(field, ",")
if len(splitFieldType) != 2 {
fmt.Println("invalid fields argument")
return
}
fieldName := splitFieldType[0]
fieldType := splitFieldType[1]
m[fieldName] = fieldType
}
modelFields = funcs.MakeModelFieldsFromMap(m)
}
modelPath, err := funcs.CreateModel(path, model, keyType, parent, parentIdType, modelFields)
if err != nil {
fmt.Println(err)
return
}
fmt.Println(fmt.Sprintf("Model %s file added in %s", model, modelPath))
return
}
}