Compare commits

..

No commits in common. "85d75101130c15f3fa5936db3ae67eab5f98b8ed" and "dd33153ca1cff19fb77c84ad30d6bc9822562d36" have entirely different histories.

3 changed files with 43 additions and 38 deletions

2
go.mod
View File

@ -8,7 +8,6 @@ require (
github.com/go-playground/validator/v10 v10.11.1 github.com/go-playground/validator/v10 v10.11.1
github.com/go-redis/redis/v8 v8.11.5 github.com/go-redis/redis/v8 v8.11.5
github.com/json-iterator/go v1.1.12 github.com/json-iterator/go v1.1.12
github.com/spf13/pflag v1.0.5
github.com/spf13/viper v1.14.0 github.com/spf13/viper v1.14.0
github.com/stretchr/testify v1.8.1 github.com/stretchr/testify v1.8.1
go.uber.org/zap v1.24.0 go.uber.org/zap v1.24.0
@ -37,6 +36,7 @@ require (
github.com/spf13/afero v1.9.3 // indirect github.com/spf13/afero v1.9.3 // indirect
github.com/spf13/cast v1.5.0 // indirect github.com/spf13/cast v1.5.0 // indirect
github.com/spf13/jwalterweatherman v1.1.0 // indirect github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/subosito/gotenv v1.4.2 // indirect github.com/subosito/gotenv v1.4.2 // indirect
go.uber.org/atomic v1.10.0 // indirect go.uber.org/atomic v1.10.0 // indirect
go.uber.org/multierr v1.9.0 // indirect go.uber.org/multierr v1.9.0 // indirect

View File

@ -4,63 +4,43 @@ import (
"fmt" "fmt"
"github.com/spf13/pflag" "github.com/spf13/pflag"
"github.com/spf13/viper" "github.com/spf13/viper"
"log"
) )
type Config struct {
*viper.Viper
}
var ( var (
conf = &Config{Viper: viper.New()} vp = viper.New()
) )
// 加载命令行参数
func LoadFlag() { func LoadFlag() {
if !pflag.Parsed() { if !pflag.Parsed() {
pflag.Parse() pflag.Parse()
} }
err := conf.BindPFlags(pflag.CommandLine) err := vp.BindPFlags(pflag.CommandLine)
if err != nil { if err != nil {
panic(fmt.Errorf("load command line fail: %s", err)) panic(fmt.Errorf("load command line fail: %s", err))
} }
} }
// 指定文件加载
func LoadFile(confFile string) { func LoadFile(confFile string) {
log.Printf("read conf file: %s", confFile) vp.SetConfigFile(confFile)
err := vp.ReadInConfig()
conf.SetConfigFile(confFile)
err := conf.ReadInConfig()
if err != nil { if err != nil {
panic(fmt.Errorf("read file fail: %s", err)) panic(fmt.Errorf("read file fail: %s", err))
} }
} }
func Conf() *Config { func LoadFileTo(confFile string, conf interface{}) {
return conf vp.SetConfigFile(confFile)
err := vp.ReadInConfig()
if err != nil {
panic(fmt.Errorf("read file fail: %s", err))
} }
func (c *Config) GetStringDefault(key, defaultVal string) string { err = vp.Unmarshal(conf)
v := c.GetString(key) if err != nil {
if v == "" { panic(fmt.Errorf("parse file fail: %s", err))
return defaultVal
} }
return v
} }
func (c *Config) GetIntDefault(key string, defaultVal int) int { func Conf() *viper.Viper {
v := c.GetString(key) // 未设置 空字符串 return vp
if v == "" {
return defaultVal
}
return c.GetInt(key)
}
func (c *Config) GetBoolDefault(key string, defaultVal bool) bool {
v := c.GetString(key) // 未设置 空字符串
if v == "" {
return defaultVal
}
return c.GetBool(key)
} }

25
myconf/conf_test.go Normal file
View File

@ -0,0 +1,25 @@
package myconf
import (
"fmt"
"testing"
)
func TestConf(t *testing.T) {
type Config struct {
App struct {
Name string
Addr string
Port int
}
Redis struct {
Addr string
Db int
}
}
var config = &Config{}
LoadFile("test.yaml", config)
fmt.Printf("%+v\n", config)
}