-
Notifications
You must be signed in to change notification settings - Fork 0
/
snuffler.go
61 lines (53 loc) · 1.88 KB
/
snuffler.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
61
package snuffler
import (
"path/filepath"
)
// Snuffler is the primary object holding all of the bits required to snuffle
// through config files.
type Snuffler struct {
conf interface{}
filePatterns []filePattern
files []*configFile
}
// AddFile accepts a string containing a filename and adds it to the list of
// files that the snuffler should load. This file must exist. If you want to
// add a file that may or may not exist, use MaybeAddFile.
func (s *Snuffler) AddFile(p string) error {
return s.addFile(p)
}
// MaybeAddFile accepts a string containing a filename and adds it to the list
// of files that the snuffler should load. This file need not exist. If you
// want to add a file that must exist, use AddFile.
func (s *Snuffler) MaybeAddFile(p string) {
s.addFile(p)
}
// AddGlob accepts a string containing a Glob[0] to search for config files.
//
// [0]: https://golang.org/pkg/path/filepath/#Glob
func (s *Snuffler) AddGlob(g string) {
matches, _ := filepath.Glob(g)
for _, match := range matches {
s.addFile(match)
}
}
// Snuffle performs the noble task of snuffling through all of the specified
// config files and paths to populate the provided config object. Files are
// loaded in the order they were received, and values are overwritten if
// subsequent files specify them.
func (s *Snuffler) Snuffle() error {
return s.unmarshalFiles(s.conf)
}
// Snorfle performs all the same tasks as Snuffle, but does not use the stored
// config object reference, instead populating the one that is provided as an
// argument.
func (s *Snuffler) Snorfle(conf interface{}) error {
return s.unmarshalFiles(conf)
}
// New creates a new snuffler object with the given interface. You can then
// add files to the resulting snuffler and, when run, it will load the config
// from each of them into the interface.
func New(c interface{}) *Snuffler {
return &Snuffler{
conf: c,
}
}