github.com/paweljw/pop/v5@v5.4.6/soda/cmd/generate/model_cmd.go (about)

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