forked from gobuffalo/pop
/
config_cmd.go
66 lines (58 loc) · 1.75 KB
/
config_cmd.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
62
63
64
65
66
package generate
import (
"fmt"
"os"
"path/filepath"
"strings"
"github.com/gobuffalo/makr"
"github.com/gobuffalo/pop"
"github.com/markbates/going/defaults"
"github.com/pkg/errors"
"github.com/spf13/cobra"
)
func init() {
ConfigCmd.Flags().StringVarP(&dialect, "type", "t", "postgres", fmt.Sprintf("The type of database you want to use (%s)", strings.Join(pop.AvailableDialects, ", ")))
}
var dialect string
// ConfigCmd is the command to generate pop config files
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 Config(cfgFile, data)
},
}
// GenerateConfig generates pop configuration files.
//
// Deprecated: use Config instead.
func GenerateConfig(cfgFile string, data map[string]interface{}) error {
fmt.Println(`Warning: GenerateConfig is deprecated, and will be removed in a future version. Please use Config instead.`)
return Config(cfgFile, data)
}
// Config generates pop configuration files.
func Config(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)
}