/
config.go
71 lines (59 loc) · 1.5 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
package danmu
import (
"errors"
"flag"
"fmt"
log "github.com/alecthomas/log4go"
"github.com/larspensjo/config"
"os"
"runtime"
)
var (
Conf *Config
appPath = os.Getenv("GOPATH") + "/src/github.com/kong36088/danmu/"
configFile = flag.String("config", appPath+"config/config.ini", "General configuration file")
)
type Config struct {
values map[string]map[string]string
}
//topic list
func NewConfig() *Config {
return &Config{
values: make(map[string]map[string]string),
}
}
func InitConfig() error {
Conf = NewConfig()
runtime.GOMAXPROCS(runtime.NumCPU())
flag.Parse()
cfgSecs, err := config.ReadDefault(*configFile)
if err != nil {
return errors.New(fmt.Sprintf("Fail to find %s %s", *configFile, err))
}
for _, section := range cfgSecs.Sections() {
options, err := cfgSecs.SectionOptions(section)
if err != nil {
log.Error("Read options of file %s section %s failed, %s\n", *configFile, section, err)
continue
}
Conf.values[section] = make(map[string]string)
for _, v := range options {
option, err := cfgSecs.String(section, v)
if err != nil {
log.Error("Read file %s option %s failed, %s\n", *configFile, v, err)
continue
}
Conf.values[section][v] = option
}
}
return nil
}
func (c *Config) GetConfig(section, option string) string {
return c.values[section][option]
}
func (c *Config) GetSectionConfig(section string) map[string]string {
return c.values[section]
}
func (c *Config) GetAllConfig() map[string]map[string]string {
return c.values
}