From 26bf8320e352cfa6c87f9efef8e0701b909522ca Mon Sep 17 00:00:00 2001 From: kzkzzzz Date: Sun, 23 Jun 2024 11:32:45 +0800 Subject: [PATCH 1/5] update --- myconf/conf.go | 11 +++++------ myconf/conf_test.go | 2 +- 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/myconf/conf.go b/myconf/conf.go index f217400..9a12b11 100644 --- a/myconf/conf.go +++ b/myconf/conf.go @@ -6,20 +6,19 @@ import ( ) var ( - vp *viper.Viper + vp = viper.New() ) -func Init(confPath string, conf interface{}) { - vp = viper.New() - vp.SetConfigFile(confPath) +func LoadFile(confFile string, conf interface{}) { + vp.SetConfigFile(confFile) err := vp.ReadInConfig() if err != nil { - panic(fmt.Errorf("读取配置文件失败 %s", err)) + panic(fmt.Errorf("read file fail: %s", err)) } err = vp.Unmarshal(conf) if err != nil { - panic(fmt.Errorf("解析配置文件失败 %s", err)) + panic(fmt.Errorf("parse file fail: %s", err)) } } diff --git a/myconf/conf_test.go b/myconf/conf_test.go index 3158e14..83ee6b5 100644 --- a/myconf/conf_test.go +++ b/myconf/conf_test.go @@ -20,6 +20,6 @@ func TestConf(t *testing.T) { } var config = &Config{} - Init("test.yaml", config) + LoadFile("test.yaml", config) fmt.Printf("%+v\n", config) } From abce1f5c8d0d745c8dfdce410ae92cbb74222c4f Mon Sep 17 00:00:00 2001 From: kzkzzzz Date: Sun, 23 Jun 2024 13:15:47 +0800 Subject: [PATCH 2/5] update --- myconf/conf.go | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/myconf/conf.go b/myconf/conf.go index 9a12b11..0e31646 100644 --- a/myconf/conf.go +++ b/myconf/conf.go @@ -2,6 +2,7 @@ package myconf import ( "fmt" + "github.com/spf13/pflag" "github.com/spf13/viper" ) @@ -9,6 +10,13 @@ var ( vp = viper.New() ) +func LoadFlag() { + 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() From f17cf17e4f498a837b18cecc0f521d8aa47fd9f9 Mon Sep 17 00:00:00 2001 From: kzkzzzz Date: Sun, 23 Jun 2024 13:26:48 +0800 Subject: [PATCH 3/5] update --- myconf/conf.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/myconf/conf.go b/myconf/conf.go index 0e31646..33ddc5c 100644 --- a/myconf/conf.go +++ b/myconf/conf.go @@ -11,6 +11,9 @@ var ( ) func LoadFlag() { + if !pflag.Parsed() { + pflag.Parse() + } err := vp.BindPFlags(pflag.CommandLine) if err != nil { panic(fmt.Errorf("load command line fail: %s", err)) From ebf5c21cde3aa3017feffad30e7d63f9c8467345 Mon Sep 17 00:00:00 2001 From: kzkzzzz Date: Sun, 23 Jun 2024 14:24:25 +0800 Subject: [PATCH 4/5] update --- myconf/conf.go | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/myconf/conf.go b/myconf/conf.go index 33ddc5c..881fe23 100644 --- a/myconf/conf.go +++ b/myconf/conf.go @@ -20,7 +20,15 @@ func LoadFlag() { } } -func LoadFile(confFile string, conf interface{}) { +func LoadFile(confFile string) { + vp.SetConfigFile(confFile) + err := vp.ReadInConfig() + if err != nil { + panic(fmt.Errorf("read file fail: %s", err)) + } +} + +func LoadFileTo(confFile string, conf interface{}) { vp.SetConfigFile(confFile) err := vp.ReadInConfig() if err != nil { From dd33153ca1cff19fb77c84ad30d6bc9822562d36 Mon Sep 17 00:00:00 2001 From: kzkzzzz Date: Sat, 29 Jun 2024 22:11:39 +0800 Subject: [PATCH 5/5] update --- mymysql/mysql.go | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/mymysql/mysql.go b/mymysql/mysql.go index a66f557..bf39564 100644 --- a/mymysql/mysql.go +++ b/mymysql/mysql.go @@ -53,17 +53,16 @@ func DB(key ...string) *gorm.DB { return instance } -func InitDefault(config *Config) error { - return Init(DefaultKey, config) +func InitDefault(config *Config) { + Init(DefaultKey, config) } -func Init(key string, config *Config) error { +func Init(key string, config *Config) { db, err := New(config) if err != nil { - return err + panic(err) } instanceMap[key] = db - return nil } func New(config *Config) (*gorm.DB, error) {