-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathloader.go
More file actions
50 lines (41 loc) · 1.24 KB
/
loader.go
File metadata and controls
50 lines (41 loc) · 1.24 KB
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
package serum
import (
"fmt"
"github.com/wingocard/serum/internal/envparser"
)
// A Loader loads env variables from a source into an Injector and prepares
// them to be injected using the the Inject method.
type Loader interface {
Load(ij *Injector) error
}
// LoaderFunc is an adapter type that allows an ordinary function
// to be used as a loader.
type LoaderFunc func(ij *Injector) error
// Load implements the loader interface.
func (f LoaderFunc) Load(ij *Injector) error {
return f(ij)
}
// FromFile returns a loader that will parse a .env file for key/value pairs and
// assign them to an Injector.
func FromFile(path string) Loader {
return LoaderFunc(func(ij *Injector) error {
envVars, err := envparser.ParseFile(path)
if err != nil {
return fmt.Errorf("error loading env vars from file: %w", err)
}
ij.envVars = envVars
return nil
})
}
// FromEnv returns a loader that will parse the current process' environment for
// the specified keys and assigns them to an Injector.
func FromEnv(keys []string) Loader {
return LoaderFunc(func(ij *Injector) error {
envVars, err := envparser.ParseEnv(keys)
if err != nil {
return fmt.Errorf("error loading env vars from env: %s", err)
}
ij.envVars = envVars
return nil
})
}