mycommon/myconf/conf.go

39 lines
602 B
Go

package myconf
import (
"fmt"
"github.com/spf13/pflag"
"github.com/spf13/viper"
)
var (
vp = viper.New()
)
func LoadFlag() {
if !pflag.Parsed() {
pflag.Parse()
}
err := vp.BindPFlags(pflag.CommandLine)
if err != nil {
panic(fmt.Errorf("load command line fail: %s", err))
}
}
func LoadFile(confFile string, conf interface{}) {
vp.SetConfigFile(confFile)
err := vp.ReadInConfig()
if err != nil {
panic(fmt.Errorf("read file fail: %s", err))
}
err = vp.Unmarshal(conf)
if err != nil {
panic(fmt.Errorf("parse file fail: %s", err))
}
}
func Conf() *viper.Viper {
return vp
}