github.com/friesencr/pop/v6@v6.1.6/soda/cmd/generate/model_cmd.go (about) 1 package generate 2 3 import ( 4 "context" 5 "os" 6 "os/exec" 7 8 "github.com/friesencr/pop/v6" 9 "github.com/friesencr/pop/v6/genny/fizz/ctable" 10 gmodel "github.com/friesencr/pop/v6/genny/model" 11 "github.com/gobuffalo/attrs" 12 "github.com/gobuffalo/fizz" 13 "github.com/gobuffalo/genny/v2" 14 "github.com/gobuffalo/genny/v2/gogen" 15 "github.com/gobuffalo/logger" 16 "github.com/spf13/cobra" 17 ) 18 19 var modelCmdConfig struct { 20 SkipMigration bool 21 StructTag string 22 MigrationType string 23 ModelPath string 24 } 25 26 func init() { 27 ModelCmd.Flags().StringVarP(&modelCmdConfig.StructTag, "struct-tag", "", "json", "sets the struct tags for model (xml/json/jsonapi)") 28 ModelCmd.Flags().StringVarP(&modelCmdConfig.MigrationType, "migration-type", "", "fizz", "sets the type of migration files for model (sql or fizz)") 29 ModelCmd.Flags().BoolVarP(&modelCmdConfig.SkipMigration, "skip-migration", "s", false, "Skip creating a new fizz migration for this model.") 30 ModelCmd.Flags().StringVarP(&modelCmdConfig.ModelPath, "models-path", "", "models", "the path the model will be created in") 31 } 32 33 // ModelCmd is the cmd to generate a model 34 var ModelCmd = &cobra.Command{ 35 Use: "model [name]", 36 Aliases: []string{"m"}, 37 Short: "Generates a model for your database", 38 RunE: func(cmd *cobra.Command, args []string) error { 39 name := "" 40 if len(args) > 0 { 41 name = args[0] 42 } 43 44 var ( 45 atts attrs.Attrs 46 err error 47 ) 48 if len(args) > 1 { 49 atts, err = attrs.ParseArgs(args[1:]...) 50 if err != nil { 51 return err 52 } 53 } 54 55 run := genny.WetRunner(context.Background()) 56 57 // Ensure the generator is as verbose as the old one. 58 lg := logger.New(logger.DebugLevel) 59 run.Logger = lg 60 61 // Mount models generator 62 g, err := gmodel.New(&gmodel.Options{ 63 Name: name, 64 Attrs: atts, 65 Path: modelCmdConfig.ModelPath, 66 Encoding: modelCmdConfig.StructTag, 67 ForceDefaultID: true, 68 ForceDefaultTimestamps: true, 69 }) 70 if err != nil { 71 return err 72 } 73 run.With(g) 74 75 // format generated go files 76 pwd, _ := os.Getwd() 77 g, err = gogen.Fmt(pwd) 78 if err != nil { 79 return err 80 } 81 run.With(g) 82 83 // generated modules may have new dependencies 84 if _, err := os.Stat("go.mod"); err == nil { 85 g = genny.New() 86 g.Command(exec.Command("go", "mod", "tidy")) 87 run.With(g) 88 } 89 90 // Mount migrations generator 91 if !modelCmdConfig.SkipMigration { 92 p := cmd.Flag("path") 93 path := "" 94 if p != nil { 95 path = p.Value.String() 96 } 97 e := cmd.Flag("env") 98 var translator fizz.Translator 99 if modelCmdConfig.MigrationType == "sql" { 100 db, err := pop.Connect(e.Value.String()) 101 if err != nil { 102 return err 103 } 104 translator = db.Dialect.FizzTranslator() 105 } 106 107 g, err = ctable.New(&ctable.Options{ 108 TableName: name, 109 Attrs: atts, 110 Path: path, 111 Type: modelCmdConfig.MigrationType, 112 Translator: translator, 113 ForceDefaultID: true, 114 ForceDefaultTimestamps: true, 115 }) 116 if err != nil { 117 return err 118 } 119 run.With(g) 120 } 121 122 return run.Run() 123 }, 124 }