Skip to content

Commit

Permalink
added a new "init" command to generate a default database.yml
Browse files Browse the repository at this point in the history
  • Loading branch information
markbates committed Aug 17, 2016
1 parent 90c1350 commit 6741bcd
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 15 deletions.
8 changes: 1 addition & 7 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
"gopkg.in/yaml.v2"
)

var lookupPaths = []string{"", "/config", "../", "../config", "../..", "../../config"}
var lookupPaths = []string{"", "./config", "/config", "../", "../config", "../..", "../../config"}
var ConfigName = "database.yml"

func init() {
Expand All @@ -39,12 +39,6 @@ func AddLookupPaths(paths ...string) {
}

func findConfigPath() (string, error) {
// pwd, err := getAppPath()
// if err != nil {
// return "", err
// }
//
// lookup paths
for _, p := range LookupPaths() {
path, _ := filepath.Abs(filepath.Join(p, ConfigName))
if _, err := os.Stat(path); err == nil {
Expand Down
97 changes: 97 additions & 0 deletions soda/cmd/init.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package cmd

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

"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)
},
}

func init() {
RootCmd.AddCommand(initCmd)
initCmd.Flags().StringVarP(&dialect, "type", "t", "postgres", "What type of database do you want to use? (postgres, mysql, sqlite3)")
}
16 changes: 9 additions & 7 deletions soda/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,19 +42,21 @@ func Execute() {

func init() {
RootCmd.Flags().BoolVarP(&version, "version", "v", false, "Show version information")
RootCmd.PersistentFlags().StringVarP(&cfgFile, "config", "c", "config/database.yml", "The configuration file you would like to use.")
RootCmd.PersistentFlags().StringVarP(&cfgFile, "config", "c", "", "The configuration file you would like to use.")
RootCmd.PersistentFlags().StringVarP(&env, "env", "e", "development", "The environment you want to run migrations against. Will use $GO_ENV if set.")
RootCmd.PersistentFlags().BoolVarP(&debugMode, "debug", "d", false, "Use debug/verbose mode")
}

func setConfigLocation() {
abs, err := filepath.Abs(cfgFile)
if err != nil {
return
if cfgFile != "" {
abs, err := filepath.Abs(cfgFile)
if err != nil {
return
}
dir, file := filepath.Split(abs)
pop.AddLookupPaths(dir)
pop.ConfigName = file
}
dir, file := filepath.Split(abs)
pop.AddLookupPaths(dir)
pop.ConfigName = file
}

func getConn() *pop.Connection {
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.0.1"
const Version = "3.1.0"

0 comments on commit 6741bcd

Please sign in to comment.