From e4fa93e893a9b80d22df2f17fd712c9e7dedfbfb Mon Sep 17 00:00:00 2001 From: xing Date: Thu, 1 Jun 2023 22:46:46 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=20yaml=E6=9C=AC=E8=BA=AB?= =?UTF-8?q?=E5=B0=B1=E5=85=BC=E5=AE=B9json....?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/pkg/config/config.go | 32 +++++++------------------------- 1 file changed, 7 insertions(+), 25 deletions(-) diff --git a/app/pkg/config/config.go b/app/pkg/config/config.go index 2d483e2..2a299f4 100644 --- a/app/pkg/config/config.go +++ b/app/pkg/config/config.go @@ -2,14 +2,12 @@ package config import ( "encoding/json" - "errors" "fmt" "github.com/fthvgb1/wp-go/safety" "gopkg.in/yaml.v2" "io" "net/http" "os" - "path/filepath" "strings" "time" ) @@ -106,23 +104,7 @@ func InitConfig(conf string) error { return err } var c Config - switch strings.ToLower(filepath.Ext(conf)) { - case ".yaml": - err = yaml.Unmarshal(file, &c) - case ".json": - err = jsonToYaml(file, &c) - default: - err = yaml.Unmarshal(file, &c) - if err == nil { - break - } - err = jsonToYaml(file, &c) - if err == nil { - break - } - return errors.Join(errors.New("can't parse the config"), err) - } - + err = yaml.Unmarshal(file, &c) if err != nil { return err } @@ -145,12 +127,12 @@ func jsonToYaml[T any](b []byte, c T) error { } type Dsn struct { - Host string `yaml:"host" json:"host,omitempty"` - Port json.Number `yaml:"port" json:"port,omitempty"` - Db string `yaml:"db" json:"db,omitempty"` - User string `yaml:"user" json:"user,omitempty"` - Password string `yaml:"password" json:"password,omitempty"` - Charset string `yaml:"charset" json:"charset,omitempty"` + Host string `yaml:"host" json:"host,omitempty"` + Port string `yaml:"port" json:"port,omitempty"` + Db string `yaml:"db" json:"db,omitempty"` + User string `yaml:"user" json:"user,omitempty"` + Password string `yaml:"password" json:"password,omitempty"` + Charset string `yaml:"charset" json:"charset,omitempty"` } func (m Dsn) GetDsn() string {