-
Notifications
You must be signed in to change notification settings - Fork 0
/
dotenv.go
56 lines (53 loc) · 1.09 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
package dotenv
import (
"bufio"
"io"
"os"
)
// Read reads the .env file and returns the values as a map.
func Read(path string) (map[string]string, error) {
result := make(map[string]string)
file, err := os.Open(path)
if err != nil {
return nil, err
}
reader := bufio.NewReader(file)
for {
key, err := reader.ReadBytes('=')
if err != nil {
break
}
value, err := reader.ReadBytes('\n')
if err != nil {
if err == io.EOF {
result[string(key[:len(key)-1])] = string(value)
}
break
}
result[string(key[:len(key)-1])] = string(value[:len(value)-1])
}
return result, nil
}
// Apply reads the .env file and sets the values in the environment.
func Apply(path string) error {
file, err := os.Open(path)
if err != nil {
return err
}
reader := bufio.NewReader(file)
for {
key, err := reader.ReadBytes('=')
if err != nil {
break
}
value, err := reader.ReadBytes('\n')
if err != nil {
if err == io.EOF {
os.Setenv(string(key[:len(key)-1]), string(value))
}
break
}
os.Setenv(string(key[:len(key)-1]), string(value[:len(value)-1]))
}
return nil
}