-
Notifications
You must be signed in to change notification settings - Fork 1
/
woocommerce.go
52 lines (44 loc) · 1.11 KB
/
woocommerce.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
package gocommerce
import (
"os"
"regexp"
)
type WooCommerce struct {
basePlatform
}
var wpLookupRgx = map[string]string{
"user": `define\(\s*['"]DB_USER['"]\s*,\s*['"](\S+?)['"]\s*\);`,
"pass": `define\(\s*['"]DB_PASSWORD['"]\s*,\s*['"]([^']{0,64})['"]\s*\);`,
"host": `define\(\s*['"]DB_HOST['"]\s*,\s*['"](\S+?)['"]\s*\);`,
"db": `define\(\s*['"]DB_NAME['"]\s*,\s*['"](\S+?)['"]\s*\);`,
"prefix": `$?table_prefix\s*=\s*['"]([^']*?)['"]\s*;`,
}
func (w *WooCommerce) ParseConfig(cfgPath string) (*StoreConfig, error) {
data, err := os.ReadFile(cfgPath)
if err != nil {
return nil, err
}
matches := map[string]string{}
port := 3306
for k, v := range wpLookupRgx {
m := regexp.MustCompile(v).FindStringSubmatch(string(data))
if len(m) != 2 {
continue
}
matches[k] = m[1]
}
if h, p, e := parseHostPort(matches["host"]); p > 0 && e == nil {
matches["host"] = h
port = p
}
return &StoreConfig{
DB: &DBConfig{
Host: matches["host"],
User: matches["user"],
Pass: matches["pass"],
Name: matches["db"],
Prefix: matches["prefix"],
Port: port,
},
}, nil
}