diff --git a/config/components/aws/config.go b/config/components/aws/config.go index 2527bfc..d39fc37 100644 --- a/config/components/aws/config.go +++ b/config/components/aws/config.go @@ -32,6 +32,7 @@ import ( cfgcus "github.com/nabbar/golib/aws/configCustom" libhtc "github.com/nabbar/golib/httpcli" libreq "github.com/nabbar/golib/request" + libvpr "github.com/nabbar/golib/viper" spfcbr "github.com/spf13/cobra" spfvpr "github.com/spf13/viper" ) @@ -117,11 +118,11 @@ func (o *componentAws) _getConfig() (libaws.Config, *libreq.OptionsHealth, *libh flg = o._getFlagUpdate() mon *libreq.OptionsHealth htc *libhtc.Options - vpr *spfvpr.Viper + vpr libvpr.Viper err error ) - if vpr = o._getSPFViper(); vpr == nil { + if vpr = o._getViper(); vpr == nil { return nil, nil, nil, ErrorComponentNotInitialized.Error(nil) } else if key = o._getKey(); len(key) < 1 { return nil, nil, nil, ErrorComponentNotInitialized.Error(nil) diff --git a/config/components/database/config.go b/config/components/database/config.go index 6634976..7817a0a 100644 --- a/config/components/database/config.go +++ b/config/components/database/config.go @@ -29,6 +29,8 @@ package database import ( "time" + libvpr "github.com/nabbar/golib/viper" + "github.com/nabbar/golib/database/gorm" spfcbr "github.com/spf13/cobra" @@ -111,11 +113,11 @@ func (o *componentDatabase) _getConfig() (*gorm.Config, error) { var ( key string cfg gorm.Config - vpr *spfvpr.Viper + vpr libvpr.Viper err error ) - if vpr = o._getSPFViper(); vpr == nil { + if vpr = o._getViper(); vpr == nil { return nil, ErrorComponentNotInitialized.Error(nil) } else if key = o._getKey(); len(key) < 1 { return nil, ErrorComponentNotInitialized.Error(nil) diff --git a/config/components/head/config.go b/config/components/head/config.go index f8da99d..7a16e0d 100644 --- a/config/components/head/config.go +++ b/config/components/head/config.go @@ -28,8 +28,8 @@ package head import ( librtr "github.com/nabbar/golib/router/header" + libvpr "github.com/nabbar/golib/viper" spfcbr "github.com/spf13/cobra" - spfvpr "github.com/spf13/viper" ) func (o *componentHead) RegisterFlag(Command *spfcbr.Command) error { @@ -40,11 +40,11 @@ func (o *componentHead) _getConfig() (*librtr.HeadersConfig, error) { var ( key string cfg librtr.HeadersConfig - vpr *spfvpr.Viper + vpr libvpr.Viper err error ) - if vpr = o._getSPFViper(); vpr == nil { + if vpr = o._getViper(); vpr == nil { return nil, ErrorComponentNotInitialized.Error(nil) } else if key = o._getKey(); len(key) < 1 { return nil, ErrorComponentNotInitialized.Error(nil) diff --git a/config/components/http/config.go b/config/components/http/config.go index 3ac4d96..1810653 100644 --- a/config/components/http/config.go +++ b/config/components/http/config.go @@ -29,9 +29,10 @@ package http import ( "fmt" + libvpr "github.com/nabbar/golib/viper" + htpool "github.com/nabbar/golib/httpserver/pool" spfcbr "github.com/spf13/cobra" - spfvpr "github.com/spf13/viper" ) func (o *componentHttp) RegisterFlag(Command *spfcbr.Command) error { @@ -42,11 +43,11 @@ func (o *componentHttp) _getConfig() (*htpool.Config, error) { var ( key string cfg htpool.Config - vpr *spfvpr.Viper + vpr libvpr.Viper err error ) - if vpr = o._getSPFViper(); vpr == nil { + if vpr = o._getViper(); vpr == nil { return nil, ErrorComponentNotInitialized.Error(nil) } else if key = o._getKey(); len(key) < 1 { return nil, ErrorComponentNotInitialized.Error(nil) diff --git a/config/components/ldap/config.go b/config/components/ldap/config.go index 3649ad8..05da10c 100644 --- a/config/components/ldap/config.go +++ b/config/components/ldap/config.go @@ -28,8 +28,8 @@ package ldap import ( lbldap "github.com/nabbar/golib/ldap" + libvpr "github.com/nabbar/golib/viper" spfcbr "github.com/spf13/cobra" - spfvpr "github.com/spf13/viper" ) func (o *componentLDAP) RegisterFlag(Command *spfcbr.Command) error { @@ -40,11 +40,11 @@ func (o *componentLDAP) _getConfig() (*lbldap.Config, error) { var ( key string cfg lbldap.Config - vpr *spfvpr.Viper + vpr libvpr.Viper err error ) - if vpr = o._getSPFViper(); vpr == nil { + if vpr = o._getViper(); vpr == nil { return nil, ErrorComponentNotInitialized.Error(nil) } else if key = o._getKey(); len(key) < 1 { return nil, ErrorComponentNotInitialized.Error(nil) diff --git a/config/components/log/config.go b/config/components/log/config.go index 31f2e12..7f4af7a 100644 --- a/config/components/log/config.go +++ b/config/components/log/config.go @@ -28,6 +28,7 @@ package log import ( logcfg "github.com/nabbar/golib/logger/config" + libvpr "github.com/nabbar/golib/viper" spfcbr "github.com/spf13/cobra" spfvpr "github.com/spf13/viper" ) @@ -73,11 +74,11 @@ func (o *componentLog) _getConfig() (*logcfg.Options, error) { var ( key string cfg logcfg.Options - vpr *spfvpr.Viper + vpr libvpr.Viper err error ) - if vpr = o._getSPFViper(); vpr == nil { + if vpr = o._getViper(); vpr == nil { return nil, ErrorComponentNotInitialized.Error(nil) } else if key = o._getKey(); len(key) < 1 { return nil, ErrorComponentNotInitialized.Error(nil) diff --git a/config/components/mail/config.go b/config/components/mail/config.go index acc458a..2cd12b5 100644 --- a/config/components/mail/config.go +++ b/config/components/mail/config.go @@ -28,6 +28,7 @@ package mail import ( libmail "github.com/nabbar/golib/mail" + libvpr "github.com/nabbar/golib/viper" spfcbr "github.com/spf13/cobra" spfvpr "github.com/spf13/viper" ) @@ -81,11 +82,11 @@ func (o *componentMail) _getConfig() (*libmail.Config, error) { var ( key string cfg libmail.Config - vpr *spfvpr.Viper + vpr libvpr.Viper err error ) - if vpr = o._getSPFViper(); vpr == nil { + if vpr = o._getViper(); vpr == nil { return nil, ErrorComponentNotInitialized.Error(nil) } else if key = o._getKey(); len(key) < 1 { return nil, ErrorComponentNotInitialized.Error(nil) diff --git a/config/components/request/config.go b/config/components/request/config.go index 82acbe5..28bac85 100644 --- a/config/components/request/config.go +++ b/config/components/request/config.go @@ -28,8 +28,8 @@ package request import ( libreq "github.com/nabbar/golib/request" + libvpr "github.com/nabbar/golib/viper" spfcbr "github.com/spf13/cobra" - spfvpr "github.com/spf13/viper" ) func (o *componentRequest) RegisterFlag(Command *spfcbr.Command) error { @@ -40,11 +40,11 @@ func (o *componentRequest) _getConfig() (*libreq.Options, error) { var ( key string cfg libreq.Options - vpr *spfvpr.Viper + vpr libvpr.Viper err error ) - if vpr = o._getSPFViper(); vpr == nil { + if vpr = o._getViper(); vpr == nil { return nil, ErrorComponentNotInitialized.Error(nil) } else if key = o._getKey(); len(key) < 1 { return nil, ErrorComponentNotInitialized.Error(nil) diff --git a/config/components/smtp/config.go b/config/components/smtp/config.go index 5ee9a3f..2a69c7c 100644 --- a/config/components/smtp/config.go +++ b/config/components/smtp/config.go @@ -29,6 +29,7 @@ package smtp import ( libmon "github.com/nabbar/golib/monitor/types" smtpcf "github.com/nabbar/golib/smtp/config" + libvpr "github.com/nabbar/golib/viper" spfcbr "github.com/spf13/cobra" spfvpr "github.com/spf13/viper" ) @@ -58,11 +59,11 @@ func (o *componentSmtp) _getConfig() (smtpcf.Config, *libmon.Config, error) { var ( key string cfg smtpcf.ConfigModel - vpr *spfvpr.Viper + vpr libvpr.Viper err error ) - if vpr = o._getSPFViper(); vpr == nil { + if vpr = o._getViper(); vpr == nil { return nil, nil, ErrorComponentNotInitialized.Error(nil) } else if key = o._getKey(); len(key) < 1 { return nil, nil, ErrorComponentNotInitialized.Error(nil) diff --git a/config/components/tls/config.go b/config/components/tls/config.go index f622bec..4a355a6 100644 --- a/config/components/tls/config.go +++ b/config/components/tls/config.go @@ -28,8 +28,8 @@ package tls import ( libtls "github.com/nabbar/golib/certificates" + libvpr "github.com/nabbar/golib/viper" spfcbr "github.com/spf13/cobra" - spfvpr "github.com/spf13/viper" ) func (o *componentTls) RegisterFlag(Command *spfcbr.Command) error { @@ -40,11 +40,11 @@ func (o *componentTls) _getConfig() (*libtls.Config, error) { var ( key string cfg libtls.Config - vpr *spfvpr.Viper + vpr libvpr.Viper err error ) - if vpr = o._getSPFViper(); vpr == nil { + if vpr = o._getViper(); vpr == nil { return nil, ErrorComponentNotInitialized.Error(nil) } else if key = o._getKey(); len(key) < 1 { return nil, ErrorComponentNotInitialized.Error(nil) diff --git a/viper/interface.go b/viper/interface.go index ae0251d..e1e125b 100644 --- a/viper/interface.go +++ b/viper/interface.go @@ -29,6 +29,7 @@ package viper import ( "io" "sync/atomic" + "time" liblog "github.com/nabbar/golib/logger" @@ -67,6 +68,24 @@ type Viper interface { UnmarshalKey(key string, rawVal interface{}) error Unmarshal(rawVal interface{}) error UnmarshalExact(rawVal interface{}) error + + GetBool(key string) bool + GetString(key string) string + GetInt(key string) int + GetInt32(key string) int32 + GetInt64(key string) int64 + GetUint(key string) uint + GetUint16(key string) uint16 + GetUint32(key string) uint32 + GetUint64(key string) uint64 + GetFloat64(key string) float64 + GetTime(key string) time.Time + GetDuration(key string) time.Duration + GetIntSlice(key string) []int + GetStringSlice(key string) []string + GetStringMap(key string) map[string]any + GetStringMapString(key string) map[string]string + GetStringMapStringSlice(key string) map[string][]string } func New(ctx libctx.FuncContext, log liblog.FuncLog) Viper { diff --git a/viper/viper.go b/viper/viper.go new file mode 100644 index 0000000..5140f1d --- /dev/null +++ b/viper/viper.go @@ -0,0 +1,97 @@ +/* + * MIT License + * + * Copyright (c) 2022 Nicolas JUHEL + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + * + * + */ + +package viper + +import "time" + +func (v *viper) GetBool(key string) bool { + return v.Viper().GetBool(key) +} + +func (v *viper) GetString(key string) string { + return v.Viper().GetString(key) +} + +func (v *viper) GetInt(key string) int { + return v.Viper().GetInt(key) +} + +func (v *viper) GetInt32(key string) int32 { + return v.Viper().GetInt32(key) +} + +func (v *viper) GetInt64(key string) int64 { + return v.Viper().GetInt64(key) +} + +func (v *viper) GetUint(key string) uint { + return v.Viper().GetUint(key) +} + +func (v *viper) GetUint16(key string) uint16 { + return v.Viper().GetUint16(key) +} + +func (v *viper) GetUint32(key string) uint32 { + return v.Viper().GetUint32(key) +} + +func (v *viper) GetUint64(key string) uint64 { + return v.Viper().GetUint64(key) +} + +func (v *viper) GetFloat64(key string) float64 { + return v.Viper().GetFloat64(key) +} + +func (v *viper) GetTime(key string) time.Time { + return v.Viper().GetTime(key) +} + +func (v *viper) GetDuration(key string) time.Duration { + return v.Viper().GetDuration(key) +} + +func (v *viper) GetIntSlice(key string) []int { + return v.Viper().GetIntSlice(key) +} + +func (v *viper) GetStringSlice(key string) []string { + return v.Viper().GetStringSlice(key) +} + +func (v *viper) GetStringMap(key string) map[string]any { + return v.Viper().GetStringMap(key) +} + +func (v *viper) GetStringMapString(key string) map[string]string { + return v.Viper().GetStringMapString(key) +} + +func (v *viper) GetStringMapStringSlice(key string) map[string][]string { + return v.Viper().GetStringMapStringSlice(key) +}