-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
77 lines (67 loc) · 1.39 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
package goconf
import (
"fmt"
"os"
"gopkg.in/ini.v1"
)
//ConfigParse 配置环境
type ConfigParse struct {
Path string
NowConfig *ini.File
Env string
}
//NewConfigParse 初始化
func NewConfigParse(Path string) *ConfigParse {
if len(Path) != 0 && Path[len(Path)-1:len(Path)] == "/" {
Path = Path[0 : len(Path)-1]
}
temp := &ConfigParse{Path: Path}
temp.init()
return temp
}
func (c *ConfigParse) getFilePath(name string) (path string) {
// pwd, _ := os.Getwd()
if name == "" {
path = c.Path + "/resource/app.conf"
} else {
path = c.Path + fmt.Sprintf("/resource/app-%v.conf", name)
}
return path
}
func (c *ConfigParse) getFileName() string {
base := c.getFilePath("")
selfcfg, err := ini.Load(base)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
// c.selfcfgFile = selfcfg
dev := selfcfg.Section("app").Key("app").MustString("")
c.Env = dev
if dev == "" {
fmt.Println("app.conf的app为空")
os.Exit(1)
}
return dev
}
//init 内部初始化
func (c *ConfigParse) init() {
// name:=
path := c.getFilePath(c.getFileName())
cf, err := ini.Load(path)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
c.NowConfig = cf
nowConfig = cf
}
var nowConfig *ini.File
//GetConfig 获取配置文件
func (c *ConfigParse) GetConfig() *ini.File {
return c.NowConfig
}
// how to use
// parse := NewConfigParse("project_path")
// parse.Init()
// iniconf = parse.GetConfig()