generate

package
v3.8.0+incompatible Latest Latest
Warning

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

Go to latest
Published: Aug 30, 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 {
		dialect = strings.ToLower(dialect)
		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 errors.Wrap(err, "couldn't get the current directory")
			}
			err = os.MkdirAll(path.Dir(cfgFile), 0766)
			f, err := os.Create(cfgFile)
			if err != nil {
				return errors.Wrapf(err, "couldn't create the config file %s", cfgFile)
			}
			tp := template.Must(template.New("database.yml").Parse(t))

			dir = path.Base(dir)
			err = tp.Execute(f, dir)
			if err != nil {
				return errors.Wrap(err, "couldn't execute template")
			}
			fmt.Printf("Generated %s using the %s template.\n", cfgFile, dialect)
			return nil
		}
		return errors.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 errors.Wrapf(err, "couldn't create folder %s", model.Package)
		}

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

		tfname := 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(tfname, []byte(tmp), 0666)
		if err != nil {
			return errors.Wrapf(err, "couldn't write to file %s", tfname)
		}
		fmt.Printf("> %s\n", tfname)

		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