package setting

import (
	"errors"
	"fengkuangGameServerJob/pkg/file"
	"github.com/spf13/viper"
)

type Setting struct {
	vp *viper.Viper
}

// NewSetting 实例化Setting
func NewSetting(path string) (*Setting, error) {
	if path == "" {
		path = "./conf/app.yaml"
	}
	if file.CheckNotExist(path) == true {
		return nil, errors.New("配置文件不存在")
	}
	vp := viper.New()
	vp.SetConfigFile(path)
	err := vp.ReadInConfig()
	if err != nil {
		return nil, err
	}
	return &Setting{vp}, nil
}

// ReadConf 读取配置
func (s *Setting) ReadConf(v interface{}) error {
	return s.vp.Unmarshal(&v)
}
