-
Notifications
You must be signed in to change notification settings - Fork 0
/
env.go
60 lines (50 loc) · 1.3 KB
/
env.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
package env
import (
"fmt"
"os"
"strings"
)
// Loader loads environment variables from a file.
type Loader struct {
setter func(string, string) error
getwd func() (string, error)
fileReader func(path string) (string, error)
}
// NewLoader creates a new Loader.
func NewLoader() *Loader {
return &Loader{
setter: os.Setenv,
getwd: os.Getwd,
fileReader: readOptionalFile,
}
}
// LoadOptional loads the environment variables from the given file.
// It ignores the file if it does not exist.
func (l *Loader) LoadOptional(file string) error {
file, err := resolveFilePath(file, l.getwd)
if err != nil {
return fmt.Errorf("failed to resolve file path: %w", err)
}
content, err := l.fileReader(file)
if err != nil {
return fmt.Errorf("failed to read file: %w", err)
}
return apply(content, l.setter)
}
// Apply loads the environment variables from the given content.
func (l *Loader) Apply(content string) error {
return apply(content, l.setter)
}
// apply sets the environment variables from the given content.
func apply(content string, setter func(key, value string) error) error {
for _, line := range strings.Split(content, "\n") {
key, value := parseLine(line)
if key == "" {
continue
}
if err := setter(key, value); err != nil {
return err
}
}
return nil
}