-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.go
165 lines (145 loc) · 4.75 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
package main
import (
"flag"
"fmt"
"os"
"path"
"strings"
"ergo.services/tools/ergo/templates"
)
var (
OptionInit listOptions
OptionPath string
OptionWithApp listOptions
OptionWithSup listOptions
OptionWithActor listOptions
OptionWithWeb listOptions
OptionWithTCP listOptions
OptionWithUDP listOptions
OptionWithSaga listOptions
OptionWithStage listOptions
OptionWithPool listOptions
OptionWithRaft listOptions
OptionWithMsg listOptions
OptionWithCloud string
)
func init() {
flag.Var(&OptionInit, "init", "Node name. Available params: ssl, module")
flag.StringVar(&OptionPath, "path", ".", "Set location")
flag.Var(&OptionWithApp, "with-app", "Add Application. The name must be capitalized.")
flag.Var(&OptionWithSup, "with-sup", "Add Supervisor. Available params: type, restart")
flag.Var(&OptionWithActor, "with-actor", "Add actor")
flag.Var(&OptionWithWeb, "with-web", "Add Web-server. Available params: host, port, ssl")
flag.Var(&OptionWithTCP, "with-tcp", "Add TCP-server. Available params: host, port, ssl")
flag.Var(&OptionWithUDP, "with-udp", "Add UDP-server. Available params: host, port")
flag.Var(&OptionWithSaga, "with-saga", "Add Saga")
flag.Var(&OptionWithStage, "with-stage", "Add Stage")
flag.Var(&OptionWithPool, "with-pool", "Add Pool of workers")
flag.Var(&OptionWithRaft, "with-raft", "Add Raft")
flag.Var(&OptionWithMsg, "with-msg", "Add message for the networking")
flag.StringVar(&OptionWithCloud, "with-cloud", "", "Enable cloud with given cluster name")
}
func main() {
flag.Parse()
if len(OptionInit) == 0 {
fmt.Println("error: node name is empty")
return
}
optionNode := OptionInit[0]
optionNode.Package = "main"
optionNode.Templates = templates.Node
if _, exist := optionNode.Params["module"]; exist == false {
optionNode.Params["module"] = optionNode.LoName
}
mod := path.Base(optionNode.Params["module"].(string))
optionNode.Params["module-name"] = mod
dir := path.Join(OptionPath, mod)
optionNode.Dir = dir
list := []*listOptions{
OptionWithActor.WithTemplates(templates.Actor).WithDir(dir),
OptionWithWeb.WithTemplates(templates.Web).WithDir(dir),
OptionWithTCP.WithTemplates(templates.TCP).WithDir(dir),
OptionWithUDP.WithTemplates(templates.UDP).WithDir(dir),
OptionWithSaga.WithTemplates(templates.Saga).WithDir(dir),
OptionWithStage.WithTemplates(templates.Stage).WithDir(dir),
OptionWithPool.WithTemplates(templates.Pool).WithDir(dir),
OptionWithRaft.WithTemplates(templates.Raft).WithDir(dir),
OptionWithSup.WithTemplates(templates.Sup).WithDir(dir),
// must be here due to traversing over the children
// and updating the file location
OptionWithApp.WithTemplates(templates.App).WithDir(dir).WithAppDir("apps"),
}
fmt.Printf("Generating project %q...\n", dir)
for _, l := range list {
for _, option := range *l {
if err := generate(option); err != nil {
fmt.Printf("error: %s\n", err)
return
}
if option.Parent == nil && option.IsApp == false {
// must be started by node
optionNode.Children = append(optionNode.Children, option)
}
}
}
// node options - applications
if len(OptionWithApp) > 0 {
optionNode.Params["applications"] = OptionWithApp
}
// node options - cloud
if OptionWithCloud != "" {
optionNode.Params["cloud"] = OptionWithCloud
}
// register types (messages for networking)
if len(OptionWithMsg) > 0 {
optionType := &Option{
Name: "types",
Dir: dir,
Package: mod,
Templates: templates.Types,
Params: make(map[string]any),
Children: OptionWithMsg,
}
if err := generate(optionType); err != nil {
fmt.Printf("error: %s\n", err)
return
}
// enable RegisterTypes() call
optionNode.Params["types"] = true
}
if err := generate(optionNode); err != nil {
fmt.Printf("error: %s\n", err)
return
}
// README.md
optionReadme := Option{
Name: "README.md",
Dir: dir,
Templates: templates.Readme,
Params: make(map[string]any),
KeepOriginalName: true,
SkipGoFormat: true,
}
optionReadme.Params["applications"] = OptionWithApp
optionReadme.Params["processes"] = optionNode.Children
optionReadme.Params["project"] = optionNode.Name
optionReadme.Params["types"] = OptionWithMsg
optionReadme.Params["optionCloud"] = "false"
if len(OptionWithCloud) > 0 {
optionReadme.Params["optionCloud"] = "true"
}
optionReadme.Params["optionTypes"] = "false"
if len(OptionWithMsg) > 0 {
optionReadme.Params["optionTypes"] = "true"
}
optionReadme.Params["args"] = strings.Join(os.Args, " ")
if err := generate(&optionReadme); err != nil {
fmt.Printf("error: %s\n", err)
return
}
if err := generateGoMod(optionNode); err != nil {
fmt.Printf("error: can not generate go.mod file - %s", err)
return
}
fmt.Println("Successfully completed.")
}