-
Notifications
You must be signed in to change notification settings - Fork 0
/
envLoader.go
55 lines (48 loc) · 881 Bytes
/
envLoader.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
package envLoader
import (
"fmt"
"os"
"strings"
)
var (
WorkingDirectory string
Path = "/.env"
)
func getEnvFile() string {
if WorkingDirectory == "" {
wd, err := os.Getwd()
if err != nil {
panic(err)
}
WorkingDirectory = wd
}
envFile := WorkingDirectory + Path
fmt.Printf("[envLoader] -> reading from %s\n", envFile)
_, err := os.Stat(envFile)
if err != nil {
panic(err)
}
fileContent, err := os.ReadFile(envFile)
if err != nil {
panic(err)
}
return string(fileContent)
}
func Load() {
fileContent := getEnvFile()
environments := strings.Split(fileContent, "\n")
for _, env := range environments {
if strings.HasPrefix(env, "#") {
continue
}
if !strings.Contains(env, "=") {
continue
}
if env != "" {
e := strings.Split(env, "=")
if err := os.Setenv(e[0], e[1]); err != nil {
panic(err)
}
}
}
}