-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
346 lines (309 loc) · 9.29 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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
package main
import (
"database/sql"
"errors"
"fmt"
"time"
"os"
"strings"
prompt "github.com/c-bata/go-prompt"
"github.com/urfave/cli"
"github.com/zainul/gan/internal/app"
app_creator "github.com/zainul/gan/internal/app-creator"
"github.com/zainul/gan/internal/constant"
"github.com/zainul/gan/internal/database"
"github.com/zainul/gan/internal/entity"
"github.com/zainul/gan/internal/io"
"github.com/zainul/gan/internal/log"
)
func completer(d prompt.Document) []prompt.Suggest {
s := []prompt.Suggest{
{Text: constant.Migrate, Description: "Migrate migrations script"},
{Text: constant.Seed, Description: "Seed the data from file"},
{Text: constant.ReverseForSeed, Description: "Reverse table to struct and the added to seeder package"},
{Text: constant.CreateSeed, Description: "Create seed template file"},
{Text: constant.CreateFromFile, Description: "Create migration from SQL file"},
{Text: constant.Create, Description: "Create migration file"},
{Text: constant.CreateApp, Description: "Create starter apps"},
{Text: constant.ReverseDB, Description: "Reverse DB to Entity and Repo"},
}
return prompt.FilterHasPrefix(s, d.GetWordBeforeCursor(), true)
}
func completerEmpty(d prompt.Document) []prompt.Suggest {
s := make([]prompt.Suggest, 0)
return prompt.FilterHasPrefix(s, d.GetWordBeforeCursor(), true)
}
func completerConfig(d prompt.Document) []prompt.Suggest {
s := []prompt.Suggest{
{Text: "migrations/config.json", Description: "By Author"},
}
return prompt.FilterHasPrefix(s, d.GetWordBeforeCursor(), true)
}
func completerConfigCreateApp(d prompt.Document) []prompt.Suggest {
s := make([]prompt.Suggest, 0)
return prompt.FilterHasPrefix(s, d.GetWordBeforeCursor(), true)
}
func main() {
var config string
appCli := cli.NewApp()
appCli.Name = "gan"
appCli.Usage = "gan use for migrate and seed the database"
appCli.Version = "0.0.1"
appCli.Flags = []cli.Flag{
cli.StringFlag{
Name: "config, c",
Usage: "Load configuration from `FILE`",
Destination: &config,
},
}
fmt.Println(
`
--------------------------
______ _______ __ _
| ____ |_____| | \ |
|_____| | | | \_|
--------------------------
`,
)
appCli.Commands = []cli.Command{
{
Name: constant.Migrate,
Usage: "Migrate migrations script",
Action: func(c *cli.Context) error {
if cfg, err := io.OpenConfigFile(config); err != nil {
log.Error(fmt.Sprintf("Error opening file %+v", err))
} else {
mig := app.NewMigration(cfg.Dir, cfg.Conn, cfg.SeedDir)
mig.Migrate(constant.StatusUp)
}
return nil
},
},
{
Name: constant.Seed,
Usage: "Seed the data from file",
Action: func(c *cli.Context) error {
if cfg, err := io.OpenConfigFile(config); err != nil {
log.Error(fmt.Sprintf("Error opening file %+v", err))
} else {
mig := app.NewMigration(cfg.Dir, cfg.Conn, cfg.SeedDir)
mig.Seed()
}
return nil
},
},
{
Name: constant.ReverseForSeed,
Usage: "Reverse table to struct and the added to seeder package",
Action: func(c *cli.Context) error {
if cfg, err := io.OpenConfigFile(config); err != nil {
log.Error(fmt.Sprintf("Error opening file %+v", err))
} else {
reverseSeed(cfg)
}
return nil
},
},
{
Name: constant.CreateSeed,
Usage: "Create seed template file",
Action: func(c *cli.Context) error {
var cfg entity.Config
var err error
if cfg, err = io.OpenConfigFile(config); err != nil {
log.Error(fmt.Sprintf("Error opening file %+v", err))
}
if c.Args().First() == "" {
log.Error("Please provide 1st argument")
return errors.New("argument not completed")
}
seedTemplate(cfg, c.Args().First())
return nil
},
},
{
Name: constant.CreateFromFile,
Usage: "Create migration from SQL file",
Action: func(c *cli.Context) error {
var cfg entity.Config
var err error
if cfg, err = io.OpenConfigFile(config); err != nil {
log.Error(fmt.Sprintf("Error opening file %+v", err))
}
if c.Args().First() == "" {
log.Error("Please provide 1st argument")
return errors.New("argument not completed")
}
mig := app.NewMigration(cfg.Dir, cfg.Conn, cfg.SeedDir)
_ = mig.CreateFile(
fmt.Sprintf("%v_%v_%v",
c.Args().First(),
time.Now().Format("20060102_150405"),
time.Now().UnixNano(),
),
constant.DotGo,
constant.FileTypeMigrationFromFile,
)
log.Info("completed task: ", c.Args().First())
return nil
},
},
{
Name: constant.Create,
Usage: "Create migration file",
Action: func(c *cli.Context) error {
var cfg entity.Config
var err error
if cfg, err = io.OpenConfigFile(config); err != nil {
log.Error(fmt.Sprintf("Error opening file %+v", err))
}
if c.Args().First() == "" {
log.Error("Please provide 1st argument")
return errors.New("argument not completed")
}
migrationFile(cfg, c.Args().First())
return nil
},
},
{
Name: constant.CreateApp,
Usage: "Create Starter Apps service",
Action: func(c *cli.Context) error {
fmt.Println("Hi , I will serve service for you with ♥ ")
return nil
},
},
{
Name: constant.ReverseDB,
Usage: "Reverse DB to Entity Repo",
Action: func(c *cli.Context) error {
fmt.Println("Hi , I will serve you with ♥ ")
return nil
},
},
}
err := appCli.Run(os.Args)
fmt.Println("Please select action that you want")
t := prompt.Input("> ", completer)
t = strings.TrimSpace(t)
config = prompt.Input("Where is the config file stored ?", completerConfig)
var cfg entity.Config
if cfg, err = io.OpenConfigFile(config); err != nil {
log.Error(fmt.Sprintf("Error opening file %+v", err))
}
switch t {
case constant.Migrate:
mig := app.NewMigration(cfg.Dir, cfg.Conn, cfg.SeedDir)
mig.Migrate(constant.StatusUp)
break
case constant.Seed:
mig := app.NewMigration(cfg.Dir, cfg.Conn, cfg.SeedDir)
mig.Seed()
break
case constant.ReverseForSeed:
reverseSeed(cfg)
break
case constant.CreateSeed:
var entity string
for {
entity = prompt.Input("What name for entity that you want to seed ?", completerEmpty)
if strings.TrimSpace(entity) != "" {
break
}
}
if cfg, err := io.OpenConfigFile(config); err != nil {
log.Error(fmt.Sprintf("Error opening file %+v", err))
} else {
seedTemplate(cfg, entity)
}
break
case constant.CreateFromFile:
var entity string
for {
entity = prompt.Input("What name of migration ?", completerEmpty)
if strings.TrimSpace(entity) != "" {
break
}
}
if cfg, err := io.OpenConfigFile(config); err != nil {
log.Error(fmt.Sprintf("Error opening file %+v", err))
} else {
mig := app.NewMigration(cfg.Dir, cfg.Conn, cfg.SeedDir)
mig.Migrate(constant.StatusUp)
mig.CreateFile(
fmt.Sprintf("%v_%v_%v",
entity,
time.Now().Format("20060102_150405"),
time.Now().UnixNano(),
),
constant.DotGo,
constant.FileTypeMigrationFromFile,
)
}
break
case constant.Create:
var entity string
for {
entity = prompt.Input("What name of migration ?", completerEmpty)
if strings.TrimSpace(entity) != "" {
break
}
}
if cfg, err := io.OpenConfigFile(config); err != nil {
log.Error(fmt.Sprintf("Error opening file %+v", err))
} else {
migrationFile(cfg, entity)
}
break
case constant.CreateApp:
name := prompt.Input("What is your service name ? (ex: danisa) ", completerConfigCreateApp)
packageName := prompt.Input("What your root of your package you want ? (ex: github.com/zainul) ", completerConfigCreateApp)
yourPath := prompt.Input(fmt.Sprintf("Your GOPATH ? ex : /home/zainul/go"), completerConfigCreateApp)
fmt.Println("Hi , I will serve service " + name + " for you with ♥ ")
c := app_creator.Creator{
PackageURL: cfg.CreatorApp.PackageURL,
StarterProjectName: cfg.CreatorApp.StarterProjectName,
StarterPackageName: cfg.CreatorApp.StarterPackageName,
ProjectName: name,
Package: packageName,
Path: yourPath,
}
err := c.CopyFindAndReplace()
if err != nil {
log.Error("Error when creating app :( ", err)
}
break
case constant.ReverseDB:
var (
cfg entity.Config
err error
)
if cfg, err = io.OpenConfigFile(config); err != nil {
log.Error(fmt.Sprintf("Error opening file %+v", err))
return
}
mig := app.NewMigration(cfg.Dir, cfg.Conn, cfg.SeedDir, cfg.ProjectStructure, cfg.ProjectPackage)
if cfg.ProjectStructure != (entity.ProjectStructure{}) {
conn, err := sql.Open("postgres", cfg.Conn)
db := database.NewDB(conn)
structs, err := db.GetEntityWithoutTableName()
if err != nil {
log.Error(err)
os.Exit(2)
}
for _, strc := range structs {
mig.CreateFile(strc.TableName, constant.DotGo, constant.FileTypeReverse, strc.Models, constant.CreateEntity, strc.TableName)
}
for _, strc := range structs {
mig.CreateFile(strc.TableName, constant.DotGo, constant.FileTypeReverse, strc.Models, constant.CreateUseCase, strc.TableName)
}
for _, strc := range structs {
mig.CreateFile(strc.TableName, constant.DotGo, constant.FileTypeReverse, strc.Models, constant.CreateStore, strc.TableName)
}
for _, strc := range structs {
mig.CreateFile(strc.TableName, constant.DotGo, constant.FileTypeReverse, strc.Models, constant.CreateStoreImpl, strc.TableName)
}
}
break
}
}