generate

package
v3.7.0+incompatible Latest Latest
Warning

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

Go to latest
Published: Aug 26, 2016 License: MIT Imports: 14 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.",
	RunE: func(cmd *cobra.Command, args []string) error {
		if t, ok := configTemplates[dialect]; ok {
			cflag := cmd.Flag("config")
			cfgFile := defaults.String(cflag.Value.String(), "database.yml")
			dir, err := os.Getwd()
			if err != nil {
				return err
			}
			os.MkdirAll(path.Dir(cfgFile), 0766)
			f, err := os.Create(cfgFile)
			if err != nil {
				return err
			}
			tp := template.Must(template.New("database.yml").Parse(t))

			dir = path.Base(dir)
			err = tp.Execute(f, dir)
			if err == nil {
				fmt.Printf("Generated %s using the %s template.\n", cfgFile, dialect)
			}
			return err
		}
		return fmt.Errorf("Could not initialize %s!", dialect)
	},
}
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 {
		if len(args) == 0 {
			return errors.New("You must supply a name for your migration!")
		}
		cflag := cmd.Flag("path")
		migrationPath := defaults.String(cflag.Value.String(), "./migrations")
		return pop.MigrationCreate(migrationPath, args[0], "fizz", nil, nil)
	},
}
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 {
		if len(args) == 0 {
			return errors.New("You must supply a name for your model!")
		}

		model := newModel(args[0])

		hasNulls := false
		for _, def := range args[1:] {
			col := strings.Split(def, ":")
			if len(col) == 1 {
				col = append(col, "string")
			}
			nullable := nrx.MatchString(col[1])
			if !hasNulls && nullable {
				hasNulls = true
				model.Imports = append(model.Imports, "github.com/markbates/pop/nulls")
			}
			model.Attributes = append(model.Attributes, attribute{
				Names:        newName(col[0]),
				OriginalType: col[1],
				GoType:       colType(col[1]),
				Nullable:     nullable,
			})
		}

		err := os.MkdirAll(model.Package, 0766)
		if err != nil {
			return err
		}

		fname := filepath.Join(model.Package, model.Names.File+".go")
		err = ioutil.WriteFile(fname, []byte(model.String()), 0666)
		if err != nil {
			return err
		}
		fmt.Printf("> %s\n", fname)

		fname = filepath.Join(model.Package, model.Names.File+"_test.go")
		tmp := strings.Replace(modelTestTemplate, "MODEL_NAME", model.Names.Proper, -1)
		tmp = strings.Replace(tmp, "PACKAGE_NAME", model.Package, -1)
		err = ioutil.WriteFile(fname, []byte(tmp), 0666)
		if err != nil {
			return err
		}
		fmt.Printf("> %s\n", fname)

		md, _ := filepath.Abs(fname)
		goi := exec.Command("gofmt", "-w", md)
		out, err := goi.CombinedOutput()
		if err != nil {
			fmt.Printf("Received an error when trying to run gofmt -> %#v\n", err)
			fmt.Println(out)
		}

		if !skipMigration {
			cflag := cmd.Flag("path")
			migrationPath := defaults.String(cflag.Value.String(), "./migrations")
			err = pop.MigrationCreate(migrationPath, fmt.Sprintf("create_%s", model.Names.Table), "fizz", []byte(model.Fizz()), []byte(fmt.Sprintf("drop_table(\"%s\")", model.Names.Table)))
			if err != nil {
				return err
			}
		}

		return nil
	},
}
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 {
		if len(args) == 0 {
			return errors.New("You must supply a name for your migration!")
		}
		cflag := cmd.Flag("path")
		migrationPath := defaults.String(cflag.Value.String(), "./migrates")
		return pop.MigrationCreate(migrationPath, args[0], "sql", nil, nil)
	},
}

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