-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtemp.go
56 lines (52 loc) · 1.28 KB
/
temp.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
// Reads the relative .env file with required environment variables
// Ensures the file doesn't include invalid characters
// For each listed variable, sets the key/value pair as an environment variable so it can be pulled
// into the project for use
package main
import (
"fmt"
"os"
"strings"
)
func loadDotEnv() error {
data, err := os.ReadFile(".env")
if err != nil {
return err
}
lines := strings.Split(string(data), "\n")
for _, line := range lines {
line = strings.TrimSpace(line)
if strings.HasPrefix(line, "#") {
continue
}
kvp := strings.SplitN(line, "=", 2)
if len(kvp) != 2 || kvp[0] == "" || strings.Contains(kvp[0], " ") || strings.Contains(kvp[0], "'") ||
strings.Contains(kvp[0], "\"") {
fmt.Printf("Warning: invalid line in .env: %s\n", line)
continue
}
key := kvp[0]
value := kvp[1]
if value[0] == '"' {
if value[len(value)-1] != '"' {
fmt.Printf("Warning: invalid line in .env: %s\n", line)
continue
}
value = value[1 : len(value)-1]
} else if value[0] == '\'' {
if value[len(value)-1] != '\'' {
fmt.Printf("Warning: invalid line in .env: %s\n", line)
continue
}
value = value[: len(value)-1]
}
os.Setenv(key, value)
}
return nil
}
func init() {
err := loadDotEnv()
if err != nil {
fmt.Println(err)
}
}