-
Notifications
You must be signed in to change notification settings - Fork 0
/
vars.go
185 lines (147 loc) · 2.9 KB
/
vars.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
package config
import (
"bufio"
"bytes"
"fmt"
"io"
"strings"
"unicode"
)
func parseVars(r io.Reader, vars map[string]string) error {
reader := bufio.NewReader(r)
var name, value []byte
atName := true
atValue := false
atComment := false
for {
r, _, err := reader.ReadRune()
if err != nil {
if err == io.EOF {
if atValue {
vars[string(name)] = varValue(value)
}
break
}
return fmt.Errorf("config: cannot read from input (%s)", err)
}
if r == '#' {
if atValue {
vars[string(name)] = varValue(value)
}
name = nil
value = nil
atName = false
atValue = false
atComment = true
continue
}
if r == '\n' || r == '\r' {
if atValue {
vars[string(name)] = varValue(value)
}
name = nil
value = nil
atName = true
atValue = false
if atComment {
atComment = false
continue
}
continue
}
if atComment {
continue
}
if r == '=' {
if atValue {
value = append(value, byte(r))
}
atName = false
atValue = true
continue
}
if atName {
if unicode.IsSpace(r) {
continue
}
name = append(name, byte(r))
continue
}
if atValue {
value = append(value, byte(r))
}
}
return nil
}
func varValue(v []byte) string {
return string(bytes.Trim(bytes.TrimSpace(v), `"'`))
}
func interpolateVars(vars map[string]string) {
for k, v := range vars {
if strings.IndexByte(v, '$') == -1 {
continue
}
atVar := false
var name []byte
var newValue []byte
for i := 0; i < len(v); i++ {
// Variable starts
if v[i] == '$' {
atVar = isAtVar(v, i)
if i == len(v)-1 && i-1 >= 0 && v[i-1] != '\\' {
newValue = append(newValue, v[i])
}
if atVar {
continue
}
}
if !atVar {
if nextVarIsDoubleEscaped(v, i) {
newValue = append(newValue, v[i])
continue
}
if nextVarIsEscaped(v, i) {
continue
}
newValue = append(newValue, v[i])
continue
}
if atVar && (v[i] == '{' || v[i] == '}') {
continue
}
if unicode.IsSpace(rune(v[i])) {
newValue = append(newValue, []byte(vars[string(name)])...)
newValue = append(newValue, v[i])
name = nil
atVar = false
continue
}
name = append(name, v[i])
}
if atVar {
newValue = append(newValue, []byte(vars[string(name)])...)
}
vars[k] = string(newValue)
}
}
func isAtVar(v string, i int) bool {
atVar := true
// Variable is escaped
if i-1 >= 0 && v[i-1] == '\\' {
atVar = false
}
// Variable is double escaped
if i-2 > 0 && v[i-2] == '\\' {
atVar = true
}
if i+1 < len(v) && (unicode.IsSpace(rune(v[i+1])) || v[i+1] == '"' || v[i+1] == '\'') {
atVar = false
}
return atVar
}
func nextVarIsDoubleEscaped(v string, i int) bool {
return v[i] == '\\' && i+1 < len(v) && v[i+1] == '\\' && i+2 < len(v) && v[i+2] == '$'
}
func nextVarIsEscaped(v string, i int) bool {
return v[i] == '\\' && i+1 < len(v) && v[i+1] == '$'
}