-
Notifications
You must be signed in to change notification settings - Fork 9
/
config.go
executable file
·121 lines (108 loc) · 2.07 KB
/
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
/**
* config file end with enter line
https://github.com/vaughan0/go-ini/blob/master/ini.go
*/
package gof
import (
"bufio"
"io"
"os"
"regexp"
"strconv"
"strings"
)
const confLineDelimer byte = '\n'
var (
regex = regexp.MustCompile("^\\s*([^#\\s]+)\\s*=\\s*\"*(.+)*\"*\\s*$")
)
// Config
type Config struct {
configDict map[string]interface{}
}
func NewConfig() *Config {
return &Config{
configDict: make(map[string]interface{}),
}
}
// 从文件中加载配置
func LoadConfig(file string) (cfg *Config, err error) {
s := &Config{}
_err := s.load(file)
return s, _err
}
// 从配置中读取数据
func (c *Config) GetString(key string) string {
k, e := c.configDict[key]
if e {
v, _ := k.(string)
return v
}
return ""
}
// 从配置中读取数据
func (c *Config) Get(key string) interface{} {
v, e := c.configDict[key]
if e {
return v
}
return nil
}
func (c *Config) Set(key string, v interface{}) {
if _, ok := c.configDict[key]; ok {
panic("Key '" + key + "' is exist in config")
}
c.configDict[key] = v
}
func (c *Config) GetInt(key string) int {
k, e := c.configDict[key]
if e {
v, ok := k.(int)
if ok {
return v
}
if sv, ok := k.(string); ok {
if iv, err := strconv.Atoi(sv); err == nil {
return iv
}
}
}
return 0
}
func (c *Config) GetFloat(key string) float64 {
k, e := c.configDict[key]
if e {
v, ok := k.(float64)
if ok {
return v
}
if sv, ok := k.(string); ok {
if iv, err := strconv.ParseFloat(sv, 64); err == nil {
return iv
}
}
}
return 0
}
// 从文件中加载配置
func (c *Config) load(file string) (err error) {
c.configDict = make(map[string]interface{})
//var allContent string = ""
f, _err := os.Open(file)
if _err != nil {
return _err
}
defer f.Close()
reader := bufio.NewReader(f)
for {
line, _err := reader.ReadString(confLineDelimer)
if _err == io.EOF {
break
}
if regex.Match([]byte(line)) {
matches := regex.FindStringSubmatch(line)
//c.configDict[matches[1]] = matches[2]
c.configDict[matches[1]] = strings.TrimSpace(matches[2])
}
}
return nil
}