-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathconf_helper.go
executable file
·67 lines (56 loc) · 1.09 KB
/
conf_helper.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
/*
* Description : 配置文件 yaml TODO 测试
* Author : ManGe
* Mail : 2912882908@qq.com
**/
package gathertool
import (
"fmt"
"os"
yaml "gopkg.in/yaml.v3"
)
// TODO 扩展到所有能支持的配置文件
var Config *conf
type conf struct {
Path string
Data map[string]any
}
func NewConf(appConfigPath string) error {
Config = &conf{
Path: appConfigPath,
Data: make(map[string]any),
}
err := Config.Init()
return err
}
func (c *conf) Init() error {
if !FileExists(c.Path) {
return fmt.Errorf("未找到配置文件: %v", c.Path)
}
Info("读取配置文件:", c.Path)
//读取yaml文件到缓存中
config, err := os.ReadFile(c.Path)
if err != nil {
ErrorF("读取配置文件 %v 失败", c.Path)
return err
}
return yaml.Unmarshal(config, c.Data)
}
func (c *conf) GetInt(key string) int {
if c.Data == nil {
_ = c.Init()
}
return Any2Int(c.Data[key])
}
func (c *conf) Get(key string) any {
if c.Data == nil {
_ = c.Init()
}
return c.Data[key]
}
func (c *conf) GetStr(key string) string {
if c.Data == nil {
_ = c.Init()
}
return Any2String(c.Data[key])
}