github.com/solongordon/pop@v4.10.0+incompatible/soda/cmd/generate/model_cmd.go (about) 1 package generate 2 3 import ( 4 "strings" 5 6 "github.com/pkg/errors" 7 "github.com/spf13/cobra" 8 ) 9 10 var modelCmdConfig struct { 11 SkipMigration bool 12 StructTag string 13 MigrationType string 14 ModelPath string 15 } 16 17 func init() { 18 ModelCmd.Flags().StringVarP(&modelCmdConfig.StructTag, "struct-tag", "", "json", "sets the struct tags for model (xml or json)") 19 ModelCmd.Flags().StringVarP(&modelCmdConfig.MigrationType, "migration-type", "", "fizz", "sets the type of migration files for model (sql or fizz)") 20 ModelCmd.Flags().BoolVarP(&modelCmdConfig.SkipMigration, "skip-migration", "s", false, "Skip creating a new fizz migration for this model.") 21 ModelCmd.Flags().StringVarP(&modelCmdConfig.ModelPath, "models-path", "", "models", "the path the model will be created in") 22 } 23 24 // ModelCmd is the cmd to generate a model 25 var ModelCmd = &cobra.Command{ 26 Use: "model [name]", 27 Aliases: []string{"m"}, 28 Short: "Generates a model for your database", 29 RunE: func(cmd *cobra.Command, args []string) error { 30 if len(args) == 0 { 31 return errors.New("you must supply a name for your model") 32 } 33 34 p := cmd.Flag("path") 35 e := cmd.Flag("env") 36 data := map[string]interface{}{ 37 "skipMigration": modelCmdConfig.SkipMigration, 38 "marshalType": modelCmdConfig.StructTag, 39 "migrationType": modelCmdConfig.MigrationType, 40 "modelPath": modelCmdConfig.ModelPath, 41 "path": p.Value.String(), 42 "env": e.Value.String(), 43 } 44 return Model(args[0], data, args[1:]) 45 }, 46 } 47 48 // Model generates new model files to work with pop. 49 func Model(name string, opts map[string]interface{}, attributes []string) error { 50 if strings.TrimSpace(name) == "" { 51 return errors.New("model name can't be empty") 52 } 53 mt, found := opts["marshalType"].(string) 54 if !found { 55 return errors.New("marshalType option is required") 56 } 57 58 pp, found := opts["modelPath"].(string) 59 if !found { 60 return errors.New("modelPath option is required") 61 } 62 63 model, err := newModel(name, mt, pp) 64 if err != nil { 65 return errors.WithStack(err) 66 } 67 68 for _, def := range attributes { 69 a, err := newAttribute(def, &model) 70 if err != nil { 71 return err 72 } 73 if err := model.addAttribute(a); err != nil { 74 return err 75 } 76 } 77 78 // Add a default UUID, if no custom ID is provided 79 model.addID() 80 81 if err := model.generateModelFile(); err != nil { 82 return err 83 } 84 85 sm, found := opts["skipMigration"].(bool) 86 if found && sm { 87 return nil 88 } 89 90 p, found := opts["path"].(string) 91 if !found { 92 return errors.New("path option is required") 93 } 94 95 migrationT, found := opts["migrationType"].(string) 96 if !found { 97 return errors.New("migrationType option is required") 98 } 99 switch migrationT { 100 case "sql": 101 env, found := opts["env"].(string) 102 if !found { 103 return errors.New("env option is required") 104 } 105 err = model.generateSQL(p, env) 106 default: 107 err = model.generateFizz(p) 108 } 109 return err 110 }