-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
52 lines (47 loc) · 978 Bytes
/
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
package config
import (
"bufio"
"encoding/json"
"fmt"
"os"
"strings"
)
func Unmarshal(filePath string, v interface{}) (err error) {
jsonStr, err := unmarshal(filePath)
if err != nil {
return
}
err = json.Unmarshal([]byte(jsonStr), v)
return
}
func unmarshal(filePath string) (jsonStr string, err error) {
jsonStr, err = readFile(filePath)
if err != nil {
return
}
return
}
func readFile(filePath string) (jsonStr string, err error) {
f, err := os.Open(filePath)
if err != nil {
println("os open err", err)
return
}
defer f.Close()
tmpJson := []string{}
scanner := bufio.NewScanner(f)
for scanner.Scan() {
str := scanner.Text()
if strings.HasPrefix(str, "#") {
continue
}
if strings.TrimSpace(str) == ""{
continue
}
s := strings.Split(scanner.Text(), ":")
tmp := fmt.Sprintf("\"%s\":%s", s[0], strings.Join(s[1:], ":"))
tmpJson = append(tmpJson, tmp)
}
jsonStr = fmt.Sprintf("{%s}", strings.Join(tmpJson, ","))
return
}