Skip to content

Commit

Permalink
feat(configuration): add viper config wrapper (#205)
Browse files Browse the repository at this point in the history
  • Loading branch information
plastikfan committed Oct 10, 2023
1 parent 40dff7e commit 2ccbe39
Show file tree
Hide file tree
Showing 9 changed files with 1,181 additions and 20 deletions.
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
"paramset",
"prealloc",
"psname",
"quantico",
"rebinder",
"refl",
"repotoken",
Expand Down
21 changes: 17 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,29 @@ require (
github.com/onsi/gomega v1.28.0
github.com/spf13/cobra v1.7.0
github.com/spf13/pflag v1.0.5
golang.org/x/exp v0.0.0-20230321023759-10a507213a29
golang.org/x/exp v0.0.0-20230905200255-921286631fa9
)

require (
github.com/fsnotify/fsnotify v1.6.0 // indirect
github.com/go-logr/logr v1.2.4 // indirect
github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572 // indirect
github.com/google/go-cmp v0.5.9 // indirect
github.com/google/pprof v0.0.0-20230406165453-00490a63f317 // indirect
github.com/kr/pretty v0.3.1 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/magiconair/properties v1.8.7 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/pelletier/go-toml/v2 v2.1.0 // indirect
github.com/pkg/errors v0.9.1 // indirect
golang.org/x/tools v0.12.0 // indirect
github.com/sagikazarmark/locafero v0.3.0 // indirect
github.com/sagikazarmark/slog-shim v0.1.0 // indirect
github.com/sourcegraph/conc v0.3.0 // indirect
github.com/spf13/afero v1.10.0 // indirect
github.com/spf13/cast v1.5.1 // indirect
github.com/subosito/gotenv v1.6.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/tools v0.13.0 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)

Expand All @@ -27,7 +39,8 @@ require (
github.com/nicksnyder/go-i18n/v2 v2.2.1 // indirect
github.com/samber/lo v1.38.1
github.com/snivilised/extendio v0.2.1
golang.org/x/net v0.14.0 // indirect
github.com/spf13/viper v1.17.0
golang.org/x/net v0.15.0 // indirect
golang.org/x/sys v0.12.0 // indirect
golang.org/x/text v0.13.0
)
475 changes: 459 additions & 16 deletions go.sum

Large diffs are not rendered by default.

13 changes: 13 additions & 0 deletions src/assistant/configuration/configuration_suite_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package configuration_test

import (
"testing"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)

func TestConfiguration(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Configuration Suite")
}
189 changes: 189 additions & 0 deletions src/assistant/configuration/global-config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
package configuration

import (
"time"

"github.com/spf13/pflag"
"github.com/spf13/viper"
)

type ViperConfig interface {
AddConfigPath(in string)
AutomaticEnv()
BindFlagValue(key string, flag viper.FlagValue) error
BindFlagValues(flags viper.FlagValueSet) error
BindPFlag(key string, flag *pflag.Flag) error
ConfigFileUsed() string
Get(key string) interface{}
GetBool(key string) bool
GetDuration(key string) time.Duration
GetFloat64(key string) float64
GetInt(key string) int
GetInt32(key string) int32
GetInt64(key string) int64
GetIntSlice(key string) []int
GetUint(key string) uint
GetUint16(key string) uint16
GetUint32(key string) uint32
GetUint64(key string) uint64
GetTime(key string) time.Time
GetSizeInBytes(key string) uint
GetString(key string) string
GetStringMap(key string) map[string]interface{}
GetStringMapString(key string) map[string]string
GetStringMapStringSlice(key string) map[string][]string
GetStringSlice(key string) []string
InConfig(key string) bool
ReadInConfig() error
SetConfigFile(in string)
SetConfigName(in string)
SetConfigType(in string)
SetTypeByDefaultValue(enable bool)
Sub(key string) *viper.Viper
Unmarshal(rawVal interface{}, opts ...viper.DecoderConfigOption) error
UnmarshalExact(rawVal interface{}, opts ...viper.DecoderConfigOption) error
UnmarshalKey(key string, rawVal interface{}, opts ...viper.DecoderConfigOption) error
}

type GlobalViperConfig struct {
}

func (p *GlobalViperConfig) AddConfigPath(in string) {
viper.AddConfigPath(in)
}

func (p *GlobalViperConfig) AutomaticEnv() {
viper.AutomaticEnv()
}

func (p *GlobalViperConfig) BindFlagValue(key string, flag viper.FlagValue) error {
return viper.BindFlagValue(key, flag)
}

func (p *GlobalViperConfig) BindFlagValues(flags viper.FlagValueSet) error {
return viper.BindFlagValues(flags)
}

func (p *GlobalViperConfig) BindPFlag(key string, flag *pflag.Flag) error {
return viper.BindPFlag(key, flag)
}

func (p *GlobalViperConfig) ConfigFileUsed() string {
return viper.ConfigFileUsed()
}

func (p *GlobalViperConfig) Get(key string) interface{} {
return viper.Get(key)
}

func (p *GlobalViperConfig) GetBool(key string) bool {
return viper.GetBool(key)
}

func (p *GlobalViperConfig) GetDuration(key string) time.Duration {
return viper.GetDuration(key)
}

func (p *GlobalViperConfig) GetFloat64(key string) float64 {
return viper.GetFloat64(key)
}

func (p *GlobalViperConfig) GetInt(key string) int {
return viper.GetInt(key)
}

func (p *GlobalViperConfig) GetInt32(key string) int32 {
return viper.GetInt32(key)
}

func (p *GlobalViperConfig) GetInt64(key string) int64 {
return viper.GetInt64(key)
}

func (p *GlobalViperConfig) GetIntSlice(key string) []int {
return viper.GetIntSlice(key)
}

func (p *GlobalViperConfig) GetUint(key string) uint {
return viper.GetUint(key)
}

func (p *GlobalViperConfig) GetUint16(key string) uint16 {
return viper.GetUint16(key)
}

func (p *GlobalViperConfig) GetUint32(key string) uint32 {
return viper.GetUint32(key)
}

func (p *GlobalViperConfig) GetUint64(key string) uint64 {
return viper.GetUint64(key)
}

func (p *GlobalViperConfig) GetTime(key string) time.Time {
return viper.GetTime(key)
}

func (p *GlobalViperConfig) GetSizeInBytes(key string) uint {
return viper.GetSizeInBytes(key)
}

func (p *GlobalViperConfig) GetString(key string) string {
return viper.GetString(key)
}

func (p *GlobalViperConfig) GetStringMap(key string) map[string]interface{} {
return viper.GetStringMap(key)
}

func (p *GlobalViperConfig) GetStringMapString(key string) map[string]string {
return viper.GetStringMapString(key)
}

func (p *GlobalViperConfig) GetStringMapStringSlice(key string) map[string][]string {
return viper.GetStringMapStringSlice(key)
}

func (p *GlobalViperConfig) GetStringSlice(key string) []string {
return viper.GetStringSlice(key)
}

func (p *GlobalViperConfig) InConfig(key string) bool {
return viper.InConfig(key)
}

func (p *GlobalViperConfig) ReadInConfig() error {
return viper.ReadInConfig()
}

func (p *GlobalViperConfig) SetConfigFile(in string) {
viper.SetConfigFile(in)
}

func (p *GlobalViperConfig) SetConfigName(in string) {
viper.SetConfigName(in)
}

func (p *GlobalViperConfig) SetConfigType(in string) {
viper.SetConfigType(in)
}

func (p *GlobalViperConfig) SetTypeByDefaultValue(enable bool) {
viper.SetTypeByDefaultValue(enable)
}

func (p *GlobalViperConfig) Sub(key string) *viper.Viper {
return viper.Sub(key)
}

func (p *GlobalViperConfig) Unmarshal(rawVal interface{}, opts ...viper.DecoderConfigOption) error {
return viper.Unmarshal(rawVal, opts...)
}

func (p *GlobalViperConfig) UnmarshalExact(rawVal interface{}, opts ...viper.DecoderConfigOption) error {
return viper.UnmarshalExact(rawVal, opts...)
}

func (p *GlobalViperConfig) UnmarshalKey(key string, rawVal interface{}, opts ...viper.DecoderConfigOption) error {
return viper.UnmarshalKey(key, rawVal, opts...)
}
Loading

0 comments on commit 2ccbe39

Please sign in to comment.