-
Notifications
You must be signed in to change notification settings - Fork 0
/
dotenv.go
77 lines (62 loc) · 1.24 KB
/
dotenv.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
package dotenv
import (
"os"
)
// Reads your env file and loads it into ENV for this process.
//
// It overrides existing variables
func Load(filename string) error {
buf, err := os.ReadFile(filename)
if err != nil {
return err
}
var startName, endName, startValue, pos int
var parsingName = true
var insideQuotes = false
var quoteType byte
for pos < len(buf) {
if parsingName {
startName = pos
for pos < len(buf) && buf[pos] != '=' {
pos++
}
endName = pos
parsingName = false
pos++ // skip '='
} else {
if buf[pos] == '"' || buf[pos] == '\'' {
insideQuotes = true
quoteType = buf[pos]
pos++ // skip quote
}
startValue = pos
for pos < len(buf) {
if insideQuotes {
if buf[pos] == quoteType {
break
}
} else if buf[pos] == '\n' || buf[pos] == '\r' {
break
}
pos++
}
os.Setenv(string(buf[startName:endName]), string(buf[startValue:pos]))
pos++ // skip end byte
// skip whitespace bytes
for pos < len(buf) && isWhiteSpace(buf[pos]) {
pos++
}
parsingName = true
insideQuotes = false
}
}
return nil
}
func isWhiteSpace(b byte) bool {
switch b {
case '\t', '\n', '\v', '\f', '\r', ' ':
return true
default:
return false
}
}