-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathloader.go
More file actions
72 lines (62 loc) · 1.58 KB
/
loader.go
File metadata and controls
72 lines (62 loc) · 1.58 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package config
import (
"io"
"os"
)
//Loader defines an entity that can generate a new Values instance.
type Loader interface {
Load() (*Values, error)
}
//ReaderFuncLoader is a func definition that takes in an io.Reader and returns
//a new Values instance and possible error.
type ReaderFuncLoader func(io.Reader) (*Values, error)
type fileFuncLoader struct {
rfl ReaderFuncLoader
paths []string
}
//NewFileFuncLoader creates a Loader that uses rfl to load and merge Values from
//from each file existing at each path in paths.
//If rfl returns an error for any path in paths then that error is immediately
//returned from Loader.Load() and Values will be nil.
func NewFileFuncLoader(rfl ReaderFuncLoader, paths ...string) Loader {
return &fileFuncLoader{
rfl: rfl,
paths: paths,
}
}
func (l *fileFuncLoader) Load() (*Values, error) {
values := NewValues()
for _, path := range l.paths {
temp, err := l.loadPath(path)
if err != nil {
return nil, err
}
values.Merge(NewKey(), temp)
}
return values, nil
}
func (l *fileFuncLoader) loadPath(path string) (*Values, error) {
file, err := os.Open(path)
if err != nil {
return nil, err
}
values, err := l.rfl(file)
if err != nil {
return nil, err
}
return values, file.Close()
}
type readerFuncLoader struct {
rfl ReaderFuncLoader
r io.Reader
}
//NewReaderFuncLoader creates a Loader that return rfl(r) in its Load() method.
func NewReaderFuncLoader(rfl ReaderFuncLoader, r io.Reader) Loader {
return &readerFuncLoader{
rfl: rfl,
r: r,
}
}
func (l *readerFuncLoader) Load() (*Values, error) {
return l.rfl(l.r)
}