-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathparser.go
191 lines (165 loc) · 4.03 KB
/
parser.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
package dotenv
import (
"fmt"
"os"
"regexp"
"strings"
)
const (
prefixSingleQuote = '\''
prefixDoubleQuote = '"'
)
var (
escapeRegex = regexp.MustCompile(`\\.`)
unescapeCharsRegex = regexp.MustCompile(`\\([^$])`)
)
// Decoder decodes the contents of an env file into a map.
type Decoder interface {
Decode(b []byte, v map[string]any) error
}
// DefaultDecoder is the default decoder used by the library.
type DefaultDecoder struct {
line int
}
// Decode decodes the contents of b into v.
func (d *DefaultDecoder) Decode(b []byte, v map[string]any) error {
data := string(b)
lines := strings.Split(data, "\n")
var curKey, curVal string
var curQuote byte
for _, line := range lines {
d.line++
if curQuote == 0 {
// not in a quoted value block
line = strings.TrimSpace(line)
// Skip empty lines and comments
if line == "" || line[0] == '#' {
continue
}
// find the first occurrence of an equal sign or colon
key, val, ok := strings.Cut(line, "=")
if !ok {
key, val, ok = strings.Cut(line, ":")
// TODO: support inherited variables
}
key = strings.TrimSpace(key)
if !strings.HasPrefix(key, "export ") && strings.Contains(key, " ") {
return fmt.Errorf("line %d: key cannot contain spaces", d.line)
}
val = strings.TrimSpace(val)
// check if the value is quoted
quote, isQuoted := isPrefixQuoted(val)
if isQuoted {
// get the value without the quotes
// if the value is quoted, check if it's a multi-line value
idx := d.findTerminator(val[1:], quote)
if idx == -1 {
// if the value is not terminated, continue to the next line
curKey = key
curVal = val
curQuote = quote
continue
}
}
val = parseValue(val)
addEnv(key, val, v)
continue
}
// in a quoted value block
curVal += "\n" + line
if d.findTerminator(line, curQuote) == -1 {
continue
}
// value is terminated, parse and add to the environment
curVal = parseValue(curVal)
addEnv(curKey, curVal, v)
curKey, curVal, curQuote = "", "", 0
}
if curQuote != 0 {
return fmt.Errorf("line %d: unterminated quoted value", d.line)
}
return nil
}
// addEnv adds the key and value to the environment.
func addEnv(key, value string, v map[string]any) {
if strings.HasPrefix(key, "export ") {
_ = os.Setenv(key[7:], value)
return
}
v[strings.ToUpper(key)] = value
}
// findTerminator finds the terminator of a quote in a string
// and returns the index of the terminator.
func (d *DefaultDecoder) findTerminator(str string, quote byte) int {
previousCharIsEscape := false
for i := 0; i < len(str); i++ {
char := str[i]
if char == quote {
if !previousCharIsEscape {
return i
}
}
if !previousCharIsEscape && char == '\\' {
previousCharIsEscape = true
continue
}
if previousCharIsEscape {
previousCharIsEscape = false
continue
}
}
return -1
}
func parseValue(value string) string {
value = strings.TrimSpace(value)
if value == "" {
return ""
}
// remove comments but only if the value is not quoted
if !isQuoted(value) {
if i := strings.Index(value, "#"); i >= 0 {
value = value[:i]
}
}
// remove leading and trailing spaces
value = strings.TrimSpace(value)
if len(value) > 1 {
if quote, ok := isPrefixQuoted(value); ok {
// remove quotes
value = value[1 : len(value)-1]
if quote == prefixDoubleQuote {
value = escapeRegex.ReplaceAllStringFunc(value, func(s string) string {
c := strings.TrimPrefix(s, "\\")
switch c {
case "n":
return "\n"
case "r":
return "\r"
default:
return s
}
})
// unescape characters
value = unescapeCharsRegex.ReplaceAllString(value, "$1")
}
}
}
return value
}
func isPrefixQuoted(s string) (byte, bool) {
if s == "" {
return 0, false
}
switch quote := s[0]; quote {
case prefixDoubleQuote, prefixSingleQuote:
return quote, true
default:
return 0, false
}
}
func isQuoted(s string) bool {
if len(s) < 2 {
return false
}
return s[0] == s[len(s)-1] && (s[0] == prefixDoubleQuote || s[0] == prefixSingleQuote)
}