-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.go
More file actions
54 lines (46 loc) · 1.43 KB
/
config.go
File metadata and controls
54 lines (46 loc) · 1.43 KB
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
package main
import (
"fmt"
"strings"
"gopkg.in/ini.v1"
"github.com/go-sql-driver/mysql"
)
func loadConfigFile(c *mysql.Config, defaultsFile string, defaultsGroup string) (*mysql.Config, error) {
if c == nil {
c = mysql.NewConfig()
}
options := ini.LoadOptions{
AllowBooleanKeys: true,
IgnoreContinuation: true,
IgnoreInlineComment: true,
}
params, err := ini.LoadSources(options, defaultsFile)
if err != nil {
return nil, err
}
for _, s := range params.Sections() {
if s.Name() != defaultsGroup {
continue
}
if c.User == "" && s.Key("user").String() != "" {
c.User = s.Key("user").String()
}
if c.Passwd == "" && s.Key("password").String() != "" {
c.Passwd = s.Key("password").String()
}
if c.Addr == "" && s.Key("socket").String() != "" {
c.Net = "unix"
c.Addr = s.Key("socket").String()
}
if c.Addr == "" && s.Key("host").String() != "" {
c.Net = "tcp"
c.Addr = s.Key("host").String()
}
if c.Addr == "" && s.Key("port").String() != "" {
addr := strings.Split(c.Addr, ":")
c.Addr = addr[0] + ":" + s.Key("port").String()
}
return c, nil
}
return nil, fmt.Errorf("Client section not found")
}