-
Notifications
You must be signed in to change notification settings - Fork 19
/
db.go
150 lines (118 loc) · 3.5 KB
/
db.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
package main
import (
"strings"
"github.com/AlecAivazis/survey/v2"
"github.com/GoAdminGroup/go-admin/modules/config"
"github.com/GoAdminGroup/go-admin/modules/db"
"gopkg.in/ini.v1"
)
type dbInfo struct {
DriverName string
Host string
Port string
File string
User string
Password string
Schema string
Database string
}
func initSurvey() {
survey.SelectQuestionTemplate = strings.ReplaceAll(survey.SelectQuestionTemplate,
"type to filter", "type to filter, enter to select")
survey.MultiSelectQuestionTemplate = strings.ReplaceAll(survey.MultiSelectQuestionTemplate,
"enter to select", "space to select")
survey.SelectQuestionTemplate = strings.ReplaceAll(survey.SelectQuestionTemplate,
"Use arrows to move, type to filter, enter to select",
getWord("Use arrows to move, type to filter, enter to select"))
survey.MultiSelectQuestionTemplate = strings.ReplaceAll(survey.MultiSelectQuestionTemplate,
"Use arrows to move, space to select, type to filter",
getWord("Use arrows to move, space to select, type to filter"))
}
func getDBInfoFromINIConfig(cfg *ini.File, connection string) *dbInfo {
section := "database"
if connection != "default" && connection != "" {
section = section + "." + connection
}
dbCfgModel, exist := cfg.GetSection(section)
if exist == nil {
return &dbInfo{
DriverName: dbCfgModel.Key("driver").Value(),
Host: dbCfgModel.Key("host").Value(),
User: dbCfgModel.Key("username").Value(),
Port: dbCfgModel.Key("port").Value(),
File: dbCfgModel.Key("file").Value(),
Password: dbCfgModel.Key("password").Value(),
Database: dbCfgModel.Key("database").Value(),
Schema: dbCfgModel.Key("schema").Value(),
}
}
return &dbInfo{}
}
func askForDBConfig(info *dbInfo) config.DatabaseList {
initSurvey()
if info.DriverName == "" {
info.DriverName = singleSelect(getWord("choose a driver"),
[]string{db.DriverMysql, db.DriverPostgresql, db.DriverSqlite, db.DriverMssql}, db.DriverMysql)
}
if info.DriverName != db.DriverSqlite {
defaultPort := "3306"
defaultUser := "root"
if info.DriverName == db.DriverPostgresql {
defaultPort = "5432"
defaultUser = "postgres"
}
if info.DriverName == db.DriverMssql {
defaultPort = "1433"
defaultUser = "sa"
}
if info.Host == "" {
info.Host = promptWithDefault("sql address", "127.0.0.1")
}
if info.Port == "" {
info.Port = promptWithDefault("sql port", defaultPort)
}
if info.User == "" {
info.User = promptWithDefault("sql username", defaultUser)
}
if info.Password == "" {
info.Password = promptPassword()
}
if info.Schema == "" && info.DriverName == db.DriverPostgresql {
info.Schema = promptWithDefault("sql schema", "public")
}
if info.Database == "" {
info.Database = prompt("sql database name")
}
return map[string]config.Database{
"default": {
Host: info.Host,
Port: info.Port,
User: info.User,
Pwd: info.Password,
Name: info.Database,
MaxIdleConns: 5,
MaxOpenConns: 10,
Driver: info.DriverName,
File: "",
},
}
} else {
if info.File == "" {
info.File = promptWithDefault("sql file", "./admin.db")
}
return map[string]config.Database{
"default": {
Driver: info.DriverName,
File: info.File,
},
}
}
}
func askForDBConnection(info *dbInfo) db.Connection {
var (
cfg = askForDBConfig(info)
conn = db.GetConnectionByDriver(info.DriverName)
)
conn.InitDB(cfg)
return conn
}