32 lines
775 B
Go
Raw Normal View History

2024-12-17 22:28:48 +08:00
package myViper
import (
"fmt"
"github.com/spf13/viper"
)
type SimpleViper struct {
config interface{}
configType string
configName string
configPath string
}
func NewSimpleViper(config interface{}, configType string, configName string, configPath string) *SimpleViper {
return &SimpleViper{config: config, configType: configType, configName: configName, configPath: configPath}
}
func (t *SimpleViper) Apply() {
v := viper.New()
v.SetConfigFile(fmt.Sprintf("%s/%s.yaml", t.configPath, t.configName))
v.SetConfigType(t.configType)
err := v.ReadInConfig()
if err != nil {
panic(fmt.Errorf("Fatal error config file: %s \n", err))
}
if err := v.Unmarshal(t.config); err != nil {
panic(fmt.Errorf("Fatal Unmarshal error config file: %s \n", err))
}
}