/
model_cmd.go
124 lines (110 loc) · 3.07 KB
/
model_cmd.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
package generate
import (
"context"
"os"
"os/exec"
"github.com/gobuffalo/attrs"
"github.com/gobuffalo/fizz"
"github.com/gobuffalo/genny/v2"
"github.com/gobuffalo/genny/v2/gogen"
"github.com/gobuffalo/logger"
"github.com/gobuffalo/pop/v6"
"github.com/gobuffalo/pop/v6/genny/fizz/ctable"
gmodel "github.com/gobuffalo/pop/v6/genny/model"
"github.com/spf13/cobra"
)
var modelCmdConfig struct {
SkipMigration bool
StructTag string
MigrationType string
ModelPath string
}
func init() {
ModelCmd.Flags().StringVarP(&modelCmdConfig.StructTag, "struct-tag", "", "json", "sets the struct tags for model (xml/json/jsonapi)")
ModelCmd.Flags().StringVarP(&modelCmdConfig.MigrationType, "migration-type", "", "fizz", "sets the type of migration files for model (sql or fizz)")
ModelCmd.Flags().BoolVarP(&modelCmdConfig.SkipMigration, "skip-migration", "s", false, "Skip creating a new fizz migration for this model.")
ModelCmd.Flags().StringVarP(&modelCmdConfig.ModelPath, "models-path", "", "models", "the path the model will be created in")
}
// ModelCmd is the cmd to generate a model
var ModelCmd = &cobra.Command{
Use: "model [name]",
Aliases: []string{"m"},
Short: "Generates a model for your database",
RunE: func(cmd *cobra.Command, args []string) error {
name := ""
if len(args) > 0 {
name = args[0]
}
var (
atts attrs.Attrs
err error
)
if len(args) > 1 {
atts, err = attrs.ParseArgs(args[1:]...)
if err != nil {
return err
}
}
run := genny.WetRunner(context.Background())
// Ensure the generator is as verbose as the old one.
lg := logger.New(logger.DebugLevel)
run.Logger = lg
// Mount models generator
g, err := gmodel.New(&gmodel.Options{
Name: name,
Attrs: atts,
Path: modelCmdConfig.ModelPath,
Encoding: modelCmdConfig.StructTag,
ForceDefaultID: true,
ForceDefaultTimestamps: true,
})
if err != nil {
return err
}
run.With(g)
// format generated go files
pwd, _ := os.Getwd()
g, err = gogen.Fmt(pwd)
if err != nil {
return err
}
run.With(g)
// generated modules may have new dependencies
if _, err := os.Stat("go.mod"); err == nil {
g = genny.New()
g.Command(exec.Command("go", "mod", "tidy"))
run.With(g)
}
// Mount migrations generator
if !modelCmdConfig.SkipMigration {
p := cmd.Flag("path")
path := ""
if p != nil {
path = p.Value.String()
}
e := cmd.Flag("env")
var translator fizz.Translator
if modelCmdConfig.MigrationType == "sql" {
db, err := pop.Connect(e.Value.String())
if err != nil {
return err
}
translator = db.Dialect.FizzTranslator()
}
g, err = ctable.New(&ctable.Options{
TableName: name,
Attrs: atts,
Path: path,
Type: modelCmdConfig.MigrationType,
Translator: translator,
ForceDefaultID: true,
ForceDefaultTimestamps: true,
})
if err != nil {
return err
}
run.With(g)
}
return run.Run()
},
}