mirror of
https://github.com/nabbar/golib.git
synced 2025-11-01 11:32:34 +08:00
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:
@@ -32,6 +32,7 @@ import (
|
|||||||
cfgcus "github.com/nabbar/golib/aws/configCustom"
|
cfgcus "github.com/nabbar/golib/aws/configCustom"
|
||||||
libhtc "github.com/nabbar/golib/httpcli"
|
libhtc "github.com/nabbar/golib/httpcli"
|
||||||
libreq "github.com/nabbar/golib/request"
|
libreq "github.com/nabbar/golib/request"
|
||||||
|
libvpr "github.com/nabbar/golib/viper"
|
||||||
spfcbr "github.com/spf13/cobra"
|
spfcbr "github.com/spf13/cobra"
|
||||||
spfvpr "github.com/spf13/viper"
|
spfvpr "github.com/spf13/viper"
|
||||||
)
|
)
|
||||||
@@ -117,11 +118,11 @@ func (o *componentAws) _getConfig() (libaws.Config, *libreq.OptionsHealth, *libh
|
|||||||
flg = o._getFlagUpdate()
|
flg = o._getFlagUpdate()
|
||||||
mon *libreq.OptionsHealth
|
mon *libreq.OptionsHealth
|
||||||
htc *libhtc.Options
|
htc *libhtc.Options
|
||||||
vpr *spfvpr.Viper
|
vpr libvpr.Viper
|
||||||
err error
|
err error
|
||||||
)
|
)
|
||||||
|
|
||||||
if vpr = o._getSPFViper(); vpr == nil {
|
if vpr = o._getViper(); vpr == nil {
|
||||||
return nil, nil, nil, ErrorComponentNotInitialized.Error(nil)
|
return nil, nil, nil, ErrorComponentNotInitialized.Error(nil)
|
||||||
} else if key = o._getKey(); len(key) < 1 {
|
} else if key = o._getKey(); len(key) < 1 {
|
||||||
return nil, nil, nil, ErrorComponentNotInitialized.Error(nil)
|
return nil, nil, nil, ErrorComponentNotInitialized.Error(nil)
|
||||||
|
|||||||
@@ -29,6 +29,8 @@ package database
|
|||||||
import (
|
import (
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
libvpr "github.com/nabbar/golib/viper"
|
||||||
|
|
||||||
"github.com/nabbar/golib/database/gorm"
|
"github.com/nabbar/golib/database/gorm"
|
||||||
|
|
||||||
spfcbr "github.com/spf13/cobra"
|
spfcbr "github.com/spf13/cobra"
|
||||||
@@ -111,11 +113,11 @@ func (o *componentDatabase) _getConfig() (*gorm.Config, error) {
|
|||||||
var (
|
var (
|
||||||
key string
|
key string
|
||||||
cfg gorm.Config
|
cfg gorm.Config
|
||||||
vpr *spfvpr.Viper
|
vpr libvpr.Viper
|
||||||
err error
|
err error
|
||||||
)
|
)
|
||||||
|
|
||||||
if vpr = o._getSPFViper(); vpr == nil {
|
if vpr = o._getViper(); vpr == nil {
|
||||||
return nil, ErrorComponentNotInitialized.Error(nil)
|
return nil, ErrorComponentNotInitialized.Error(nil)
|
||||||
} else if key = o._getKey(); len(key) < 1 {
|
} else if key = o._getKey(); len(key) < 1 {
|
||||||
return nil, ErrorComponentNotInitialized.Error(nil)
|
return nil, ErrorComponentNotInitialized.Error(nil)
|
||||||
|
|||||||
@@ -28,8 +28,8 @@ package head
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
librtr "github.com/nabbar/golib/router/header"
|
librtr "github.com/nabbar/golib/router/header"
|
||||||
|
libvpr "github.com/nabbar/golib/viper"
|
||||||
spfcbr "github.com/spf13/cobra"
|
spfcbr "github.com/spf13/cobra"
|
||||||
spfvpr "github.com/spf13/viper"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func (o *componentHead) RegisterFlag(Command *spfcbr.Command) error {
|
func (o *componentHead) RegisterFlag(Command *spfcbr.Command) error {
|
||||||
@@ -40,11 +40,11 @@ func (o *componentHead) _getConfig() (*librtr.HeadersConfig, error) {
|
|||||||
var (
|
var (
|
||||||
key string
|
key string
|
||||||
cfg librtr.HeadersConfig
|
cfg librtr.HeadersConfig
|
||||||
vpr *spfvpr.Viper
|
vpr libvpr.Viper
|
||||||
err error
|
err error
|
||||||
)
|
)
|
||||||
|
|
||||||
if vpr = o._getSPFViper(); vpr == nil {
|
if vpr = o._getViper(); vpr == nil {
|
||||||
return nil, ErrorComponentNotInitialized.Error(nil)
|
return nil, ErrorComponentNotInitialized.Error(nil)
|
||||||
} else if key = o._getKey(); len(key) < 1 {
|
} else if key = o._getKey(); len(key) < 1 {
|
||||||
return nil, ErrorComponentNotInitialized.Error(nil)
|
return nil, ErrorComponentNotInitialized.Error(nil)
|
||||||
|
|||||||
@@ -29,9 +29,10 @@ package http
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
|
libvpr "github.com/nabbar/golib/viper"
|
||||||
|
|
||||||
htpool "github.com/nabbar/golib/httpserver/pool"
|
htpool "github.com/nabbar/golib/httpserver/pool"
|
||||||
spfcbr "github.com/spf13/cobra"
|
spfcbr "github.com/spf13/cobra"
|
||||||
spfvpr "github.com/spf13/viper"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func (o *componentHttp) RegisterFlag(Command *spfcbr.Command) error {
|
func (o *componentHttp) RegisterFlag(Command *spfcbr.Command) error {
|
||||||
@@ -42,11 +43,11 @@ func (o *componentHttp) _getConfig() (*htpool.Config, error) {
|
|||||||
var (
|
var (
|
||||||
key string
|
key string
|
||||||
cfg htpool.Config
|
cfg htpool.Config
|
||||||
vpr *spfvpr.Viper
|
vpr libvpr.Viper
|
||||||
err error
|
err error
|
||||||
)
|
)
|
||||||
|
|
||||||
if vpr = o._getSPFViper(); vpr == nil {
|
if vpr = o._getViper(); vpr == nil {
|
||||||
return nil, ErrorComponentNotInitialized.Error(nil)
|
return nil, ErrorComponentNotInitialized.Error(nil)
|
||||||
} else if key = o._getKey(); len(key) < 1 {
|
} else if key = o._getKey(); len(key) < 1 {
|
||||||
return nil, ErrorComponentNotInitialized.Error(nil)
|
return nil, ErrorComponentNotInitialized.Error(nil)
|
||||||
|
|||||||
@@ -28,8 +28,8 @@ package ldap
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
lbldap "github.com/nabbar/golib/ldap"
|
lbldap "github.com/nabbar/golib/ldap"
|
||||||
|
libvpr "github.com/nabbar/golib/viper"
|
||||||
spfcbr "github.com/spf13/cobra"
|
spfcbr "github.com/spf13/cobra"
|
||||||
spfvpr "github.com/spf13/viper"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func (o *componentLDAP) RegisterFlag(Command *spfcbr.Command) error {
|
func (o *componentLDAP) RegisterFlag(Command *spfcbr.Command) error {
|
||||||
@@ -40,11 +40,11 @@ func (o *componentLDAP) _getConfig() (*lbldap.Config, error) {
|
|||||||
var (
|
var (
|
||||||
key string
|
key string
|
||||||
cfg lbldap.Config
|
cfg lbldap.Config
|
||||||
vpr *spfvpr.Viper
|
vpr libvpr.Viper
|
||||||
err error
|
err error
|
||||||
)
|
)
|
||||||
|
|
||||||
if vpr = o._getSPFViper(); vpr == nil {
|
if vpr = o._getViper(); vpr == nil {
|
||||||
return nil, ErrorComponentNotInitialized.Error(nil)
|
return nil, ErrorComponentNotInitialized.Error(nil)
|
||||||
} else if key = o._getKey(); len(key) < 1 {
|
} else if key = o._getKey(); len(key) < 1 {
|
||||||
return nil, ErrorComponentNotInitialized.Error(nil)
|
return nil, ErrorComponentNotInitialized.Error(nil)
|
||||||
|
|||||||
@@ -28,6 +28,7 @@ package log
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
logcfg "github.com/nabbar/golib/logger/config"
|
logcfg "github.com/nabbar/golib/logger/config"
|
||||||
|
libvpr "github.com/nabbar/golib/viper"
|
||||||
spfcbr "github.com/spf13/cobra"
|
spfcbr "github.com/spf13/cobra"
|
||||||
spfvpr "github.com/spf13/viper"
|
spfvpr "github.com/spf13/viper"
|
||||||
)
|
)
|
||||||
@@ -73,11 +74,11 @@ func (o *componentLog) _getConfig() (*logcfg.Options, error) {
|
|||||||
var (
|
var (
|
||||||
key string
|
key string
|
||||||
cfg logcfg.Options
|
cfg logcfg.Options
|
||||||
vpr *spfvpr.Viper
|
vpr libvpr.Viper
|
||||||
err error
|
err error
|
||||||
)
|
)
|
||||||
|
|
||||||
if vpr = o._getSPFViper(); vpr == nil {
|
if vpr = o._getViper(); vpr == nil {
|
||||||
return nil, ErrorComponentNotInitialized.Error(nil)
|
return nil, ErrorComponentNotInitialized.Error(nil)
|
||||||
} else if key = o._getKey(); len(key) < 1 {
|
} else if key = o._getKey(); len(key) < 1 {
|
||||||
return nil, ErrorComponentNotInitialized.Error(nil)
|
return nil, ErrorComponentNotInitialized.Error(nil)
|
||||||
|
|||||||
@@ -28,6 +28,7 @@ package mail
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
libmail "github.com/nabbar/golib/mail"
|
libmail "github.com/nabbar/golib/mail"
|
||||||
|
libvpr "github.com/nabbar/golib/viper"
|
||||||
spfcbr "github.com/spf13/cobra"
|
spfcbr "github.com/spf13/cobra"
|
||||||
spfvpr "github.com/spf13/viper"
|
spfvpr "github.com/spf13/viper"
|
||||||
)
|
)
|
||||||
@@ -81,11 +82,11 @@ func (o *componentMail) _getConfig() (*libmail.Config, error) {
|
|||||||
var (
|
var (
|
||||||
key string
|
key string
|
||||||
cfg libmail.Config
|
cfg libmail.Config
|
||||||
vpr *spfvpr.Viper
|
vpr libvpr.Viper
|
||||||
err error
|
err error
|
||||||
)
|
)
|
||||||
|
|
||||||
if vpr = o._getSPFViper(); vpr == nil {
|
if vpr = o._getViper(); vpr == nil {
|
||||||
return nil, ErrorComponentNotInitialized.Error(nil)
|
return nil, ErrorComponentNotInitialized.Error(nil)
|
||||||
} else if key = o._getKey(); len(key) < 1 {
|
} else if key = o._getKey(); len(key) < 1 {
|
||||||
return nil, ErrorComponentNotInitialized.Error(nil)
|
return nil, ErrorComponentNotInitialized.Error(nil)
|
||||||
|
|||||||
@@ -28,8 +28,8 @@ package request
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
libreq "github.com/nabbar/golib/request"
|
libreq "github.com/nabbar/golib/request"
|
||||||
|
libvpr "github.com/nabbar/golib/viper"
|
||||||
spfcbr "github.com/spf13/cobra"
|
spfcbr "github.com/spf13/cobra"
|
||||||
spfvpr "github.com/spf13/viper"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func (o *componentRequest) RegisterFlag(Command *spfcbr.Command) error {
|
func (o *componentRequest) RegisterFlag(Command *spfcbr.Command) error {
|
||||||
@@ -40,11 +40,11 @@ func (o *componentRequest) _getConfig() (*libreq.Options, error) {
|
|||||||
var (
|
var (
|
||||||
key string
|
key string
|
||||||
cfg libreq.Options
|
cfg libreq.Options
|
||||||
vpr *spfvpr.Viper
|
vpr libvpr.Viper
|
||||||
err error
|
err error
|
||||||
)
|
)
|
||||||
|
|
||||||
if vpr = o._getSPFViper(); vpr == nil {
|
if vpr = o._getViper(); vpr == nil {
|
||||||
return nil, ErrorComponentNotInitialized.Error(nil)
|
return nil, ErrorComponentNotInitialized.Error(nil)
|
||||||
} else if key = o._getKey(); len(key) < 1 {
|
} else if key = o._getKey(); len(key) < 1 {
|
||||||
return nil, ErrorComponentNotInitialized.Error(nil)
|
return nil, ErrorComponentNotInitialized.Error(nil)
|
||||||
|
|||||||
@@ -29,6 +29,7 @@ package smtp
|
|||||||
import (
|
import (
|
||||||
libmon "github.com/nabbar/golib/monitor/types"
|
libmon "github.com/nabbar/golib/monitor/types"
|
||||||
smtpcf "github.com/nabbar/golib/smtp/config"
|
smtpcf "github.com/nabbar/golib/smtp/config"
|
||||||
|
libvpr "github.com/nabbar/golib/viper"
|
||||||
spfcbr "github.com/spf13/cobra"
|
spfcbr "github.com/spf13/cobra"
|
||||||
spfvpr "github.com/spf13/viper"
|
spfvpr "github.com/spf13/viper"
|
||||||
)
|
)
|
||||||
@@ -58,11 +59,11 @@ func (o *componentSmtp) _getConfig() (smtpcf.Config, *libmon.Config, error) {
|
|||||||
var (
|
var (
|
||||||
key string
|
key string
|
||||||
cfg smtpcf.ConfigModel
|
cfg smtpcf.ConfigModel
|
||||||
vpr *spfvpr.Viper
|
vpr libvpr.Viper
|
||||||
err error
|
err error
|
||||||
)
|
)
|
||||||
|
|
||||||
if vpr = o._getSPFViper(); vpr == nil {
|
if vpr = o._getViper(); vpr == nil {
|
||||||
return nil, nil, ErrorComponentNotInitialized.Error(nil)
|
return nil, nil, ErrorComponentNotInitialized.Error(nil)
|
||||||
} else if key = o._getKey(); len(key) < 1 {
|
} else if key = o._getKey(); len(key) < 1 {
|
||||||
return nil, nil, ErrorComponentNotInitialized.Error(nil)
|
return nil, nil, ErrorComponentNotInitialized.Error(nil)
|
||||||
|
|||||||
@@ -28,8 +28,8 @@ package tls
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
libtls "github.com/nabbar/golib/certificates"
|
libtls "github.com/nabbar/golib/certificates"
|
||||||
|
libvpr "github.com/nabbar/golib/viper"
|
||||||
spfcbr "github.com/spf13/cobra"
|
spfcbr "github.com/spf13/cobra"
|
||||||
spfvpr "github.com/spf13/viper"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func (o *componentTls) RegisterFlag(Command *spfcbr.Command) error {
|
func (o *componentTls) RegisterFlag(Command *spfcbr.Command) error {
|
||||||
@@ -40,11 +40,11 @@ func (o *componentTls) _getConfig() (*libtls.Config, error) {
|
|||||||
var (
|
var (
|
||||||
key string
|
key string
|
||||||
cfg libtls.Config
|
cfg libtls.Config
|
||||||
vpr *spfvpr.Viper
|
vpr libvpr.Viper
|
||||||
err error
|
err error
|
||||||
)
|
)
|
||||||
|
|
||||||
if vpr = o._getSPFViper(); vpr == nil {
|
if vpr = o._getViper(); vpr == nil {
|
||||||
return nil, ErrorComponentNotInitialized.Error(nil)
|
return nil, ErrorComponentNotInitialized.Error(nil)
|
||||||
} else if key = o._getKey(); len(key) < 1 {
|
} else if key = o._getKey(); len(key) < 1 {
|
||||||
return nil, ErrorComponentNotInitialized.Error(nil)
|
return nil, ErrorComponentNotInitialized.Error(nil)
|
||||||
|
|||||||
@@ -29,6 +29,7 @@ package viper
|
|||||||
import (
|
import (
|
||||||
"io"
|
"io"
|
||||||
"sync/atomic"
|
"sync/atomic"
|
||||||
|
"time"
|
||||||
|
|
||||||
liblog "github.com/nabbar/golib/logger"
|
liblog "github.com/nabbar/golib/logger"
|
||||||
|
|
||||||
@@ -67,6 +68,24 @@ type Viper interface {
|
|||||||
UnmarshalKey(key string, rawVal interface{}) error
|
UnmarshalKey(key string, rawVal interface{}) error
|
||||||
Unmarshal(rawVal interface{}) error
|
Unmarshal(rawVal interface{}) error
|
||||||
UnmarshalExact(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 {
|
func New(ctx libctx.FuncContext, log liblog.FuncLog) Viper {
|
||||||
|
|||||||
97
viper/viper.go
Normal file
97
viper/viper.go
Normal 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)
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user