Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

config module support viper #22

Open
wants to merge 1 commit into
base: release/1.5.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 6 additions & 14 deletions config/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,33 +5,25 @@ import "time"
var config *Config

func GetString(key string) string {
return config.Viper.GetString(key)
return config.GetString(key)
}

func GetInt(key string) int {
return config.Viper.GetInt(key)
return config.GetInt(key)
}

func GetBool(key string) bool {
return config.Viper.GetBool(key)
return config.GetBool(key)
}

func GetTime(key string) time.Time {
return config.Viper.GetTime(key)
return config.GetTime(key)
}

func GetFloat64(key string) float64 {
return config.Viper.GetFloat64(key)
}

func GetDuration(key string) time.Duration {
return config.Viper.GetDuration(key)
return config.GetFloat64(key)
}

func UnmarshalKey(key string, rawVal interface{}) {
config.Viper.UnmarshalKey(key, &rawVal)
}

func WithConfig(c *Config) {
config = c
config.UnmarshalKey(key, &rawVal)
}
49 changes: 37 additions & 12 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,48 +2,73 @@ package config

import (
"strings"
"time"
)

"github.com/spf13/viper"
var (
vendor Properties
)

type Config struct {
//key: scheme, value: url
protocols map[string]string

Viper *viper.Viper
}

