forked from gobuffalo/pop
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
188 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
`, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |