-
Notifications
You must be signed in to change notification settings - Fork 1
/
build.go
126 lines (109 loc) · 3.38 KB
/
build.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
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
package bviper
import (
"container/list"
"strings"
"github.com/go-masonry/mortar/interfaces/cfg"
"github.com/spf13/viper"
)
const (
// EnvKeyDelimiterFrom shell environment delimiter
EnvKeyDelimiterFrom = "_"
// EnvKeyDelimiterTo delimiter used by application
EnvKeyDelimiterTo = "."
)
type viperConfig struct {
envDelimiterFrom, envDelimiterTo string
mainConfigFilePath string
extraConfigFilePaths []string
}
type defaultViperBuilder struct {
ll *list.List
}
// Builder creates a simple viper instance with a given config file path
// configFilePath should point to a file that have one of these extensions:
//
// "json", "toml", "yaml", "yml", "properties", "props", "prop", "hcl", "dotenv", "env", "ini"
func Builder() cfg.Builder {
return &defaultViperBuilder{
ll: list.New(),
}
}
func (vb *defaultViperBuilder) SetConfigFile(path string) cfg.Builder {
vb.ll.PushBack(func(cfg *viperConfig) {
cfg.mainConfigFilePath = path
})
return vb
}
func (vb *defaultViperBuilder) AddExtraConfigFile(path string) cfg.Builder {
vb.ll.PushBack(func(cfg *viperConfig) {
cfg.extraConfigFilePaths = append(cfg.extraConfigFilePaths, path)
})
return vb
}
func (vb *defaultViperBuilder) SetEnvDelimiterReplacer(from, to string) cfg.Builder {
vb.ll.PushBack(func(cfg *viperConfig) {
if len(from) > 0 && len(to) > 0 {
cfg.envDelimiterFrom = from
cfg.envDelimiterTo = to
}
})
return vb
}
func (vb *defaultViperBuilder) Build() (cfg.Config, error) {
// apply options
viperCfg := &viperConfig{
envDelimiterFrom: EnvKeyDelimiterFrom,
envDelimiterTo: EnvKeyDelimiterTo,
}
for e := vb.ll.Front(); e != nil; e = e.Next() {
f := e.Value.(func(cfg *viperConfig))
f(viperCfg)
}
// build instance with multiple files
viperWithOptions := viper.NewWithOptions(
viper.EnvKeyReplacer(strings.NewReplacer(viperCfg.envDelimiterTo, viperCfg.envDelimiterFrom)), // yeah not the right order...
)
viperWithOptions.AutomaticEnv()
// TODO consider having an ENV variable with File path
if len(viperCfg.mainConfigFilePath) > 0 {
files := append([]string{viperCfg.mainConfigFilePath}, viperCfg.extraConfigFilePaths...)
for _, file := range files {
viperWithOptions.SetConfigFile(file)
if err := viperWithOptions.MergeInConfig(); err != nil {
return nil, err
}
}
}
return &viperWrapper{
instance: viperWithOptions,
}, nil
}
// CustomBuilder allows one to use an already initialized and configure Viper instance
//
// Note:
// It's important to receive a viper instance that is already initialized using viper.ReadInConfig() or viper.ReadConfig(in io.Reader) or viper.ReadRemoteConfig()
// Also note that all other Builder function will do nothing since we assume this instance is configured properly
func CustomBuilder(custom *viper.Viper) cfg.Builder {
return &customViperBuilder{
viperInstance: custom,
}
}
type customViperBuilder struct {
viperInstance *viper.Viper
}
func (cvb *customViperBuilder) SetConfigFile(path string) cfg.Builder {
return cvb
}
func (cvb *customViperBuilder) AddExtraConfigFile(path string) cfg.Builder {
return cvb
}
func (cvb *customViperBuilder) SetEnvDelimiterReplacer(from, to string) cfg.Builder {
return cvb
}
func (cvb *customViperBuilder) Build() (cfg.Config, error) {
return &viperWrapper{
instance: cvb.viperInstance,
}, nil
}
var _ cfg.Builder = (*customViperBuilder)(nil)
var _ cfg.Builder = (*defaultViperBuilder)(nil)