forked from xushiwei/gocode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
229 lines (201 loc) · 4.95 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
package main
import (
cfg "./configfile"
"reflect"
"strconv"
"bytes"
"path/filepath"
"fmt"
"os"
"io"
)
//-------------------------------------------------------------------------
// Config
//
// Structure represents persistent config storage of the gocode daemon. Usually
// the config is located somewhere in ~/.config/gocode directory.
//-------------------------------------------------------------------------
var Config = struct {
ProposeBuiltins bool "propose-builtins"
LibPath string "lib-path"
}{
false,
"",
}
func setValue(v reflect.Value, name, value string) {
switch t := v; t.Kind() {
case reflect.Bool:
v, ok := cfg.BoolStrings[value]
if ok {
t.SetBool(v)
}
case reflect.String:
t.SetString(value)
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
v, err := strconv.Atoi64(value)
if err == nil {
t.SetInt(v)
}
case reflect.Float32, reflect.Float64:
v, err := strconv.Atof64(value)
if err == nil {
t.SetFloat(v)
}
}
}
func listValue(v reflect.Value, name string, w io.Writer) {
switch t := v; t.Kind() {
case reflect.Bool:
fmt.Fprintf(w, "%s = %v\n", name, t.Bool())
case reflect.String:
fmt.Fprintf(w, "%s = \"%v\"\n", name, t.String())
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
fmt.Fprintf(w, "%s = %v\n", name, t.Int())
case reflect.Float32, reflect.Float64:
fmt.Fprintf(w, "%s = %v\n", name, t.Float())
}
}
func listConfig(v interface{}) string {
str, typ, ok := interfaceIsPtrStruct(v)
if !ok {
return ""
}
buf := bytes.NewBuffer(make([]byte, 0, 256))
for i := 0; i < str.NumField(); i++ {
v := str.Field(i)
name := typ.Field(i).Tag
listValue(v, name, buf)
}
return buf.String()
}
func listOption(v interface{}, name string) string {
str, typ, ok := interfaceIsPtrStruct(v)
if !ok {
return ""
}
buf := bytes.NewBuffer(make([]byte, 0, 256))
for i := 0; i < str.NumField(); i++ {
v := str.Field(i)
nm := typ.Field(i).Tag
if nm == name {
listValue(v, name, buf)
}
}
return buf.String()
}
func setOption(v interface{}, name, value string) string {
str, typ, ok := interfaceIsPtrStruct(v)
if !ok {
return ""
}
buf := bytes.NewBuffer(make([]byte, 0, 256))
for i := 0; i < str.NumField(); i++ {
v := str.Field(i)
nm := typ.Field(i).Tag
if nm == name {
setValue(v, name, value)
listValue(v, name, buf)
}
}
writeConfig(v)
return buf.String()
}
func interfaceIsPtrStruct(v interface{}) (reflect.Value, reflect.Type, bool) {
ptr := reflect.ValueOf(v)
ok := ptr.Kind() == reflect.Ptr
if !ok {
return reflect.Value{}, nil, false
}
str := ptr.Elem()
if str.Kind() != reflect.Struct {
return reflect.Value{}, nil, false
}
typ := str.Type()
return str, typ, true
}
func writeValue(v reflect.Value, name string, c *cfg.ConfigFile) {
switch v.Kind() {
case reflect.Bool, reflect.String,
reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Float32, reflect.Float64:
c.AddOption(cfg.DefaultSection, name, fmt.Sprint(v.Interface()))
}
}
func readValue(v reflect.Value, name string, c *cfg.ConfigFile) {
if !c.HasOption(cfg.DefaultSection, name) {
return
}
switch t := v; t.Kind() {
case reflect.Bool:
v, err := c.GetBool(cfg.DefaultSection, name)
if err == nil {
t.SetBool(v)
}
case reflect.String:
v, err := c.GetString(cfg.DefaultSection, name)
if err == nil {
t.SetString(v)
}
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
v, err := c.GetInt(cfg.DefaultSection, name)
if err == nil {
t.SetInt(int64(v))
}
case reflect.Float32, reflect.Float64:
v, err := c.GetFloat(cfg.DefaultSection, name)
if err == nil {
t.SetFloat(float64(v))
}
}
}
func writeConfig(v interface{}) os.Error {
const errstr = "WriteConfig expects a pointer to a struct value as an argument"
str, typ, ok := interfaceIsPtrStruct(v)
if !ok {
return os.NewError(errstr)
}
c := cfg.NewConfigFile()
for i := 0; i < str.NumField(); i++ {
v := str.Field(i)
name := typ.Field(i).Tag
writeValue(v, name, c)
}
makeSureConfigDirExists()
err := c.WriteConfigFile(configFile(), 0644, "gocode config file")
if err != nil {
return err
}
return nil
}
func readConfig(v interface{}) os.Error {
c, err := cfg.ReadConfigFile(configFile())
if err != nil {
return err
}
const errstr = "ReadConfig expects a pointer to a struct value as an argument"
str, typ, ok := interfaceIsPtrStruct(v)
if !ok {
return os.NewError(errstr)
}
for i := 0; i < str.NumField(); i++ {
v := str.Field(i)
name := typ.Field(i).Tag
readValue(v, name, c)
}
return nil
}
func xdgHomeDir() string {
xdghome := os.Getenv("XDG_CONFIG_HOME")
if xdghome == "" {
xdghome = filepath.Join(os.Getenv("HOME"), ".config")
}
return xdghome
}
func makeSureConfigDirExists() {
dir := filepath.Join(xdgHomeDir(), "gocode")
if !fileExists(dir) {
os.MkdirAll(dir, 0755)
}
}
func configFile() string {
return filepath.Join(xdgHomeDir(), "gocode", "config.ini")
}