github.com/dkishere/pop@v4.13.1+incompatible/soda/cmd/generate/fizz_cmd.go (about)

     1  package generate
     2  
     3  import (
     4  	"context"
     5  
     6  	"github.com/gobuffalo/attrs"
     7  	"github.com/gobuffalo/genny"
     8  	"github.com/gobuffalo/logger"
     9  	"github.com/gobuffalo/pop/genny/fizz/cempty"
    10  	"github.com/gobuffalo/pop/genny/fizz/ctable"
    11  	"github.com/spf13/cobra"
    12  )
    13  
    14  // FizzCmd generates a new fizz migration
    15  var FizzCmd = &cobra.Command{
    16  	Use:     "fizz [name]",
    17  	Aliases: []string{"migration"},
    18  	Short:   "Generates Up/Down migrations for your database using fizz.",
    19  	RunE: func(cmd *cobra.Command, args []string) error {
    20  		name := ""
    21  		if len(args) > 0 {
    22  			name = args[0]
    23  		}
    24  
    25  		var (
    26  			atts attrs.Attrs
    27  			err  error
    28  		)
    29  		if len(args) > 1 {
    30  			atts, err = attrs.ParseArgs(args[1:]...)
    31  			if err != nil {
    32  				return err
    33  			}
    34  		}
    35  
    36  		run := genny.WetRunner(context.Background())
    37  
    38  		// Ensure the generator is as verbose as the old one.
    39  		lg := logger.New(logger.DebugLevel)
    40  		run.Logger = lg
    41  
    42  		p := cmd.Flag("path")
    43  		path := ""
    44  		if p != nil {
    45  			path = p.Value.String()
    46  		}
    47  
    48  		if len(atts) == 0 {
    49  			g, err := cempty.New(&cempty.Options{
    50  				TableName: name,
    51  				Path:      path,
    52  				Type:      "fizz",
    53  			})
    54  			if err != nil {
    55  				return err
    56  			}
    57  			run.With(g)
    58  		} else {
    59  			g, err := ctable.New(&ctable.Options{
    60  				TableName: name,
    61  				Path:      path,
    62  				Type:      "fizz",
    63  				Attrs:     atts,
    64  			})
    65  			if err != nil {
    66  				return err
    67  			}
    68  			run.With(g)
    69  		}
    70  
    71  		return run.Run()
    72  	},
    73  }