func New() *Config {
return &Config{
config = &Config{
protocols: make(map[string]string),
Viper: viper.New(),
}
return config
}

func (c *Config) Init() error {
//初始化Contrib
for key, value := range c.protocols {
//初始化Contrib
configsourceBuilders[key].Init(value, c)
configsourceBuilders[key].Init(value)
}
return nil
}

func (c *Config) ParseArgument() *Config {
return c
}

func (c *Config) AddProtocol(protocol string) *Config {
c.protocols[strings.Split(protocol, "://")[0]] = strings.Split(protocol, "://")[1]
c.protocols[strings.Split(protocol, "://")[0]] = protocol
return c
}

func (c *Config) ReadConfig() error {
for scheme, _ := range c.protocols {
err := configsourceBuilders[scheme].Read(c)
err := configsourceBuilders[scheme].Read()
if err != nil {
return err
}
}
return nil
}

func WithVendor(v Properties) {
vendor = v
}

func (c *Config) GetString(key string) string {
return vendor.GetString(key)
}

func (c *Config) UnmarshalKey(key string, rawVal interface{}) error {
return vendor.UnmarshalKey(key, rawVal)
}

func (c *Config) GetInt(key string) int {
return vendor.GetInt(key)
}

func (c *Config) GetBool(key string) bool {
return vendor.GetBool(key)
}

func (c *Config) GetTime(key string) time.Time {
return vendor.GetTime(key)
}

func (c *Config) GetFloat64(key string) float64 {
return vendor.GetFloat64(key)
}
4 changes: 2 additions & 2 deletions config/configsource.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ var (
)

type ConfigSource interface {
Read(config *Config) error
Init(protocol string, config *Config) error
Read() error
Init(protocol string) error
}

func Register(scheme string, creator ConfigSource) {
Expand Down
36 changes: 0 additions & 36 deletions config/contrib/env/env.go

This file was deleted.

7 changes: 0 additions & 7 deletions config/contrib/env/register.go

This file was deleted.

44 changes: 0 additions & 44 deletions config/contrib/file/file.go

This file was deleted.

7 changes: 0 additions & 7 deletions config/contrib/file/register.go

This file was deleted.

16 changes: 16 additions & 0 deletions config/contrib/xviper/register.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package xviper

import "github.com/NetEase-Media/easy-ngo/config"

const (
EnvConfigSourceName = "env"
FileConfigSourceName = "file"
)

var xviper = New()

func init() {
config.Register(FileConfigSourceName, xviper)
config.Register(EnvConfigSourceName, xviper)
config.WithVendor(xviper)
}
83 changes: 83 additions & 0 deletions config/contrib/xviper/xviper.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package xviper

import (
"strings"
"time"

"github.com/spf13/viper"
)

type XViper struct {
viper *viper.Viper
}

func New() *XViper {
return &XViper{
viper: viper.New(),
}
}

func (xviper *XViper) Init(protocol string) error {
scheme := protocol[:strings.Index(protocol, "://")]
switch scheme {
case "env":
initEnv(protocol[strings.Index(protocol, "://")+3:])
case "file":
initFile(protocol[strings.Index(protocol, "://")+3:])
}
return nil
}

func initEnv(protocol string) {
kvs := strings.Split(protocol, ";")
for _, kv := range kvs {
if strings.HasPrefix(kv, "prefix=") {
xviper.viper.SetEnvPrefix(strings.TrimPrefix(kv, "prefix="))
}
}
xviper.viper.AutomaticEnv()
}

func initFile(protocol string) {
kvs := strings.Split(protocol, ";")
for _, kv := range kvs {
if strings.HasPrefix(kv, "name=") {
xviper.viper.SetConfigName(strings.TrimPrefix(kv, "name="))
} else if strings.HasPrefix(kv, "type=") {
xviper.viper.SetConfigType(strings.TrimPrefix(kv, "type="))
} else if strings.HasPrefix(kv, "path=") {
paths := strings.Split(strings.TrimPrefix(kv, "path="), ",")
for _, path := range paths {
xviper.viper.AddConfigPath(path)
}
}
}
}

func (xviper *XViper) Read() error {
return xviper.viper.ReadInConfig()
}

func (xviper *XViper) GetString(key string) string {
return xviper.viper.GetString(key)
}

func (xviper *XViper) UnmarshalKey(key string, rawVal interface{}) error {
return xviper.viper.UnmarshalKey(key, rawVal)
}

func (xviper *XViper) GetInt(key string) int {
return xviper.viper.GetInt(key)
}

func (xviper *XViper) GetBool(key string) bool {
return xviper.viper.GetBool(key)
}

func (xviper *XViper) GetTime(key string) time.Time {
return xviper.viper.GetTime(key)
}

func (xviper *XViper) GetFloat64(key string) float64 {
return xviper.viper.GetFloat64(key)
}
6 changes: 6 additions & 0 deletions config/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ go 1.18

require github.com/spf13/viper v1.16.0

require (
github.com/google/go-cmp v0.5.9 // indirect
github.com/pkg/errors v0.9.1 // indirect
)

require (
github.com/fsnotify/fsnotify v1.6.0 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
Expand All @@ -19,4 +24,5 @@ require (
golang.org/x/text v0.9.0 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
gotest.tools v2.2.0+incompatible
)
4 changes: 4 additions & 0 deletions config/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ github.com/google/go-cmp v0.5.1/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/
github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.4/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/martian v2.1.0+incompatible/go.mod h1:9I4somxYTbIHy5NJKHRl3wXiIaQGbYVAs8BPL6v8lEs=
github.com/google/martian/v3 v3.0.0/go.mod h1:y5Zk1BBys9G+gd6Jrk0W3cC1+ELVxBWuIGO+w/tUAp0=
github.com/google/martian/v3 v3.1.0/go.mod h1:y5Zk1BBys9G+gd6Jrk0W3cC1+ELVxBWuIGO+w/tUAp0=
Expand Down Expand Up @@ -137,6 +138,7 @@ github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyua
github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
github.com/pelletier/go-toml/v2 v2.0.8 h1:0ctb6s9mE31h0/lhu+J6OPmVeDxJn+kYnJc2jZR9tGQ=
github.com/pelletier/go-toml/v2 v2.0.8/go.mod h1:vuYfssBdrU2XDZ9bYydBu6t+6a6PYNcZljzZR9VXg+4=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/sftp v1.13.1/go.mod h1:3HaPG6Dq1ILlpPZRO0HVMrsydcdLt6HRDccSgb87qRg=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
Expand Down Expand Up @@ -469,6 +471,8 @@ gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gotest.tools v2.2.0+incompatible h1:VsBPFP1AI068pPrMxtb/S8Zkgf9xEmTLJjfM+P5UIEo=
gotest.tools v2.2.0+incompatible/go.mod h1:DsYFclhRJ6vuDpmuTbkuFWG+y2sxOXAzmJt81HFBacw=
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
honnef.co/go/tools v0.0.0-20190106161140-3f1c8253044a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
honnef.co/go/tools v0.0.0-20190418001031-e561f6794a2a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
Expand Down
12 changes: 12 additions & 0 deletions config/properties.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package config

import "time"

type Properties interface {
GetString(key string) string
GetInt(key string) int
GetBool(key string) bool
GetTime(key string) time.Time
GetFloat64(key string) float64
UnmarshalKey(key string, rawVal interface{}) error
}
Loading