generate

package
v6.1.6 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 19, 2023 License: MIT Imports: 19 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ConfigCmd = &cobra.Command{
	Use:              "config",
	Short:            "Generates a database.yml file for your project.",
	PersistentPreRun: func(c *cobra.Command, args []string) {},
	RunE: func(cmd *cobra.Command, args []string) error {
		cflag := cmd.Flag("config")
		cflagVal := ""
		if cflag != nil {
			cflagVal = cflag.Value.String()
		}
		cfgFile := defaults.String(cflagVal, "database.yml")

		run := genny.WetRunner(context.Background())

		pwd, _ := os.Getwd()
		g, err := config.New(&config.Options{
			Root:     pwd,
			Prefix:   filepath.Base(pwd),
			FileName: cfgFile,
			Dialect:  dialect,
		})
		if err != nil {
			return err
		}
		run.With(g)

		return run.Run()
	},
}

ConfigCmd is the command to generate pop config files

View Source
var FizzCmd = &cobra.Command{
	Use:     "fizz [name]",
	Aliases: []string{"migration"},
	Short:   "Generates Up/Down migrations for your database using fizz.",
	RunE: func(cmd *cobra.Command, args []string) error {
		name := ""
		if len(args) > 0 {
			name = args[0]
		}

		var (
			atts attrs.Attrs
			err  error
		)
		if len(args) > 1 {
			atts, err = attrs.ParseArgs(args[1:]...)
			if err != nil {
				return err
			}
		}

		run := genny.WetRunner(context.Background())

		lg := logger.New(logger.DebugLevel)
		run.Logger = lg

		p := cmd.Flag("path")
		path := ""
		if p != nil {
			path = p.Value.String()
		}

		if len(atts) == 0 {
			g, err := cempty.New(&cempty.Options{
				Name: name,
				Path: path,
				Type: "fizz",
			})
			if err != nil {
				return err
			}
			run.With(g)
		} else {
			g, err := ctable.New(&ctable.Options{
				TableName: name,
				Path:      path,
				Type:      "fizz",
				Attrs:     atts,
			})
			if err != nil {
				return err
			}
			run.With(g)
		}

		return run.Run()
	},
}

FizzCmd generates a new fizz migration

View Source
var ModelCmd = &cobra.Command{
	Use:     "model [name]",
	Aliases: []string{"m"},
	Short:   "Generates a model for your database",
	RunE: func(cmd *cobra.Command, args []string) error {
		name := ""
		if len(args) > 0 {
			name = args[0]
		}

		var (
			atts attrs.Attrs
			err  error
		)
		if len(args) > 1 {
			atts, err = attrs.ParseArgs(args[1:]...)
			if err != nil {
				return err
			}
		}

		run := genny.WetRunner(context.Background())

		lg := logger.New(logger.DebugLevel)
		run.Logger = lg

		g, err := gmodel.New(&gmodel.Options{
			Name:                   name,
			Attrs:                  atts,
			Path:                   modelCmdConfig.ModelPath,
			Encoding:               modelCmdConfig.StructTag,
			ForceDefaultID:         true,
			ForceDefaultTimestamps: true,
		})
		if err != nil {
			return err
		}
		run.With(g)

		pwd, _ := os.Getwd()
		g, err = gogen.Fmt(pwd)
		if err != nil {
			return err
		}
		run.With(g)

		if _, err := os.Stat("go.mod"); err == nil {
			g = genny.New()
			g.Command(exec.Command("go", "mod", "tidy"))
			run.With(g)
		}

		if !modelCmdConfig.SkipMigration {
			p := cmd.Flag("path")
			path := ""
			if p != nil {
				path = p.Value.String()
			}
			e := cmd.Flag("env")
			var translator fizz.Translator
			if modelCmdConfig.MigrationType == "sql" {
				db, err := pop.Connect(e.Value.String())
				if err != nil {
					return err
				}
				translator = db.Dialect.FizzTranslator()
			}

			g, err = ctable.New(&ctable.Options{
				TableName:              name,
				Attrs:                  atts,
				Path:                   path,
				Type:                   modelCmdConfig.MigrationType,
				Translator:             translator,
				ForceDefaultID:         true,
				ForceDefaultTimestamps: true,
			})
			if err != nil {
				return err
			}
			run.With(g)
		}

		return run.Run()
	},
}

ModelCmd is the cmd to generate a model

View Source
var SQLCmd = &cobra.Command{
	Use:   "sql [name]",
	Short: "Generates Up/Down migrations for your database using SQL.",
	RunE: func(cmd *cobra.Command, args []string) error {
		name := ""
		if len(args) > 0 {
			name = args[0]
		}

		var (
			atts attrs.Attrs
			err  error
		)
		if len(args) > 1 {
			atts, err = attrs.ParseArgs(args[1:]...)
			if err != nil {
				return err
			}
		}

		run := genny.WetRunner(context.Background())

		lg := logger.New(logger.DebugLevel)
		run.Logger = lg

		p := cmd.Flag("path")
		path := ""
		if p != nil {
			path = p.Value.String()
		}
		e := cmd.Flag("env")
		type nameable interface {
			Name() string
		}
		var translator nameable
		db, err := pop.Connect(e.Value.String())
		if err != nil {
			return err
		}
		t := db.Dialect.FizzTranslator()
		if tn, ok := t.(nameable); ok {
			translator = tn
		} else {
			return errors.New("invalid fizz translator")
		}

		if len(atts) == 0 {
			g, err := cempty.New(&cempty.Options{
				Name:       name,
				Path:       path,
				Type:       "sql",
				Translator: translator,
			})
			if err != nil {
				return err
			}
			run.With(g)
		} else {
			g, err := ctable.New(&ctable.Options{
				TableName:  name,
				Path:       path,
				Type:       "sql",
				Attrs:      atts,
				Translator: t,
			})
			if err != nil {
				return err
			}
			run.With(g)
		}

		return run.Run()
	},
}

SQLCmd generates a SQL migration

Functions

This section is empty.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL