-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconfig.go
115 lines (102 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
package config
import (
"flag"
"strconv"
"strings"
)
type CONFIG_MODE int32
const (
INI CONFIG_MODE = iota
JSON
ENV
)
const _DEFAULT_CONFIG = "default_config"
type ConfigPool struct {
Pool map[string]Configer
}
type Configer interface {
Get(string) string
Set(string, string)
Bool(string) bool
Int(string) int
Int64(string) int64
Float(string) float64
String(string) string
}
var Config *ConfigPool
func init() {
if Config != nil {
return
}
Config = &ConfigPool{}
Config.Pool = make(map[string]Configer)
}
func InitDefault(mode CONFIG_MODE, operationParam, defaultValue, tips string) Configer {
confFile := flag.String(operationParam, defaultValue, tips)
flag.Parse()
if *confFile == "" {
panic("run with -h to find usage")
}
AddConfiger(mode, _DEFAULT_CONFIG, *confFile)
return Default()
}
func Default() Configer {
return Use(_DEFAULT_CONFIG)
}
func Use(name string) Configer {
return Config.Pool[name]
}
func AddConfiger(mode CONFIG_MODE, name, file string) {
log.Info("add configer", mode, file)
var configer Configer
switch mode {
case INI:
configer = newIniConfiger(file)
break
case JSON:
// TODO
case ENV:
// TODO
configer = newEnvConfiger()
default:
configer = nil
}
if configer == nil {
log.Error("error configer", mode, name, file)
panic("configer is nil")
}
Config.Pool[name] = configer
}
func toInt(value string) int {
ret, err := strconv.Atoi(value)
if err != nil {
log.Error("parse to int error", value, err.Error())
}
return ret
}
func toInt64(value string) int64 {
ret, err := strconv.ParseInt(value, 10, 64)
if err != nil {
log.Error("parse to int64 error", value, err.Error())
}
return ret
}
func toFloat(value string) float64 {
ret, err := strconv.ParseFloat(value, 64)
if err != nil {
log.Error("parse to float error", value, err.Error())
}
return ret
}
func toBool(value string) bool {
ret := strings.ToLower(value)
switch ret {
case "1", "true", "y", "on", "yes":
return true
case "0", "false", "n", "off", "no":
return false
default:
log.Error("parse to bool error", value)
}
return false
}