-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathloader.go
More file actions
156 lines (131 loc) · 3.1 KB
/
loader.go
File metadata and controls
156 lines (131 loc) · 3.1 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package confless
import (
"flag"
"fmt"
"os"
"path/filepath"
"strings"
"github.com/spf13/afero"
)
type configFile struct {
path string
format fileFormat
}
type loader struct {
fs afero.Fs
envReader func() []string
envPrefix string
flagSets []*flag.FlagSet
files []*configFile
}
// Detect the file format based on the extension.
func detectFileFormat(path string) fileFormat {
ext := filepath.Ext(path)
switch strings.ToLower(ext) {
case ".json":
return FileFormatJSON
case ".yaml", ".yml":
return FileFormatYAML
default:
return FileFormatJSON
}
}
// Creates a new loader with the given options.
func NewLoader(opts ...loaderOption) *loader {
l := &loader{
fs: afero.NewOsFs(),
envReader: os.Environ,
flagSets: make([]*flag.FlagSet, 0),
files: make([]*configFile, 0),
}
// Apply the given options.
for _, opt := range opts {
opt(l)
}
return l
}
// Register an environment variable prefix to load.
// Names are converted to dot-separated paths (e.g. "MY_FLAG" -> "my.flag").
func (l *loader) RegisterEnv(pre string) {
l.envPrefix = pre
}
// Register a file to load.
func (l *loader) RegisterFile(path string, opts ...fileOption) {
file := &configFile{
path: path,
format: detectFileFormat(path),
}
// Apply the given options.
for _, opt := range opts {
opt(file)
}
l.files = append(l.files, file)
}
// Register the flags to load.
// Names are converted to dot-separated paths (e.g. "my-flag" -> "my.flag").
// Note that flags must be parsed before loading.
func (l *loader) RegisterFlags(f *flag.FlagSet) {
l.flagSets = append(l.flagSets, f)
}
// Populate the object by applying the registered sources.
func (l *loader) Load(obj any) error {
// Load the files.
for _, file := range l.files {
// Open the file.
f, err := l.fs.Open(file.path)
if err != nil {
if os.IsNotExist(err) {
// Skip if file does not exist.
continue
}
return fmt.Errorf("failed to open file: %w", err)
}
// Populate the object by the file.
err = populateByFile(f, string(file.format), obj)
_ = f.Close()
if err != nil {
return fmt.Errorf("failed to load file: %w", err)
}
}
// Load the flags.
for _, fset := range l.flagSets {
err := populateByFlags(fset, obj)
if err != nil {
return fmt.Errorf("failed to load flags: %w", err)
}
}
// Load the environment variables.
if l.envPrefix != "" {
err := populateByEnv(l.envReader(), l.envPrefix, obj)
if err != nil {
return fmt.Errorf("failed to load env: %w", err)
}
}
// Load dynamically files.
for field, format := range findFileFields(obj) {
path := field.String()
if path == "" {
continue
}
format := fileFormat(format)
if format == "" {
format = detectFileFormat(path)
}
// Open the file.
f, err := l.fs.Open(path)
if err != nil {
if os.IsNotExist(err) {
// Skip if file does not exist.
continue
}
return fmt.Errorf("failed to open file: %w", err)
}
// Populate the object by the file.
err = populateByFile(f, string(format), obj)
_ = f.Close()
if err != nil {
return fmt.Errorf("failed to load file: %w", err)
}
}
return nil
}