-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathenv_default.go
110 lines (94 loc) · 1.84 KB
/
env_default.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
package configo
import (
"bufio"
"fmt"
"io"
"os"
"strings"
)
func envDefault(c *Config, f *os.File) error {
var ckey string
popn := false
r := bufio.NewReader(f)
for {
b, _, err := r.ReadLine()
if err != nil {
if err == io.EOF {
break
}
return err
}
var p interface{}
if process := envDefaultLine(&b, &p); process == PROCESS_COMMON {
ckey = p.(string)
if ckey != "" {
popn = true
(c.Configure).(Default)[ckey] = make(Property)
} else {
popn = false
}
} else if process == PROCESS_PROPERTY {
if popn {
prop := (p).(Property)
for k, v := range prop {
(c.Configure).(Default)[ckey][k] = v
}
}
} else {
//skip
}
}
fmt.Println(c.Configure)
return nil
}
func envDefaultLine(line *[]byte, key *interface{}) int {
proc := string(*line)
//is common
proc = strings.TrimSpace(proc)
sta := strings.Index(proc, "[")
end := strings.LastIndex(proc, "]")
//check error
rlt := PROCESS_NONE
strarr := []string{"#", "//"}
//get group name
if sta >= 0 && end >= 0 {
*key = ""
if sta+1 < end {
*key = proc[sta+1 : end]
}
rlt = PROCESS_COMMON
goto LAST
}
//delete comment
for _, v := range strarr {
if idx := strings.Index(proc, v); idx > 0 {
proc = strings.TrimSpace(strings.Split(proc, v)[0])
break
} else if idx == 0 {
goto LAST
} else {
}
}
//string splite to map
if mp, err := envStringSplit(proc, "="); err == nil {
*key = mp
rlt = PROCESS_PROPERTY
}
LAST:
return rlt
}
func envStringSplit(s string, sep string) (Property, error) {
ss := strings.Split(s, sep)
if len(ss) == 2 {
key := strings.TrimSpace(ss[0])
val := strings.TrimSpace(ss[1])
return Property{key: val}, nil
}
return nil, ErrorSplitNoData
}
func envDefaultGet(s string) *Property {
if v, ok := (config.Configure).(Default)[s]; ok {
return &v
}
return nil
}