generated from Fishwaldo/go-template
/
configregistry.go
50 lines (42 loc) · 1.02 KB
/
configregistry.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
package internal
import (
"github.com/pkg/errors"
"github.com/spf13/viper"
)
type configRegistryT struct {
parse func( *viper.Viper) (error)
validate func() ([]error, error)
}
var cfgReg map[string]configRegistryT
func init() {
cfgReg = make(map[string]configRegistryT)
}
func ConfigRegister(name string, parse func( *viper.Viper) (error), validate func() ([]error, error)) (error) {
_, found := cfgReg[name]
if (found) {
return errors.New("config Section Already Registered")
}
cfgReg[name] = configRegistryT{parse: parse, validate: validate}
return nil
}
func ConfigGetSections() []string {
var ret []string
for key := range cfgReg {
ret = append(ret, key)
}
return ret
}
func ConfigCallSection(name string, cfg *viper.Viper) (error) {
mod, found := cfgReg[name]
if !found {
return errors.New("No Config Section Registered")
}
return mod.parse(cfg)
}
func ConfigValidateSection(name string) (warnings []error, err error) {
mod, found := cfgReg[name]
if !found {
return nil, nil
}
return mod.validate()
}