Compare commits

..

No commits in common. "3493eee91942306dda0d8b490c25809702679598" and "a08b9538ae21de1ec6ea81a4a2d0a10367c64162" have entirely different histories.

View File

@ -1,12 +1,15 @@
package config package config
import ( import (
"encoding/json"
"errors"
"fmt" "fmt"
"github.com/fthvgb1/wp-go/safety" "github.com/fthvgb1/wp-go/safety"
"gopkg.in/yaml.v2" "gopkg.in/yaml.v2"
"io" "io"
"net/http" "net/http"
"os" "os"
"path/filepath"
"strings" "strings"
"time" "time"
) )
@ -103,7 +106,23 @@ func InitConfig(conf string) error {
return err return err
} }
var c Config var c Config
err = yaml.Unmarshal(file, &c) 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)
}
if err != nil { if err != nil {
return err return err
} }
@ -111,13 +130,27 @@ func InitConfig(conf string) error {
return nil return nil
} }
func jsonToYaml[T any](b []byte, c T) error {
var v map[string]any
err := json.Unmarshal(b, &v)
if err != nil {
return err
}
marshal, er := yaml.Marshal(v)
if er != nil {
return er
}
err = yaml.Unmarshal(marshal, c)
return err
}
type Dsn struct { type Dsn struct {
Host string `yaml:"host" json:"host,omitempty"` Host string `yaml:"host" json:"host,omitempty"`
Port string `yaml:"port" json:"port,omitempty"` Port json.Number `yaml:"port" json:"port,omitempty"`
Db string `yaml:"db" json:"db,omitempty"` Db string `yaml:"db" json:"db,omitempty"`
User string `yaml:"user" json:"user,omitempty"` User string `yaml:"user" json:"user,omitempty"`
Password string `yaml:"password" json:"password,omitempty"` Password string `yaml:"password" json:"password,omitempty"`
Charset string `yaml:"charset" json:"charset,omitempty"` Charset string `yaml:"charset" json:"charset,omitempty"`
} }
func (m Dsn) GetDsn() string { func (m Dsn) GetDsn() string {