-
Notifications
You must be signed in to change notification settings - Fork 3
/
db_config.go
185 lines (153 loc) · 3.66 KB
/
db_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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package main
import(
"os"
"path"
"strings"
)
var cmdDBConfig = &Command{
UsageLine: "db:config -driver=[driver_name] -dbname=[dbname]",
Short: "generate gorm database config for Revel application",
Long: `
Create new database.conf file in app/conf of your application.
It puts all necessary files as import.
--driver is required. The configuration will be changed based on --driver parameter.
For example:
revel db:config -driver=[mysql] -dbName=[dbname]
`,
}
var driver flagValue
var dbName flagValue
func init() {
cmdDBConfig.Run = generateDBConfig
cmdDBConfig.Flag.Var(&driver, "driver", "database driver: mysql, sqlite3, postgresql, etc.")
cmdDBConfig.Flag.Var(&dbName, "dbname", "database name: test, app_name, etc.")
}
func generateDBConfig(cmd *Command, args []string) {
gopath := os.Getenv("GOPATH")
if gopath == "" {
ColorLog("[ERRO] $GOPATH not found.\nRun 'revel help db' for usage.\n")
os.Exit(2)
}
// check driver params
if len(args) < 0 {
ColorLog("[ERRO] No driver given.\nRun 'revel help db' for usage.\n")
os.Exit(2)
}else{
cmd.Flag.Parse(args[0:])
}
// check driver params
if len(args) == 1 {
cmd.Flag.Parse(args[1:])
}
if dbName.String() == ""{
dbName = "test"
}
ColorLog("[SUCC] database driver is using as '%s'.\n",driver.String())
ColorLog("[SUCC] database name is using as '%s'.\n",dbName.String())
// get current path
pwd, _ := os.Getwd()
destFile := path.Join(pwd, "conf", "database.conf")
if _, err := os.Stat(destFile); !os.IsNotExist(err) {
if err = os.Remove(destFile); err != nil{
ColorLog("[WARN] Failed to remove existing file database.go:", err)
os.Exit(2)
}
}
if file, err := os.OpenFile(destFile, os.O_CREATE|os.O_EXCL|os.O_RDWR, 0666); err == nil {
defer file.Close()
switch driver{
case "mysql":
content := strings.Replace(mysqlDatabaseConfigTpl, "{{dbName}}", dbName.String(), -1)
file.WriteString(content)
case "sqlite3":
content := strings.Replace(sqliteDatabaseConfigTpl, "{{dbName}}", dbName.String(), -1)
file.WriteString(content)
case "postgresql":
content := strings.Replace(postgresDatabaseConfigTpl, "{{dbName}}", dbName.String(), -1)
file.WriteString(content)
default:
ColorLog("[ERRO] Invalid database driver.\n");
os.Exit(2)
}
} else {
ColorLog("[ERRO] No driver given.\nRun 'revel help db' for usage.\n")
os.Exit(2)
}
ColorLog("[SUCC] grom database config file is generated as '%s'.\n",destFile)
}
// templates
var mysqlDatabaseConfigTpl =
`[dev]
driver= mysql
encoding: utf8
database: {{dbName}}_dev
maxConn = 30
maxIdle = 30
username= root
password=
host = 127.0.0.1
port = 3306
[prod]
driver: mysql
encoding: utf8
database: {{dbName}}
maxConn = 30
maxIdle = 30
username= root
password=
host = 127.0.0.1
port = 3306
[test]
driver= mysql
encoding= utf8
database= {{dbName}}_test
maxConn = 30
maxIdle = 30
username= root
password=
host = 127.0.0.1
port = 3306`
var sqliteDatabaseConfigTpl =
`[dev]
driver= sqlite3
database= db/{{dbName}}_dev.sqlite3
maxConn = 30
maxIdle = 30
timeout: 5000
[prod]
driver= sqlite3
database= db/{{dbName}}.sqlite3
maxConn = 30
maxIdle = 30
timeout= 5000
[test]
driver= sqlite3
database= db/{{dbName}}_test.sqlite3
maxConn = 30
maxIdle = 30
timeout= 5000`
var postgresDatabaseConfigTpl =
`[dev]
driver= postgresql
encoding= unicode
database= {{dbName}}_dev
maxConn = 30
maxIdle = 30
username= postgres
password= postgres
[prod]
driver: postgresql
encoding= unicode
database= {{dbName}}
maxConn = 30
maxIdle = 30
username= postgres
password= postgres
[test]
driver: postgresql
encoding= unicode
database= {{dbName}}_test
maxConn = 30
maxIdle = 30
username= postgres
password= postgres`