-
Notifications
You must be signed in to change notification settings - Fork 14
/
config.go
65 lines (53 loc) · 2.04 KB
/
config.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
package config
import (
"context"
"github.com/anz-bank/sysl-go/validator"
)
type defaultConfigKey struct{}
type DefaultConfig struct {
Library LibraryConfig `yaml:"library" mapstructure:"library"`
// config used for setting up the sysl-go admin server
Admin *AdminConfig `yaml:"admin" mapstructure:"admin"`
GenCode GenCodeConfig `yaml:"genCode" mapstructure:"genCode"`
// development config can be used to set some config options only appropriate for dev/test environments.
Development *DevelopmentConfig `yaml:"development" mapstructure:"development"`
}
// GetDefaultConfig retrieves the externally-provided config from the context.
// The default config is injected into the server context during bootstrapping and can therefore
// be called from anywhere within the running application.
func GetDefaultConfig(ctx context.Context) *DefaultConfig {
m, _ := ctx.Value(defaultConfigKey{}).(*DefaultConfig)
return m
}
// PutDefaultConfig puts the externally-provided config into the given context, returning the new context.
func PutDefaultConfig(ctx context.Context, config *DefaultConfig) context.Context {
return context.WithValue(ctx, defaultConfigKey{}, config)
}
// LoadConfig reads and validates a configuration loaded from file.
// file: the path to the yaml-encoded config file
// defaultConfig: a pointer to the default config struct to populate
// customConfig: a pointer to the custom config struct to populate.
func LoadConfig(file string, defaultConfig *DefaultConfig, customConfig interface{}) error {
b := NewConfigReaderBuilder().WithConfigFile(file).WithDefaults(SetDefaults)
err := b.Build().Unmarshal(defaultConfig)
if err != nil {
return err
}
err = b.Build().Unmarshal(customConfig)
if err != nil {
return err
}
err = validator.Validate(defaultConfig)
if err != nil {
return err
}
err = validator.Validate(customConfig)
if err != nil {
return err
}
return err
}
func SetDefaults(setter func(key string, value interface{})) {
SetLibraryConfigDefaults("Library.", setter)
SetGenCodeConfigDefaults("GenCode.", setter)
}