Package Viper:

- Add Getxxxx function from viper into interface to prevent using lib viper and spf viper instance

Package Config:
- change pointer to retrieve config: using golib viper instead of spf viper (allow hook instead of no hook)
This commit is contained in:
Nicolas JUHEL
2024-01-05 16:12:05 +01:00
parent 086cea4702
commit 31cff079a7
12 changed files with 148 additions and 25 deletions

View File

@@ -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)

View File

@@ -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)

View File

@@ -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)

View File

@@ -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)

View File

@@ -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)

View File

@@ -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)

View File

@@ -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)

View File

@@ -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)

View File

@@ -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)

View File

@@ -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)

View File

@@ -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 {

97
viper/viper.go Normal file
View File

@@ -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)
}