Skip to content

Commit

Permalink
added a generate sub command
Browse files Browse the repository at this point in the history
  • Loading branch information
markbates committed Aug 18, 2016
1 parent b900eb4 commit f4cb42f
Show file tree
Hide file tree
Showing 10 changed files with 188 additions and 84 deletions.
7 changes: 6 additions & 1 deletion migration.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,13 @@ func MigrationCreate(path, name, ext string) error {
n := time.Now().UTC()
s := n.Format("20060102150405")

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

up := filepath.Join(path, (fmt.Sprintf("%s_%s.up.%s", s, name, ext)))
err := ioutil.WriteFile(up, []byte(""), 0666)
err = ioutil.WriteFile(up, []byte(""), 0666)
if err != nil {
return err
}
Expand Down
18 changes: 18 additions & 0 deletions soda/cmd/generate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package cmd

import (
"github.com/markbates/pop/soda/cmd/generate"
"github.com/spf13/cobra"
)

var generateCmd = &cobra.Command{
Use: "generate",
Aliases: []string{"g"},
}

func init() {
generateCmd.AddCommand(generate.ConfigCmd)
generateCmd.AddCommand(generate.FizzCmd)
generateCmd.AddCommand(generate.SQLCmd)
RootCmd.AddCommand(generateCmd)
}
46 changes: 46 additions & 0 deletions soda/cmd/generate/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package generate

import (
"fmt"
"html/template"
"os"
"path"

"github.com/markbates/going/defaults"
"github.com/spf13/cobra"
)

func init() {
ConfigCmd.Flags().StringVarP(&dialect, "type", "t", "postgres", "What type of database do you want to use? (postgres, mysql, sqlite3)")
}

var dialect string

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)
},
}
61 changes: 61 additions & 0 deletions soda/cmd/generate/config_templates.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package generate

var configTemplates = map[string]string{
"postgres": `development:
dialect: postgres
database: {{.}}_development
username: postgres
password: postgres
host: 127.0.0.1
test:
dialect: postgres
database: {{.}}_test
username: postgres
password: postgres
host: 127.0.0.1
production:
dialect: postgres
database: {{.}}_production
username: postgres
password: postgres
host: 127.0.0.1
`,
"mysql": `development:
dialect: "mysql"
database: "{{.}}_development"
host: "localhost"
port: "3306"
user: "root"
password: "root"
test:
dialect: "mysql"
database: "{{.}}_test"
host: "localhost"
port: "3306"
user: "root"
password: "root"
production:
dialect: "mysql"
database: "{{.}}_production"
host: "localhost"
port: "3306"
user: "root"
password: "root"
`,
"sqlite3": `development:
dialect: "sqlite3"
database: "./{{.}}_development.sqlite"
test:
dialect: "sqlite3"
database: "./{{.}}_test.sqlite"
production:
dialect: "sqlite3"
database: "./{{.}}_production.sqlite"
`,
}
22 changes: 22 additions & 0 deletions soda/cmd/generate/fizz_migration.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package generate

import (
"errors"

"github.com/markbates/going/defaults"
"github.com/markbates/pop"
"github.com/spf13/cobra"
)

var FizzCmd = &cobra.Command{
Use: "fizz [name]",
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(), "./migrates")
return pop.MigrationCreate(migrationPath, args[0], "fizz")
},
}
22 changes: 22 additions & 0 deletions soda/cmd/generate/sql_migration.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package generate

import (
"errors"

"github.com/markbates/going/defaults"
"github.com/markbates/pop"
"github.com/spf13/cobra"
)

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")
},
}
81 changes: 3 additions & 78 deletions soda/cmd/init.go
Original file line number Diff line number Diff line change
@@ -1,93 +1,18 @@
package cmd

import (
"fmt"
"html/template"
"os"
"path"

"github.com/markbates/pop/soda/cmd/generate"
"github.com/spf13/cobra"
)

var dialect string

var initTemplates = map[string]string{
"postgres": `development:
dialect: postgres
database: {{.}}_development
username: postgres
password: postgres
host: 127.0.0.1
test:
dialect: postgres
database: {{.}}_test
username: postgres
password: postgres
host: 127.0.0.1
production:
dialect: postgres
database: {{.}}_production
username: postgres
password: postgres
host: 127.0.0.1
`,
"mysql": `development:
dialect: "mysql"
database: "{{.}}_development"
host: "localhost"
port: "3306"
user: "root"
password: "root"
test:
dialect: "mysql"
database: "{{.}}_test"
host: "localhost"
port: "3306"
user: "root"
password: "root"
production:
dialect: "mysql"
database: "{{.}}_production"
host: "localhost"
port: "3306"
user: "root"
password: "root"
`,
"sqlite3": `development:
dialect: "sqlite3"
database: "./{{.}}_development.sqlite"
test:
dialect: "sqlite3"
database: "./{{.}}_test.sqlite"
production:
dialect: "sqlite3"
database: "./{{.}}_production.sqlite"
`,
}

var initCmd = &cobra.Command{
Use: "init",
Short: "Sets up a new Pop/Soda Project",
RunE: func(cmd *cobra.Command, args []string) error {
if t, ok := initTemplates[dialect]; ok {
dir, err := os.Getwd()
if err != nil {
return err
}
f, err := os.Create(cfgFile)
if err != nil {
return err
}
tp := template.Must(template.New("database.yml").Parse(t))
return tp.Execute(f, path.Base(dir))
}
return fmt.Errorf("Could not initialize %s!", dialect)
err := generate.ConfigCmd.RunE(cmd, args)
return err
},
}

Expand Down
7 changes: 4 additions & 3 deletions soda/cmd/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ import (
var migrationPath string

var migrateCmd = &cobra.Command{
Use: "migrate",
Short: "Runs migrations against your database.",
Use: "migrate",
Aliases: []string{"m"},
Short: "Runs migrations against your database.",
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
RootCmd.PersistentPreRun(cmd, args)
return os.MkdirAll(migrationPath, 0766)
Expand All @@ -28,5 +29,5 @@ var migrateCmd = &cobra.Command{

func init() {
RootCmd.AddCommand(migrateCmd)
migrateCmd.PersistentFlags().StringVarP(&migrationPath, "path", "p", "./migrations", "Path to the migrations folder")
RootCmd.PersistentFlags().StringVarP(&migrationPath, "path", "p", "./migrations", "Path to the migrations folder")
}
6 changes: 5 additions & 1 deletion soda/cmd/migrate_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,21 @@ package cmd

import (
"errors"
"fmt"

"github.com/markbates/pop"
"github.com/spf13/cobra"
)

var migrationType string

var depWarning = "[DEPRACTION WARNING] This command is deprecated. Please use `soda generate fizz` or `soda generate sql` instead."

var migrateCreateCmd = &cobra.Command{
Use: "create [name]",
Short: "Generates Up/Down migrations for your database.",
Short: depWarning,
RunE: func(cmd *cobra.Command, args []string) error {
fmt.Println(depWarning)
if len(args) == 0 {
return errors.New("You must supply a name for your migration!")
}
Expand Down
2 changes: 1 addition & 1 deletion soda/cmd/version.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
package cmd

const Version = "3.1.0"
const Version = "3.2.0"

0 comments on commit f4cb42f

Please sign in to comment.