-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
64 lines (56 loc) · 1.18 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
package simple_proxy
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"reflect"
)
type Config struct {
Server string `json:"server"`
ServerPort int `json:"server_port"`
Client string `json:"client"`
Password string `json:"password"`
Proxy string `json:"proxy"`
ProxyPort int `json:"proxy_port"`
}
func ParseConfig(path string) (config *Config, err error) {
file, err := os.Open(path) // For read access.
if err != nil {
return
}
defer file.Close()
data, err := ioutil.ReadAll(file)
if err != nil {
return
}
config = &Config{}
if err = json.Unmarshal(data, config); err != nil {
return nil, err
}
return
}
func UpdateConfig(old, new *Config) {
newVal := reflect.ValueOf(new).Elem()
oldVal := reflect.ValueOf(old).Elem()
for i := 0; i < newVal.NumField(); i++ {
newField := newVal.Field(i)
oldField := oldVal.Field(i)
switch newField.Kind() {
case reflect.Interface:
if fmt.Sprintf("%v", newField.Interface()) != "" {
oldField.Set(newField)
}
case reflect.String:
s := newField.String()
if s != "" {
oldField.SetString(s)
}
case reflect.Int:
i := newField.Int()
if i != 0 {
oldField.SetInt(i)
}
}
}
}