Initial commit
This commit is contained in:
@@ -0,0 +1,124 @@
|
||||
package viper_server
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"fmt"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/spf13/viper"
|
||||
"pet-house.com/core/helper/dir"
|
||||
"pet-house.com/core/helper/str"
|
||||
)
|
||||
|
||||
var (
|
||||
ErrEmptyName = errors.New("config'name can't be empty value")
|
||||
)
|
||||
|
||||
type ViperConfig struct {
|
||||
Debug bool
|
||||
Directory string
|
||||
Name string
|
||||
Type string
|
||||
Default []byte
|
||||
Watch func(*viper.Viper) error
|
||||
}
|
||||
|
||||
// getConfigFilePath
|
||||
func (vc ViperConfig) getConfigFilePath() string {
|
||||
return filepath.Join(dir.GetCurrentAbPath(), vc.Directory, str.Join(vc.Name, ".", vc.Type))
|
||||
}
|
||||
|
||||
// getConfigFileDir
|
||||
func (vc ViperConfig) getConfigFileDir() string {
|
||||
if vc.Directory == "" {
|
||||
return "config"
|
||||
}
|
||||
return vc.Directory
|
||||
}
|
||||
|
||||
// IsFileExist
|
||||
func (vc ViperConfig) IsFileExist() bool {
|
||||
return dir.IsExist(vc.getConfigFilePath())
|
||||
}
|
||||
|
||||
// Remove remove config file
|
||||
func (vc ViperConfig) Remove() error {
|
||||
return dir.Remove(vc.getConfigFilePath())
|
||||
}
|
||||
|
||||
// Recover recover config file content
|
||||
func (vc ViperConfig) Recover(b []byte) error {
|
||||
_, err := dir.WriteBytes(vc.getConfigFilePath(), b)
|
||||
return err
|
||||
}
|
||||
|
||||
// Init
|
||||
func Init(vc ViperConfig) error {
|
||||
|
||||
if vc.Name == "" {
|
||||
return ErrEmptyName
|
||||
}
|
||||
|
||||
if vc.Type == "" {
|
||||
vc.Type = "yaml"
|
||||
}
|
||||
|
||||
vc.Directory = vc.getConfigFileDir()
|
||||
|
||||
filePath := vc.getConfigFilePath()
|
||||
if vc.Debug {
|
||||
fmt.Printf("\nthis config file's path is [%s]\n", filePath)
|
||||
}
|
||||
|
||||
vi := viper.New()
|
||||
if vc.Debug {
|
||||
fmt.Printf("this config file's type is [%s]\n", vc.Type)
|
||||
}
|
||||
vi.SetConfigName(vc.Name)
|
||||
vi.SetConfigType(vc.Type)
|
||||
vi.AddConfigPath(vc.Directory)
|
||||
|
||||
isExist := dir.IsExist(filePath)
|
||||
if !isExist {
|
||||
if vc.Debug {
|
||||
fmt.Printf("this config [%s] is not exist\n", filePath)
|
||||
}
|
||||
if vc.Directory != "./" {
|
||||
err := dir.InsureDir(filepath.Dir(filePath))
|
||||
if err != nil {
|
||||
return fmt.Errorf("create dir %s fail : %v", filePath, err)
|
||||
}
|
||||
}
|
||||
|
||||
// ReadConfig
|
||||
if err := vi.ReadConfig(bytes.NewBuffer(vc.Default)); err != nil {
|
||||
if vc.Debug {
|
||||
fmt.Println(string(vc.Default))
|
||||
}
|
||||
return fmt.Errorf("read default config fail : %w ", err)
|
||||
}
|
||||
|
||||
// WriteConfigAs
|
||||
if err := vi.WriteConfigAs(filePath); err != nil {
|
||||
return fmt.Errorf("write config to path fail: %w ", err)
|
||||
}
|
||||
|
||||
} else {
|
||||
if vc.Debug {
|
||||
fmt.Printf("this config file [%s] is existed\n", filePath)
|
||||
}
|
||||
vi.SetConfigFile(filePath)
|
||||
err := vi.ReadInConfig()
|
||||
if err != nil {
|
||||
return fmt.Errorf("read config fail: %w ", err)
|
||||
}
|
||||
}
|
||||
|
||||
err := vc.Watch(vi)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,156 @@
|
||||
package viper_server
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/spf13/viper"
|
||||
"pet-house.com/core/g"
|
||||
"pet-house.com/core/helper/dir"
|
||||
"pet-house.com/core/helper/str"
|
||||
)
|
||||
|
||||
type Zap struct {
|
||||
Level string `mapstructure:"level" json:"level" yaml:"level"` //debug ,info,warn,error,panic,fatal
|
||||
Format string `mapstructure:"format" json:"format" yaml:"format"`
|
||||
Prefix string `mapstructure:"prefix" json:"prefix" yaml:"prefix"`
|
||||
Director string `mapstructure:"director" json:"director" yaml:"director"`
|
||||
LinkName string `mapstructure:"link-name" json:"link-name" yaml:"link-name"`
|
||||
ShowLine bool `mapstructure:"show-line" json:"show-line" yaml:"show-line"`
|
||||
EncodeLevel string `mapstructure:"encode-level" json:"encode-level" yaml:"encode-level"`
|
||||
StacktraceKey string `mapstructure:"stacktrace-key" json:"stacktrace-key" yaml:"stacktrace-key"`
|
||||
LogInConsole bool `mapstructure:"log-in-console" json:"log-in-console" yaml:"log-in-console"`
|
||||
}
|
||||
|
||||
func TestViperInit(t *testing.T) {
|
||||
tc := &Zap{}
|
||||
config := ViperConfig{
|
||||
Directory: g.ConfigDir,
|
||||
Name: "zap", // zap => type Zap struct
|
||||
Type: g.ConfigType,
|
||||
Watch: func(vi *viper.Viper) error {
|
||||
if err := vi.Unmarshal(tc); err != nil {
|
||||
return fmt.Errorf("get Unarshal error: %v", err)
|
||||
}
|
||||
vi.SetConfigName("zap")
|
||||
return nil
|
||||
},
|
||||
//
|
||||
Default: []byte(`{
|
||||
"level": "info",
|
||||
"format": "console",
|
||||
"prefix": "[OP-ONLINE]",
|
||||
"director": "log",
|
||||
"link-name": "latest_log",
|
||||
"show-line": true,
|
||||
"encode-level": "LowercaseColorLevelEncoder",
|
||||
"stacktrace-key": "stacktrace",
|
||||
"log-in-console": true}`),
|
||||
}
|
||||
|
||||
defer config.Remove()
|
||||
|
||||
want := Zap{
|
||||
Level: "info",
|
||||
Format: "console",
|
||||
Prefix: "[OP-ONLINE]",
|
||||
Director: "log",
|
||||
LinkName: "latest_log",
|
||||
ShowLine: true,
|
||||
EncodeLevel: "LowercaseColorLevelEncoder",
|
||||
StacktraceKey: "stacktrace",
|
||||
LogInConsole: true,
|
||||
}
|
||||
|
||||
err := Init(config)
|
||||
if err != nil {
|
||||
t.Errorf("init %s's config get error: %v", str.Join(config.Name, ".", config.Type), err)
|
||||
}
|
||||
if want.Level != tc.Level {
|
||||
t.Errorf("want %+v but get %+v", want.Level, tc.Level)
|
||||
}
|
||||
if want.Format != tc.Format {
|
||||
t.Errorf("want %+v but get %+v", want.Format, tc.Format)
|
||||
}
|
||||
if want.Prefix != tc.Prefix {
|
||||
t.Errorf("want %+v but get %+v", want.Prefix, tc.Prefix)
|
||||
}
|
||||
if want.Director != tc.Director {
|
||||
t.Errorf("want %+v but get %+v", want.Director, tc.Director)
|
||||
}
|
||||
if want.LinkName != tc.LinkName {
|
||||
t.Errorf("want %+v but get %+v", want.LinkName, tc.LinkName)
|
||||
}
|
||||
if want.ShowLine != tc.ShowLine {
|
||||
t.Errorf("want %+v but get %+v", want.ShowLine, tc.ShowLine)
|
||||
}
|
||||
if want.EncodeLevel != tc.EncodeLevel {
|
||||
t.Errorf("want %+v but get %+v", want.EncodeLevel, tc.EncodeLevel)
|
||||
}
|
||||
if want.StacktraceKey != tc.StacktraceKey {
|
||||
t.Errorf("want %+v but get %+v", want.StacktraceKey, tc.StacktraceKey)
|
||||
}
|
||||
if want.LogInConsole != tc.LogInConsole {
|
||||
t.Errorf("want %+v but get %+v", want.LogInConsole, tc.LogInConsole)
|
||||
}
|
||||
|
||||
dir.WriteBytes(filepath.Join(config.getConfigFilePath()), []byte(`{
|
||||
"level": "info1",
|
||||
"format": "console1",
|
||||
"prefix": "[OP-ONLINE]1",
|
||||
"director": "log1",
|
||||
"link-name": "latest_log1",
|
||||
"show-line": false,
|
||||
"encode-level": "LowercaseColorLevelEncoder1",
|
||||
"stacktrace-key": "stacktrace1",
|
||||
"log-in-console": false}`))
|
||||
|
||||
want1 := Zap{
|
||||
Level: "info1",
|
||||
Format: "console1",
|
||||
Prefix: "[OP-ONLINE]1",
|
||||
Director: "log1",
|
||||
LinkName: "latest_log1",
|
||||
ShowLine: false,
|
||||
EncodeLevel: "LowercaseColorLevelEncoder1",
|
||||
StacktraceKey: "stacktrace1",
|
||||
LogInConsole: false,
|
||||
}
|
||||
|
||||
err = Init(config)
|
||||
if err != nil {
|
||||
t.Errorf("init %s's config get error: %v", str.Join(config.Name, ".", config.Type), err)
|
||||
}
|
||||
|
||||
time.Sleep(5 * time.Second)
|
||||
|
||||
if want1.Level != tc.Level {
|
||||
t.Errorf("want1 %+v but get %+v", want1.Level, tc.Level)
|
||||
}
|
||||
if want1.Format != tc.Format {
|
||||
t.Errorf("want1 %+v but get %+v", want1.Format, tc.Format)
|
||||
}
|
||||
if want1.Prefix != tc.Prefix {
|
||||
t.Errorf("want1 %+v but get %+v", want1.Prefix, tc.Prefix)
|
||||
}
|
||||
if want1.Director != tc.Director {
|
||||
t.Errorf("want1 %+v but get %+v", want1.Director, tc.Director)
|
||||
}
|
||||
if want1.LinkName != tc.LinkName {
|
||||
t.Errorf("want1 %+v but get %+v", want1.LinkName, tc.LinkName)
|
||||
}
|
||||
if want1.ShowLine != tc.ShowLine {
|
||||
t.Errorf("want1 %+v but get %+v", want1.ShowLine, tc.ShowLine)
|
||||
}
|
||||
if want1.EncodeLevel != tc.EncodeLevel {
|
||||
t.Errorf("want1 %+v but get %+v", want1.EncodeLevel, tc.EncodeLevel)
|
||||
}
|
||||
if want1.StacktraceKey != tc.StacktraceKey {
|
||||
t.Errorf("want1 %+v but get %+v", want1.StacktraceKey, tc.StacktraceKey)
|
||||
}
|
||||
if want1.LogInConsole != tc.LogInConsole {
|
||||
t.Errorf("want1 %+v but get %+v", want1.LogInConsole, tc.LogInConsole)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user