|
@@ -16,13 +16,26 @@ var loaders = map[string]func([]byte, interface{}) error{
|
|
|
".yml": LoadConfigFromYamlBytes,
|
|
|
}
|
|
|
|
|
|
-func LoadConfig(file string, v interface{}) error {
|
|
|
- if content, err := ioutil.ReadFile(file); err != nil {
|
|
|
+func LoadConfig(file string, v interface{}, opts ...Option) error {
|
|
|
+ content, err := ioutil.ReadFile(file)
|
|
|
+ if err != nil {
|
|
|
return err
|
|
|
- } else if loader, ok := loaders[path.Ext(file)]; ok {
|
|
|
+ }
|
|
|
+
|
|
|
+ loader, ok := loaders[path.Ext(file)]
|
|
|
+ if !ok {
|
|
|
+ return fmt.Errorf("unrecoginized file type: %s", file)
|
|
|
+ }
|
|
|
+
|
|
|
+ var opt options
|
|
|
+ for _, o := range opts {
|
|
|
+ o(&opt)
|
|
|
+ }
|
|
|
+
|
|
|
+ if opt.env {
|
|
|
return loader([]byte(os.ExpandEnv(string(content))), v)
|
|
|
} else {
|
|
|
- return fmt.Errorf("unrecoginized file type: %s", file)
|
|
|
+ return loader(content, v)
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -34,8 +47,8 @@ func LoadConfigFromYamlBytes(content []byte, v interface{}) error {
|
|
|
return mapping.UnmarshalYamlBytes(content, v)
|
|
|
}
|
|
|
|
|
|
-func MustLoad(path string, v interface{}) {
|
|
|
- if err := LoadConfig(path, v); err != nil {
|
|
|
+func MustLoad(path string, v interface{}, opts ...Option) {
|
|
|
+ if err := LoadConfig(path, v, opts...); err != nil {
|
|
|
log.Fatalf("error: config file %s, %s", path, err.Error())
|
|
|
}
|
|
|
}
|