mirror of
https://github.com/spf13/viper
synced 2024-12-22 11:37:02 +00:00
refactor: replace jww with the new logger interface
This commit is contained in:
parent
f1f6b2122c
commit
a785a79f22
4 changed files with 49 additions and 33 deletions
9
util.go
9
util.go
|
@ -19,7 +19,6 @@ import (
|
||||||
"unicode"
|
"unicode"
|
||||||
|
|
||||||
"github.com/spf13/cast"
|
"github.com/spf13/cast"
|
||||||
jww "github.com/spf13/jwalterweatherman"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// ConfigParseError denotes failing to parse configuration file.
|
// ConfigParseError denotes failing to parse configuration file.
|
||||||
|
@ -87,8 +86,8 @@ func insensitiviseMap(m map[string]interface{}) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func absPathify(inPath string) string {
|
func absPathify(logger Logger, inPath string) string {
|
||||||
jww.INFO.Println("Trying to resolve absolute path to", inPath)
|
logger.Info("trying to resolve absolute path", "path", inPath)
|
||||||
|
|
||||||
if inPath == "$HOME" || strings.HasPrefix(inPath, "$HOME"+string(os.PathSeparator)) {
|
if inPath == "$HOME" || strings.HasPrefix(inPath, "$HOME"+string(os.PathSeparator)) {
|
||||||
inPath = userHomeDir() + inPath[5:]
|
inPath = userHomeDir() + inPath[5:]
|
||||||
|
@ -105,8 +104,8 @@ func absPathify(inPath string) string {
|
||||||
return filepath.Clean(p)
|
return filepath.Clean(p)
|
||||||
}
|
}
|
||||||
|
|
||||||
jww.ERROR.Println("Couldn't discover absolute path")
|
logger.Error(fmt.Errorf("could not discover absolute path: %w", err).Error())
|
||||||
jww.ERROR.Println(err)
|
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -87,7 +87,7 @@ func TestAbsPathify(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, test := range tests {
|
for _, test := range tests {
|
||||||
got := absPathify(test.input)
|
got := absPathify(jwwLogger{}, test.input)
|
||||||
if got != test.output {
|
if got != test.output {
|
||||||
t.Errorf("Got %v\nexpected\n%q", got, test.output)
|
t.Errorf("Got %v\nexpected\n%q", got, test.output)
|
||||||
}
|
}
|
||||||
|
|
62
viper.go
62
viper.go
|
@ -39,7 +39,6 @@ import (
|
||||||
"github.com/mitchellh/mapstructure"
|
"github.com/mitchellh/mapstructure"
|
||||||
"github.com/spf13/afero"
|
"github.com/spf13/afero"
|
||||||
"github.com/spf13/cast"
|
"github.com/spf13/cast"
|
||||||
jww "github.com/spf13/jwalterweatherman"
|
|
||||||
"github.com/spf13/pflag"
|
"github.com/spf13/pflag"
|
||||||
"github.com/subosito/gotenv"
|
"github.com/subosito/gotenv"
|
||||||
"gopkg.in/ini.v1"
|
"gopkg.in/ini.v1"
|
||||||
|
@ -260,6 +259,8 @@ type Viper struct {
|
||||||
properties *properties.Properties
|
properties *properties.Properties
|
||||||
|
|
||||||
onConfigChange func(fsnotify.Event)
|
onConfigChange func(fsnotify.Event)
|
||||||
|
|
||||||
|
logger Logger
|
||||||
}
|
}
|
||||||
|
|
||||||
// New returns an initialized Viper instance.
|
// New returns an initialized Viper instance.
|
||||||
|
@ -277,6 +278,7 @@ func New() *Viper {
|
||||||
v.env = make(map[string][]string)
|
v.env = make(map[string][]string)
|
||||||
v.aliases = make(map[string]string)
|
v.aliases = make(map[string]string)
|
||||||
v.typeByDefValue = false
|
v.typeByDefValue = false
|
||||||
|
v.logger = jwwLogger{}
|
||||||
|
|
||||||
return v
|
return v
|
||||||
}
|
}
|
||||||
|
@ -517,8 +519,9 @@ func AddConfigPath(in string) { v.AddConfigPath(in) }
|
||||||
|
|
||||||
func (v *Viper) AddConfigPath(in string) {
|
func (v *Viper) AddConfigPath(in string) {
|
||||||
if in != "" {
|
if in != "" {
|
||||||
absin := absPathify(in)
|
absin := absPathify(v.logger, in)
|
||||||
jww.INFO.Println("adding", absin, "to paths to search")
|
|
||||||
|
v.logger.Info("adding path to search paths", "path", absin)
|
||||||
if !stringInSlice(absin, v.configPaths) {
|
if !stringInSlice(absin, v.configPaths) {
|
||||||
v.configPaths = append(v.configPaths, absin)
|
v.configPaths = append(v.configPaths, absin)
|
||||||
}
|
}
|
||||||
|
@ -542,7 +545,8 @@ func (v *Viper) AddRemoteProvider(provider, endpoint, path string) error {
|
||||||
return UnsupportedRemoteProviderError(provider)
|
return UnsupportedRemoteProviderError(provider)
|
||||||
}
|
}
|
||||||
if provider != "" && endpoint != "" {
|
if provider != "" && endpoint != "" {
|
||||||
jww.INFO.Printf("adding %s:%s to remote provider list", provider, endpoint)
|
v.logger.Info("adding remote provider", "provider", provider, "endpoint", endpoint)
|
||||||
|
|
||||||
rp := &defaultRemoteProvider{
|
rp := &defaultRemoteProvider{
|
||||||
endpoint: endpoint,
|
endpoint: endpoint,
|
||||||
provider: provider,
|
provider: provider,
|
||||||
|
@ -574,7 +578,8 @@ func (v *Viper) AddSecureRemoteProvider(provider, endpoint, path, secretkeyring
|
||||||
return UnsupportedRemoteProviderError(provider)
|
return UnsupportedRemoteProviderError(provider)
|
||||||
}
|
}
|
||||||
if provider != "" && endpoint != "" {
|
if provider != "" && endpoint != "" {
|
||||||
jww.INFO.Printf("adding %s:%s to remote provider list", provider, endpoint)
|
v.logger.Info("adding remote provider", "provider", provider, "endpoint", endpoint)
|
||||||
|
|
||||||
rp := &defaultRemoteProvider{
|
rp := &defaultRemoteProvider{
|
||||||
endpoint: endpoint,
|
endpoint: endpoint,
|
||||||
provider: provider,
|
provider: provider,
|
||||||
|
@ -1390,14 +1395,15 @@ func (v *Viper) registerAlias(alias string, key string) {
|
||||||
v.aliases[alias] = key
|
v.aliases[alias] = key
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
jww.WARN.Println("Creating circular reference alias", alias, key, v.realKey(key))
|
v.logger.Warn("creating circular reference alias", "alias", alias, "key", key, "real_key", v.realKey(key))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (v *Viper) realKey(key string) string {
|
func (v *Viper) realKey(key string) string {
|
||||||
newkey, exists := v.aliases[key]
|
newkey, exists := v.aliases[key]
|
||||||
if exists {
|
if exists {
|
||||||
jww.DEBUG.Println("Alias", key, "to", newkey)
|
v.logger.Debug("key is an alias", "alias", key, "to", newkey)
|
||||||
|
|
||||||
return v.realKey(newkey)
|
return v.realKey(newkey)
|
||||||
}
|
}
|
||||||
return key
|
return key
|
||||||
|
@ -1458,7 +1464,7 @@ func (v *Viper) Set(key string, value interface{}) {
|
||||||
func ReadInConfig() error { return v.ReadInConfig() }
|
func ReadInConfig() error { return v.ReadInConfig() }
|
||||||
|
|
||||||
func (v *Viper) ReadInConfig() error {
|
func (v *Viper) ReadInConfig() error {
|
||||||
jww.INFO.Println("Attempting to read in config file")
|
v.logger.Info("attempting to read in config file")
|
||||||
filename, err := v.getConfigFile()
|
filename, err := v.getConfigFile()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
@ -1468,7 +1474,7 @@ func (v *Viper) ReadInConfig() error {
|
||||||
return UnsupportedConfigError(v.getConfigType())
|
return UnsupportedConfigError(v.getConfigType())
|
||||||
}
|
}
|
||||||
|
|
||||||
jww.DEBUG.Println("Reading file: ", filename)
|
v.logger.Debug("reading file", "file", filename)
|
||||||
file, err := afero.ReadFile(v.fs, filename)
|
file, err := afero.ReadFile(v.fs, filename)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
@ -1489,7 +1495,7 @@ func (v *Viper) ReadInConfig() error {
|
||||||
func MergeInConfig() error { return v.MergeInConfig() }
|
func MergeInConfig() error { return v.MergeInConfig() }
|
||||||
|
|
||||||
func (v *Viper) MergeInConfig() error {
|
func (v *Viper) MergeInConfig() error {
|
||||||
jww.INFO.Println("Attempting to merge in config file")
|
v.logger.Info("attempting to merge in config file")
|
||||||
filename, err := v.getConfigFile()
|
filename, err := v.getConfigFile()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
@ -1580,7 +1586,8 @@ func (v *Viper) SafeWriteConfigAs(filename string) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (v *Viper) writeConfig(filename string, force bool) error {
|
func (v *Viper) writeConfig(filename string, force bool) error {
|
||||||
jww.INFO.Println("Attempting to write configuration to file.")
|
v.logger.Info("attempting to write configuration to file")
|
||||||
|
|
||||||
var configType string
|
var configType string
|
||||||
|
|
||||||
ext := filepath.Ext(filename)
|
ext := filepath.Ext(filename)
|
||||||
|
@ -1796,7 +1803,7 @@ func mergeMaps(
|
||||||
for sk, sv := range src {
|
for sk, sv := range src {
|
||||||
tk := keyExists(sk, tgt)
|
tk := keyExists(sk, tgt)
|
||||||
if tk == "" {
|
if tk == "" {
|
||||||
jww.TRACE.Printf("tk=\"\", tgt[%s]=%v", sk, sv)
|
v.logger.Trace("", "tk", "\"\"", fmt.Sprintf("tgt[%s]", sk), sv)
|
||||||
tgt[sk] = sv
|
tgt[sk] = sv
|
||||||
if itgt != nil {
|
if itgt != nil {
|
||||||
itgt[sk] = sv
|
itgt[sk] = sv
|
||||||
|
@ -1806,7 +1813,7 @@ func mergeMaps(
|
||||||
|
|
||||||
tv, ok := tgt[tk]
|
tv, ok := tgt[tk]
|
||||||
if !ok {
|
if !ok {
|
||||||
jww.TRACE.Printf("tgt[%s] != ok, tgt[%s]=%v", tk, sk, sv)
|
v.logger.Trace("", fmt.Sprintf("ok[%s]", tk), false, fmt.Sprintf("tgt[%s]", sk), sv)
|
||||||
tgt[sk] = sv
|
tgt[sk] = sv
|
||||||
if itgt != nil {
|
if itgt != nil {
|
||||||
itgt[sk] = sv
|
itgt[sk] = sv
|
||||||
|
@ -1817,27 +1824,38 @@ func mergeMaps(
|
||||||
svType := reflect.TypeOf(sv)
|
svType := reflect.TypeOf(sv)
|
||||||
tvType := reflect.TypeOf(tv)
|
tvType := reflect.TypeOf(tv)
|
||||||
if tvType != nil && svType != tvType { // Allow for the target to be nil
|
if tvType != nil && svType != tvType { // Allow for the target to be nil
|
||||||
jww.ERROR.Printf(
|
v.logger.Error(
|
||||||
"svType != tvType; key=%s, st=%v, tt=%v, sv=%v, tv=%v",
|
"svType != tvType",
|
||||||
sk, svType, tvType, sv, tv)
|
"key", sk,
|
||||||
|
"st", svType,
|
||||||
|
"tt", tvType,
|
||||||
|
"sv", sv,
|
||||||
|
"tv", tv,
|
||||||
|
)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
jww.TRACE.Printf("processing key=%s, st=%v, tt=%v, sv=%v, tv=%v",
|
v.logger.Trace(
|
||||||
sk, svType, tvType, sv, tv)
|
"processing",
|
||||||
|
"key", sk,
|
||||||
|
"st", svType,
|
||||||
|
"tt", tvType,
|
||||||
|
"sv", sv,
|
||||||
|
"tv", tv,
|
||||||
|
)
|
||||||
|
|
||||||
switch ttv := tv.(type) {
|
switch ttv := tv.(type) {
|
||||||
case map[interface{}]interface{}:
|
case map[interface{}]interface{}:
|
||||||
jww.TRACE.Printf("merging maps (must convert)")
|
v.logger.Trace("merging maps (must convert)")
|
||||||
tsv := sv.(map[interface{}]interface{})
|
tsv := sv.(map[interface{}]interface{})
|
||||||
ssv := castToMapStringInterface(tsv)
|
ssv := castToMapStringInterface(tsv)
|
||||||
stv := castToMapStringInterface(ttv)
|
stv := castToMapStringInterface(ttv)
|
||||||
mergeMaps(ssv, stv, ttv)
|
mergeMaps(ssv, stv, ttv)
|
||||||
case map[string]interface{}:
|
case map[string]interface{}:
|
||||||
jww.TRACE.Printf("merging maps")
|
v.logger.Trace("merging maps")
|
||||||
mergeMaps(sv.(map[string]interface{}), ttv, nil)
|
mergeMaps(sv.(map[string]interface{}), ttv, nil)
|
||||||
default:
|
default:
|
||||||
jww.TRACE.Printf("setting value")
|
v.logger.Trace("setting value")
|
||||||
tgt[tk] = sv
|
tgt[tk] = sv
|
||||||
if itgt != nil {
|
if itgt != nil {
|
||||||
itgt[tk] = sv
|
itgt[tk] = sv
|
||||||
|
@ -1872,7 +1890,7 @@ func (v *Viper) getKeyValueConfig() error {
|
||||||
for _, rp := range v.remoteProviders {
|
for _, rp := range v.remoteProviders {
|
||||||
val, err := v.getRemoteConfig(rp)
|
val, err := v.getRemoteConfig(rp)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
jww.ERROR.Printf("get remote config: %s", err)
|
v.logger.Error(fmt.Errorf("get remote config: %w", err).Error())
|
||||||
|
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,13 +9,12 @@ import (
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
|
||||||
"github.com/spf13/afero"
|
"github.com/spf13/afero"
|
||||||
jww "github.com/spf13/jwalterweatherman"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// Search all configPaths for any config file.
|
// Search all configPaths for any config file.
|
||||||
// Returns the first path that exists (and is a config file).
|
// Returns the first path that exists (and is a config file).
|
||||||
func (v *Viper) findConfigFile() (string, error) {
|
func (v *Viper) findConfigFile() (string, error) {
|
||||||
jww.INFO.Println("Searching for config in ", v.configPaths)
|
v.logger.Info("searching for config in paths", "paths", v.configPaths)
|
||||||
|
|
||||||
for _, cp := range v.configPaths {
|
for _, cp := range v.configPaths {
|
||||||
file := v.searchInPath(cp)
|
file := v.searchInPath(cp)
|
||||||
|
@ -27,11 +26,11 @@ func (v *Viper) findConfigFile() (string, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (v *Viper) searchInPath(in string) (filename string) {
|
func (v *Viper) searchInPath(in string) (filename string) {
|
||||||
jww.DEBUG.Println("Searching for config in ", in)
|
v.logger.Debug("searching for config in path", "path", in)
|
||||||
for _, ext := range SupportedExts {
|
for _, ext := range SupportedExts {
|
||||||
jww.DEBUG.Println("Checking for", filepath.Join(in, v.configName+"."+ext))
|
v.logger.Debug("checking if file exists", "file", filepath.Join(in, v.configName+"."+ext))
|
||||||
if b, _ := exists(v.fs, filepath.Join(in, v.configName+"."+ext)); b {
|
if b, _ := exists(v.fs, filepath.Join(in, v.configName+"."+ext)); b {
|
||||||
jww.DEBUG.Println("Found: ", filepath.Join(in, v.configName+"."+ext))
|
v.logger.Debug("found file", "file", filepath.Join(in, v.configName+"."+ext))
|
||||||
return filepath.Join(in, v.configName+"."+ext)
|
return filepath.Join(in, v.configName+"."+ext)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue