-
Notifications
You must be signed in to change notification settings - Fork 12
/
loader.go
63 lines (54 loc) · 1.76 KB
/
loader.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
62
63
/*
Copyright © 2024 Acronis International GmbH.
Released under MIT license.
*/
package config
import (
"io"
)
// Loader loads configuration values from data provider (with initializing default values before)
// and sets them in configuration objects.
type Loader struct {
DataProvider DataProvider
}
// NewDefaultLoader creates a new configurations loader with an ability to read values from the environment variables.
func NewDefaultLoader(envVarsPrefix string) *Loader {
va := NewViperAdapter()
va.UseEnvVars(envVarsPrefix)
return NewLoader(va)
}
// NewLoader creates a new configurations' loader.
func NewLoader(dp DataProvider) *Loader {
return &Loader{dp}
}
// LoadFromFile loads configuration values from file and sets them in configuration objects.
func (l *Loader) LoadFromFile(path string, dataType DataType, cfg Config, cfgs ...Config) error {
if err := l.DataProvider.SetFromFile(path, dataType); err != nil {
return err
}
return l.load(append([]Config{cfg}, cfgs...))
}
// LoadFromReader loads configuration values from reader and sets them in configuration objects.
func (l *Loader) LoadFromReader(reader io.Reader, dataType DataType, cfg Config, cfgs ...Config) error {
if err := l.DataProvider.SetFromReader(reader, dataType); err != nil {
return err
}
return l.load(append([]Config{cfg}, cfgs...))
}
func (l *Loader) load(cfgs []Config) error {
dpForCfg := func(cfg Config) DataProvider {
if kpHolder, ok := cfg.(KeyPrefixProvider); ok && kpHolder.KeyPrefix() != "" {
return NewKeyPrefixedDataProvider(l.DataProvider, kpHolder.KeyPrefix())
}
return l.DataProvider
}
for _, cfg := range cfgs {
cfg.SetProviderDefaults(dpForCfg(cfg))
}
for _, cfg := range cfgs {
if err := cfg.Set(dpForCfg(cfg)); err != nil {
return err
}
}
return nil
}