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