This repository has been archived by the owner on May 26, 2022. It is now read-only.
forked from gobuffalo/pop
/
model_cmd.go
111 lines (95 loc) · 2.77 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
package generate
import (
"strings"
"errors"
"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 or json)")
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 {
if len(args) == 0 {
return errors.New("you must supply a name for your model")
}
p := cmd.Flag("path")
e := cmd.Flag("env")
data := map[string]interface{}{
"skipMigration": modelCmdConfig.SkipMigration,
"marshalType": modelCmdConfig.StructTag,
"migrationType": modelCmdConfig.MigrationType,
"modelPath": modelCmdConfig.ModelPath,
"path": p.Value.String(),
"env": e.Value.String(),
}
return Model(args[0], data, args[1:])
},
}
// Model generates new model files to work with pop.
func Model(name string, opts map[string]interface{}, attributes []string) error {
if strings.TrimSpace(name) == "" {
return errors.New("model name can't be empty")
}
mt, found := opts["marshalType"].(string)
if !found {
return errors.New("marshalType option is required")
}
pp, found := opts["modelPath"].(string)
if !found {
return errors.New("modelPath option is required")
}
model, err := newModel(name, mt, pp)
if err != nil {
return err
}
for _, def := range attributes {
a, err := newAttribute(def, &model)
if err != nil {
return err
}
if err := model.addAttribute(a); err != nil {
return err
}
}
// Add a default UUID, if no custom ID is provided
model.addID()
if err := model.generateModelFile(); err != nil {
return err
}
sm, found := opts["skipMigration"].(bool)
if found && sm {
return nil
}
p, found := opts["path"].(string)
if !found {
return errors.New("path option is required")
}
migrationT, found := opts["migrationType"].(string)
if !found {
return errors.New("migrationType option is required")
}
switch migrationT {
case "sql":
env, found := opts["env"].(string)
if !found {
return errors.New("env option is required")
}
err = model.generateSQL(p, env)
default:
err = model.generateFizz(p)
}
return err
}