|
|
|
package conf
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
|
|
|
|
"github.com/tal-tech/go-zero/core/mapping"
|
|
|
|
)
|
|
|
|
|
|
|
|
var loaders = map[string]func([]byte, interface{}) error{
|
|
|
|
".json": LoadConfigFromJsonBytes,
|
|
|
|
".yaml": LoadConfigFromYamlBytes,
|
|
|
|
".yml": LoadConfigFromYamlBytes,
|
|
|
|
}
|
|
|
|
|
|
|
|
func LoadConfig(file string, v interface{}, opts ...Option) error {
|
|
|
|
content, err := ioutil.ReadFile(file)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
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 loader(content, v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func LoadConfigFromJsonBytes(content []byte, v interface{}) error {
|
|
|
|
return mapping.UnmarshalJsonBytes(content, v)
|
|
|
|
}
|
|
|
|
|
|
|
|
func LoadConfigFromYamlBytes(content []byte, v interface{}) error {
|
|
|
|
return mapping.UnmarshalYamlBytes(content, v)
|
|
|
|
}
|
|
|
|
|
|
|
|
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())
|
|
|
|
}
|
|
|
|
}
|