forked from gobuffalo/pop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
61 lines (53 loc) · 1.5 KB
/
config.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package generate
import (
"os"
"path/filepath"
"strings"
"github.com/gobuffalo/envy"
"github.com/gobuffalo/makr"
"github.com/markbates/going/defaults"
"github.com/pkg/errors"
"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 {
cflag := cmd.Flag("config")
cfgFile := defaults.String(cflag.Value.String(), "database.yml")
pwd, err := os.Getwd()
if err != nil {
return errors.Wrap(err, "couldn't get the current directory")
}
data := map[string]interface{}{
"dialect": dialect,
"name": filepath.Base(pwd),
}
return GenerateConfig(cfgFile, data)
},
}
func packagePath(root string) string {
src := filepath.ToSlash(filepath.Join(envy.GoPath(), "src"))
root = filepath.ToSlash(root)
return strings.Replace(root, src+"/", "", 2)
}
func GenerateConfig(cfgFile string, data map[string]interface{}) error {
pwd, _ := os.Getwd()
if data["appPath"] == nil {
data["appPath"] = pwd
}
if data["sqlitePath"] == nil {
data["sqlitePath"] = pwd
}
dialect = strings.ToLower(data["dialect"].(string))
if t, ok := configTemplates[dialect]; ok {
g := makr.New()
g.Add(makr.NewFile(cfgFile, t))
return g.Run(".", data)
}
return errors.Errorf("Could not initialize %s!", dialect)
}