Skip to content

Commit a785a79

Browse files
committed
refactor: replace jww with the new logger interface
1 parent f1f6b21 commit a785a79

File tree

4 files changed

+49
-33
lines changed

4 files changed

+49
-33
lines changed

util.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ import (
1919
"unicode"
2020

2121
"github.com/spf13/cast"
22-
jww "github.com/spf13/jwalterweatherman"
2322
)
2423

2524
// ConfigParseError denotes failing to parse configuration file.
@@ -87,8 +86,8 @@ func insensitiviseMap(m map[string]interface{}) {
8786
}
8887
}
8988

90-
func absPathify(inPath string) string {
91-
jww.INFO.Println("Trying to resolve absolute path to", inPath)
89+
func absPathify(logger Logger, inPath string) string {
90+
logger.Info("trying to resolve absolute path", "path", inPath)
9291

9392
if inPath == "$HOME" || strings.HasPrefix(inPath, "$HOME"+string(os.PathSeparator)) {
9493
inPath = userHomeDir() + inPath[5:]
@@ -105,8 +104,8 @@ func absPathify(inPath string) string {
105104
return filepath.Clean(p)
106105
}
107106

108-
jww.ERROR.Println("Couldn't discover absolute path")
109-
jww.ERROR.Println(err)
107+
logger.Error(fmt.Errorf("could not discover absolute path: %w", err).Error())
108+
110109
return ""
111110
}
112111

util_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ func TestAbsPathify(t *testing.T) {
8787
}
8888

8989
for _, test := range tests {
90-
got := absPathify(test.input)
90+
got := absPathify(jwwLogger{}, test.input)
9191
if got != test.output {
9292
t.Errorf("Got %v\nexpected\n%q", got, test.output)
9393
}

viper.go

Lines changed: 40 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ import (
3939
"github.com/mitchellh/mapstructure"
4040
"github.com/spf13/afero"
4141
"github.com/spf13/cast"
42-
jww "github.com/spf13/jwalterweatherman"
4342
"github.com/spf13/pflag"
4443
"github.com/subosito/gotenv"
4544
"gopkg.in/ini.v1"
@@ -260,6 +259,8 @@ type Viper struct {
260259
properties *properties.Properties
261260

262261
onConfigChange func(fsnotify.Event)
262+
263+
logger Logger
263264
}
264265

265266
// New returns an initialized Viper instance.
@@ -277,6 +278,7 @@ func New() *Viper {
277278
v.env = make(map[string][]string)
278279
v.aliases = make(map[string]string)
279280
v.typeByDefValue = false
281+
v.logger = jwwLogger{}
280282

281283
return v
282284
}
@@ -517,8 +519,9 @@ func AddConfigPath(in string) { v.AddConfigPath(in) }
517519

518520
func (v *Viper) AddConfigPath(in string) {
519521
if in != "" {
520-
absin := absPathify(in)
521-
jww.INFO.Println("adding", absin, "to paths to search")
522+
absin := absPathify(v.logger, in)
523+
524+
v.logger.Info("adding path to search paths", "path", absin)
522525
if !stringInSlice(absin, v.configPaths) {
523526
v.configPaths = append(v.configPaths, absin)
524527
}
@@ -542,7 +545,8 @@ func (v *Viper) AddRemoteProvider(provider, endpoint, path string) error {
542545
return UnsupportedRemoteProviderError(provider)
543546
}
544547
if provider != "" && endpoint != "" {
545-
jww.INFO.Printf("adding %s:%s to remote provider list", provider, endpoint)
548+
v.logger.Info("adding remote provider", "provider", provider, "endpoint", endpoint)
549+
546550
rp := &defaultRemoteProvider{
547551
endpoint: endpoint,
548552
provider: provider,
@@ -574,7 +578,8 @@ func (v *Viper) AddSecureRemoteProvider(provider, endpoint, path, secretkeyring
574578
return UnsupportedRemoteProviderError(provider)
575579
}
576580
if provider != "" && endpoint != "" {
577-
jww.INFO.Printf("adding %s:%s to remote provider list", provider, endpoint)
581+
v.logger.Info("adding remote provider", "provider", provider, "endpoint", endpoint)
582+
578583
rp := &defaultRemoteProvider{
579584
endpoint: endpoint,
580585
provider: provider,
@@ -1390,14 +1395,15 @@ func (v *Viper) registerAlias(alias string, key string) {
13901395
v.aliases[alias] = key
13911396
}
13921397
} else {
1393-
jww.WARN.Println("Creating circular reference alias", alias, key, v.realKey(key))
1398+
v.logger.Warn("creating circular reference alias", "alias", alias, "key", key, "real_key", v.realKey(key))
13941399
}
13951400
}
13961401

13971402
func (v *Viper) realKey(key string) string {
13981403
newkey, exists := v.aliases[key]
13991404
if exists {
1400-
jww.DEBUG.Println("Alias", key, "to", newkey)
1405+
v.logger.Debug("key is an alias", "alias", key, "to", newkey)
1406+
14011407
return v.realKey(newkey)
14021408
}
14031409
return key
@@ -1458,7 +1464,7 @@ func (v *Viper) Set(key string, value interface{}) {
14581464
func ReadInConfig() error { return v.ReadInConfig() }
14591465

14601466
func (v *Viper) ReadInConfig() error {
1461-
jww.INFO.Println("Attempting to read in config file")
1467+
v.logger.Info("attempting to read in config file")
14621468
filename, err := v.getConfigFile()
14631469
if err != nil {
14641470
return err
@@ -1468,7 +1474,7 @@ func (v *Viper) ReadInConfig() error {
14681474
return UnsupportedConfigError(v.getConfigType())
14691475
}
14701476

1471-
jww.DEBUG.Println("Reading file: ", filename)
1477+
v.logger.Debug("reading file", "file", filename)
14721478
file, err := afero.ReadFile(v.fs, filename)
14731479
if err != nil {
14741480
return err
@@ -1489,7 +1495,7 @@ func (v *Viper) ReadInConfig() error {
14891495
func MergeInConfig() error { return v.MergeInConfig() }
14901496

14911497
func (v *Viper) MergeInConfig() error {
1492-
jww.INFO.Println("Attempting to merge in config file")
1498+
v.logger.Info("attempting to merge in config file")
14931499
filename, err := v.getConfigFile()
14941500
if err != nil {
14951501
return err
@@ -1580,7 +1586,8 @@ func (v *Viper) SafeWriteConfigAs(filename string) error {
15801586
}
15811587

15821588
func (v *Viper) writeConfig(filename string, force bool) error {
1583-
jww.INFO.Println("Attempting to write configuration to file.")
1589+
v.logger.Info("attempting to write configuration to file")
1590+
15841591
var configType string
15851592

15861593
ext := filepath.Ext(filename)
@@ -1796,7 +1803,7 @@ func mergeMaps(
17961803
for sk, sv := range src {
17971804
tk := keyExists(sk, tgt)
17981805
if tk == "" {
1799-
jww.TRACE.Printf("tk=\"\", tgt[%s]=%v", sk, sv)
1806+
v.logger.Trace("", "tk", "\"\"", fmt.Sprintf("tgt[%s]", sk), sv)
18001807
tgt[sk] = sv
18011808
if itgt != nil {
18021809
itgt[sk] = sv
@@ -1806,7 +1813,7 @@ func mergeMaps(
18061813

18071814
tv, ok := tgt[tk]
18081815
if !ok {
1809-
jww.TRACE.Printf("tgt[%s] != ok, tgt[%s]=%v", tk, sk, sv)
1816+
v.logger.Trace("", fmt.Sprintf("ok[%s]", tk), false, fmt.Sprintf("tgt[%s]", sk), sv)
18101817
tgt[sk] = sv
18111818
if itgt != nil {
18121819
itgt[sk] = sv
@@ -1817,27 +1824,38 @@ func mergeMaps(
18171824
svType := reflect.TypeOf(sv)
18181825
tvType := reflect.TypeOf(tv)
18191826
if tvType != nil && svType != tvType { // Allow for the target to be nil
1820-
jww.ERROR.Printf(
1821-
"svType != tvType; key=%s, st=%v, tt=%v, sv=%v, tv=%v",
1822-
sk, svType, tvType, sv, tv)
1827+
v.logger.Error(
1828+
"svType != tvType",
1829+
"key", sk,
1830+
"st", svType,
1831+
"tt", tvType,
1832+
"sv", sv,
1833+
"tv", tv,
1834+
)
18231835
continue
18241836
}
18251837

1826-
jww.TRACE.Printf("processing key=%s, st=%v, tt=%v, sv=%v, tv=%v",
1827-
sk, svType, tvType, sv, tv)
1838+
v.logger.Trace(
1839+
"processing",
1840+
"key", sk,
1841+
"st", svType,
1842+
"tt", tvType,
1843+
"sv", sv,
1844+
"tv", tv,
1845+
)
18281846

18291847
switch ttv := tv.(type) {
18301848
case map[interface{}]interface{}:
1831-
jww.TRACE.Printf("merging maps (must convert)")
1849+
v.logger.Trace("merging maps (must convert)")
18321850
tsv := sv.(map[interface{}]interface{})
18331851
ssv := castToMapStringInterface(tsv)
18341852
stv := castToMapStringInterface(ttv)
18351853
mergeMaps(ssv, stv, ttv)
18361854
case map[string]interface{}:
1837-
jww.TRACE.Printf("merging maps")
1855+
v.logger.Trace("merging maps")
18381856
mergeMaps(sv.(map[string]interface{}), ttv, nil)
18391857
default:
1840-
jww.TRACE.Printf("setting value")
1858+
v.logger.Trace("setting value")
18411859
tgt[tk] = sv
18421860
if itgt != nil {
18431861
itgt[tk] = sv
@@ -1872,7 +1890,7 @@ func (v *Viper) getKeyValueConfig() error {
18721890
for _, rp := range v.remoteProviders {
18731891
val, err := v.getRemoteConfig(rp)
18741892
if err != nil {
1875-
jww.ERROR.Printf("get remote config: %s", err)
1893+
v.logger.Error(fmt.Errorf("get remote config: %w", err).Error())
18761894

18771895
continue
18781896
}

viper_go1_15.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,12 @@ import (
99
"path/filepath"
1010

1111
"github.com/spf13/afero"
12-
jww "github.com/spf13/jwalterweatherman"
1312
)
1413

1514
// Search all configPaths for any config file.
1615
// Returns the first path that exists (and is a config file).
1716
func (v *Viper) findConfigFile() (string, error) {
18-
jww.INFO.Println("Searching for config in ", v.configPaths)
17+
v.logger.Info("searching for config in paths", "paths", v.configPaths)
1918

2019
for _, cp := range v.configPaths {
2120
file := v.searchInPath(cp)
@@ -27,11 +26,11 @@ func (v *Viper) findConfigFile() (string, error) {
2726
}
2827

2928
func (v *Viper) searchInPath(in string) (filename string) {
30-
jww.DEBUG.Println("Searching for config in ", in)
29+
v.logger.Debug("searching for config in path", "path", in)
3130
for _, ext := range SupportedExts {
32-
jww.DEBUG.Println("Checking for", filepath.Join(in, v.configName+"."+ext))
31+
v.logger.Debug("checking if file exists", "file", filepath.Join(in, v.configName+"."+ext))
3332
if b, _ := exists(v.fs, filepath.Join(in, v.configName+"."+ext)); b {
34-
jww.DEBUG.Println("Found: ", filepath.Join(in, v.configName+"."+ext))
33+
v.logger.Debug("found file", "file", filepath.Join(in, v.configName+"."+ext))
3534
return filepath.Join(in, v.configName+"."+ext)
3635
}
3736
}

0 commit comments

Comments
 (0)