forked from goraz/onion
-
Notifications
You must be signed in to change notification settings - Fork 0
/
folder_layer.go
48 lines (40 loc) · 1.04 KB
/
folder_layer.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
package onion
import (
"fmt"
"path/filepath"
)
type folderLayer struct {
folder string
configName string
loaded bool
file Layer
}
func (fl *folderLayer) Load() (map[string]interface{}, error) {
if fl.loaded {
return fl.file.Load()
}
files, err := filepath.Glob(fl.folder + "/" + fl.configName + ".*")
if err != nil {
return nil, err
}
for i := range files {
// Try to load each file, until the first one is accepted
fl.file = NewFileLayer(files[i])
data, err := fl.file.Load()
if err == nil {
fl.loaded = true
return data, nil
}
}
return nil, fmt.Errorf("there is no supported file in %s", fl.folder)
}
// NewFolderLayer return a new folder layer, this layer search in a folder for
// all supported file, and when it hit the first loadable file then simply return it
// the config name must not contain file extension
func NewFolderLayer(folder, configName string) Layer {
// TODO : os specific seperator
if folder[len(folder)-1:] != "/" {
folder += "/"
}
return &folderLayer{folder, configName, false, nil}
}