123456789101112131415161718192021222324252627282930 |
- package configx
- import (
- "github.com/rotisserie/eris"
- "github.com/spf13/viper"
- )
- type Config struct {
- Name string `toml:"name"`
- Port int `toml:"port"`
- Environment string `toml:"environment"`
- }
- type LoadOption func() error
- func LoadConfig(file string, opts ...LoadOption) error {
- viper.SetConfigFile(file)
- viper.SetConfigType("toml")
- if err := viper.ReadInConfig(); err != nil {
- panic(eris.Wrap(err, "无法加载配置"))
- }
- var err error
- for _, opt := range opts {
- if err = opt(); err != nil {
- return err
- }
- }
- return nil
- }
|