update
							parent
							
								
									a5fe1f32db
								
							
						
					
					
						commit
						a2a8cf0024
					
				
							
								
								
									
										2
									
								
								go.mod
								
								
								
								
							
							
						
						
									
										2
									
								
								go.mod
								
								
								
								
							| 
						 | 
					@ -8,6 +8,7 @@ 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
 | 
				
			||||||
| 
						 | 
					@ -36,7 +37,6 @@ 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
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -2,27 +2,65 @@ package myconf
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import (
 | 
					import (
 | 
				
			||||||
	"fmt"
 | 
						"fmt"
 | 
				
			||||||
 | 
						"github.com/spf13/pflag"
 | 
				
			||||||
	"github.com/spf13/viper"
 | 
						"github.com/spf13/viper"
 | 
				
			||||||
 | 
						"log"
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					type Config struct {
 | 
				
			||||||
 | 
						*viper.Viper
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
var (
 | 
					var (
 | 
				
			||||||
	vp *viper.Viper
 | 
						conf = &Config{Viper: viper.New()}
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func Init(confPath string, conf interface{}) {
 | 
					// 加载命令行参数
 | 
				
			||||||
	vp = viper.New()
 | 
					func LoadFlag() {
 | 
				
			||||||
	vp.SetConfigFile(confPath)
 | 
						if !pflag.Parsed() {
 | 
				
			||||||
	err := vp.ReadInConfig()
 | 
							pflag.Parse()
 | 
				
			||||||
	if err != nil {
 | 
					 | 
				
			||||||
		panic(fmt.Errorf("读取配置文件失败 %s", err))
 | 
					 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
						err := conf.BindPFlags(pflag.CommandLine)
 | 
				
			||||||
	err = vp.Unmarshal(conf)
 | 
					 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		panic(fmt.Errorf("解析配置文件失败 %s", err))
 | 
							panic(fmt.Errorf("load command line fail: %s", err))
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func Conf() *viper.Viper {
 | 
					// 指定文件加载
 | 
				
			||||||
	return vp
 | 
					func LoadFile(confFile string) {
 | 
				
			||||||
 | 
						log.Printf("read conf file: %s", confFile)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						conf.SetConfigFile(confFile)
 | 
				
			||||||
 | 
						err := conf.ReadInConfig()
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							panic(fmt.Errorf("read file fail: %s", err))
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func Conf() *Config {
 | 
				
			||||||
 | 
						return conf
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func (c *Config) GetStringDefault(key, defaultVal string) string {
 | 
				
			||||||
 | 
						v := c.GetString(key)
 | 
				
			||||||
 | 
						if v == "" {
 | 
				
			||||||
 | 
							return defaultVal
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						return v
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func (c *Config) GetIntDefault(key string, defaultVal int) int {
 | 
				
			||||||
 | 
						v := c.GetString(key) // 未设置 空字符串
 | 
				
			||||||
 | 
						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)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue