-
Notifications
You must be signed in to change notification settings - Fork 636
/
config.go
265 lines (243 loc) · 6.53 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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
// Copyright 2018 The Chubao Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
// implied. See the License for the specific language governing
// permissions and limitations under the License.
package config
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"os"
"path"
"strconv"
)
const (
DefaultConstConfigFile = "constcfg"
)
// Config defines the struct of a configuration in general.
type Config struct {
data map[string]interface{}
Raw []byte
}
func newConfig() *Config {
result := new(Config)
result.data = make(map[string]interface{})
return result
}
// LoadConfigFile loads config information from a JSON file.
func LoadConfigFile(filename string) (*Config, error) {
result := newConfig()
err := result.parse(filename)
if err != nil {
log.Printf("error loading config file %s: %s", filename, err)
}
return result, err
}
// LoadConfigString loads config information from a JSON string.
func LoadConfigString(s string) *Config {
result := newConfig()
err := json.Unmarshal([]byte(s), &result.data)
if err != nil {
log.Fatalf("error parsing config string %s: %s", s, err)
}
return result
}
func (c *Config) parse(fileName string) error {
jsonFileBytes, err := ioutil.ReadFile(fileName)
c.Raw = jsonFileBytes
if err == nil {
err = json.Unmarshal(jsonFileBytes, &c.data)
}
return err
}
// GetString returns a string for the config key.
func (c *Config) GetString(key string) string {
x, present := c.data[key]
if !present {
return ""
}
if result, isString := x.(string); isString {
return result
}
return ""
}
// GetFloat returns a float value for the config key.
func (c *Config) GetFloat(key string) float64 {
x, present := c.data[key]
if !present {
return -1
}
if result, isFloat := x.(float64); isFloat {
return result
}
return 0
}
// returns a bool value for the config key with default val when not present
func (c *Config) GetBoolWithDefault(key string, defval bool) bool {
_, present := c.data[key]
if !present {
return defval
}
return c.GetBool(key)
}
// GetBool returns a bool value for the config key.
func (c *Config) GetBool(key string) bool {
x, present := c.data[key]
if !present {
return false
}
if result, isBool := x.(bool); isBool {
return result
}
if result, isString := x.(string); isString {
if result == "true" {
return true
}
}
return false
}
// GetBool returns a int value for the config key.
func (c *Config) GetInt(key string) int64 {
x, present := c.data[key]
if !present {
return 0
}
if result, isInt := x.(int64); isInt {
return result
}
return 0
}
// GetBool returns a int64 value for the config key.
func (c *Config) GetInt64(key string) int64 {
x, present := c.data[key]
if !present {
return 0
}
if result, isInt := x.(int64); isInt {
return result
}
if result, isFloat := x.(float64); isFloat {
return int64(result)
}
if result, isString := x.(string); isString {
r, err := strconv.ParseInt(result, 10, 64)
if err == nil {
return r
}
}
return 0
}
// GetSlice returns an array for the config key.
func (c *Config) GetSlice(key string) []interface{} {
result, present := c.data[key]
if !present {
return []interface{}(nil)
}
return result.([]interface{})
}
func (c *Config) GetStringSlice(key string) []string {
s := c.GetSlice(key)
result := make([]string, 0, len(s))
for _, item := range s {
result = append(result, item.(string))
}
return result
}
// Check and get a string for the config key.
func (c *Config) CheckAndGetString(key string) (string, bool) {
x, present := c.data[key]
if !present {
return "", false
}
if result, isString := x.(string); isString {
return result, true
}
return "", false
}
// GetBool returns a bool value for the config key.
func (c *Config) CheckAndGetBool(key string) (bool, bool) {
x, present := c.data[key]
if !present {
return false, false
}
if result, isBool := x.(bool); isBool {
return result, true
}
// Take string value "true" and "false" as well.
if result, isString := x.(string); isString {
if result == "true" {
return true, true
}
if result == "false" {
return false, true
}
}
return false, false
}
func NewIllegalConfigError(configKey string) error {
return fmt.Errorf("illegal config %s", configKey)
}
type ConstConfig struct {
Listen string `json:"listen"`
RaftReplicaPort string `json:"raftReplicaPort"`
RaftHeartbetPort string `json:"raftHeartbetPort"`
}
func (ccfg *ConstConfig) Equals(cfg *ConstConfig) bool {
return (ccfg.Listen == cfg.Listen &&
ccfg.RaftHeartbetPort == cfg.RaftHeartbetPort &&
ccfg.RaftReplicaPort == cfg.RaftReplicaPort)
}
// check listen port, raft replica port and raft heartbeat port
func CheckOrStoreConstCfg(fileDir, fileName string, cfg *ConstConfig) (ok bool, err error) {
var filePath = path.Join(fileDir, fileName)
var buf []byte
buf, err = ioutil.ReadFile(filePath)
if err != nil && !os.IsNotExist(err) {
return false, fmt.Errorf("read config file %v failed: %v", filePath, err)
}
if os.IsNotExist(err) || len(buf) == 0 {
// Persist configuration to disk
if buf, err = json.Marshal(cfg); err != nil {
return false, fmt.Errorf("marshal const config failed: %v", err)
}
if err = os.MkdirAll(fileDir, 0755); err != nil {
return false, fmt.Errorf("make directory %v filed: %v", fileDir, err)
}
var file *os.File
if file, err = os.OpenFile(filePath, os.O_CREATE|os.O_RDWR, 0755); err != nil {
return false, fmt.Errorf("create config file %v failed: %v", filePath, err)
}
defer func() {
_ = file.Close()
if err != nil {
_ = os.Remove(filePath)
}
}()
if _, err = file.Write(buf); err != nil {
return false, fmt.Errorf("write config file %v failed: %v", filePath, err)
}
if err = file.Sync(); err != nil {
return false, fmt.Errorf("sync config file %v failed: %v", filePath, err)
}
return true, nil
}
// Load and check stored const configuration
storedConstCfg := new(ConstConfig)
if err = json.Unmarshal(buf, storedConstCfg); err != nil {
return false, fmt.Errorf("unmarshal const config %v failed: %v", filePath, err)
}
if ok := storedConstCfg.Equals(cfg); !ok {
return false, fmt.Errorf("compare const config %v and %v failed: %v", storedConstCfg, cfg, err)
}
return true, nil
}