-
Notifications
You must be signed in to change notification settings - Fork 14
/
configreaderbuilder.go
85 lines (75 loc) · 2.73 KB
/
configreaderbuilder.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package config
import (
"log"
"strings"
"github.com/spf13/afero"
"github.com/spf13/viper"
)
// ConfigReaderBuilder exposes the builder api for configReaderImpl.
// Use NewConfigReaderBuilder() and AttachEnvPrefix() to Build a ConfigReaderBuilder. Follow it up one or more calls
// to WithConfigFile() and/or WithConfigName() and finally use Build() to Build the configReaderImpl.
type ConfigReaderBuilder struct { // nolint:golint
evarReader configReaderImpl
}
// NewConfigReaderBuilder builds a new ConfigReaderBuilder.
func NewConfigReaderBuilder() ConfigReaderBuilder {
b := ConfigReaderBuilder{
evarReader: configReaderImpl{
envVars: viper.New(),
},
}
return b
}
// AttachEnvPrefix attaches appName as prefix.
func (b ConfigReaderBuilder) AttachEnvPrefix(appName string) ConfigReaderBuilder {
b.evarReader.envVars.SetEnvPrefix(appName)
b.evarReader.envVars.AutomaticEnv()
b.evarReader.envVars.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
return b
}
// WithConfigFile attaches the passed config file.
func (b ConfigReaderBuilder) WithConfigFile(configFile string) ConfigReaderBuilder {
b.evarReader.envVars.SetConfigFile(configFile)
if err := b.evarReader.envVars.MergeInConfig(); err != nil {
log.Fatalln(err)
}
return b
}
// WithConfigName attaches the passed config path and name.
func (b ConfigReaderBuilder) WithConfigName(configName string, configPath ...string) ConfigReaderBuilder {
b.evarReader.envVars.SetConfigName(configName)
for _, path := range configPath {
b.evarReader.envVars.AddConfigPath(path)
}
if err := b.evarReader.envVars.MergeInConfig(); err != nil {
log.Fatalln(err)
}
return b
}
// WithFs attaches the file system to use.
func (b ConfigReaderBuilder) WithFs(fs afero.Fs) ConfigReaderBuilder {
b.evarReader.envVars.SetFs(fs)
return b
}
// WithStrictMode controls if ConfigReader.Unmarshal handles unknown
// keys. If strict mode is false (the default), config keys with no
// corresponding config field are ignored. If strict mode is true,
// any config key with no corresponding config field will be regarded
// as a decoding error that will cause Unmarshal to return an error.
//
// Also, optionally, a list of keys to ignore and exclude from strict
// mode checking can be provided. Beware, there's some subtleties
// to how ignored keys must be named, see the comments inside
// configreaderimpl.go for details.
func (b ConfigReaderBuilder) WithStrictMode(strict bool, ignoredKeys ...string) ConfigReaderBuilder {
b.evarReader.strictMode = strict
b.evarReader.strictModeIgnoredKeys = ignoredKeys
return b
}
// Build Builds and returns the ConfigReader.
func (b ConfigReaderBuilder) Build() ConfigReader {
if err := b.evarReader.envVars.ReadInConfig(); err != nil {
log.Fatalln(err)
}
return b.evarReader
}