-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathenv.go
More file actions
35 lines (31 loc) · 764 Bytes
/
Copy pathenv.go
File metadata and controls
35 lines (31 loc) · 764 Bytes
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
package main
import (
"fmt"
"os"
"strings"
)
// LoadEnv loads environment variables from a file.
// Lines starting with # or ; are treated as comments and skipped.
// Blank lines are skipped. Keys preserve their original case.
func LoadEnv(fn string) ([]string, error) {
buf, err := os.ReadFile(fn)
if err != nil {
return nil, err
}
lines := strings.Split(string(buf), "\n")
env := []string{}
for _, x := range lines {
x = strings.TrimSpace(x)
if x == "" || strings.HasPrefix(x, "#") || strings.HasPrefix(x, ";") {
continue
}
if !strings.Contains(x, "=") {
continue
}
a := strings.SplitN(x, "=", 2)
k := strings.TrimSpace(a[0])
v := strings.TrimSpace(a[1])
env = append(env, fmt.Sprintf("%s=%s", k, v))
}
return env, nil
}