Skip to content

Commit ed0a967

Browse files
moorereasonMax Wolter
authored and
Max Wolter
committed
Fix retrieval of pflag stringSlice (#240)
Fixes #112 Changes some logging directives to use Printf
1 parent a78f70b commit ed0a967

File tree

1 file changed

+13
-10
lines changed

1 file changed

+13
-10
lines changed

viper.go

+13-10
Original file line numberDiff line numberDiff line change
@@ -749,45 +749,48 @@ func (v *Viper) find(key string) interface{} {
749749
// PFlag Override first
750750
flag, exists := v.pflags[key]
751751
if exists && flag.HasChanged() {
752-
jww.TRACE.Println(key, "found in override (via pflag):", flag.ValueString())
752+
jww.TRACE.Printf("%q found in pflag override (%s): %s", key, flag.ValueType(), flag.ValueString())
753753
switch flag.ValueType() {
754754
case "int", "int8", "int16", "int32", "int64":
755755
return cast.ToInt(flag.ValueString())
756756
case "bool":
757757
return cast.ToBool(flag.ValueString())
758+
case "stringSlice":
759+
s := strings.TrimPrefix(flag.ValueString(), "[")
760+
return strings.TrimSuffix(s, "]")
758761
default:
759762
return flag.ValueString()
760763
}
761764
}
762765

763766
val, exists = v.override[key]
764767
if exists {
765-
jww.TRACE.Println(key, "found in override:", val)
768+
jww.TRACE.Printf("%q found in override: %s", key, val)
766769
return val
767770
}
768771

769772
if v.automaticEnvApplied {
770773
// even if it hasn't been registered, if automaticEnv is used,
771774
// check any Get request
772775
if val = v.getEnv(v.mergeWithEnvPrefix(key)); val != "" {
773-
jww.TRACE.Println(key, "found in environment with val:", val)
776+
jww.TRACE.Printf("%q found in environment: %s", key, val)
774777
return val
775778
}
776779
}
777780

778781
envkey, exists := v.env[key]
779782
if exists {
780-
jww.TRACE.Println(key, "registered as env var", envkey)
783+
jww.TRACE.Printf("%q registered as env var %q", key, envkey)
781784
if val = v.getEnv(envkey); val != "" {
782-
jww.TRACE.Println(envkey, "found in environment with val:", val)
785+
jww.TRACE.Printf("%q found in environment: %s", envkey, val)
783786
return val
784787
}
785-
jww.TRACE.Println(envkey, "env value unset:")
788+
jww.TRACE.Printf("%q env value unset", envkey)
786789
}
787790

788791
val, exists = v.config[key]
789792
if exists {
790-
jww.TRACE.Println(key, "found in config:", val)
793+
jww.TRACE.Printf("%q found in config (%T): %s", key, val, val)
791794
return val
792795
}
793796

@@ -800,7 +803,7 @@ func (v *Viper) find(key string) interface{} {
800803
if reflect.TypeOf(source).Kind() == reflect.Map {
801804
val := v.searchMap(cast.ToStringMap(source), path[1:])
802805
if val != nil {
803-
jww.TRACE.Println(key, "found in nested config:", val)
806+
jww.TRACE.Printf("%q found in nested config: %s", key, val)
804807
return val
805808
}
806809
}
@@ -809,13 +812,13 @@ func (v *Viper) find(key string) interface{} {
809812

810813
val, exists = v.kvstore[key]
811814
if exists {
812-
jww.TRACE.Println(key, "found in key/value store:", val)
815+
jww.TRACE.Printf("%q found in key/value store: %s", key, val)
813816
return val
814817
}
815818

816819
val, exists = v.defaults[key]
817820
if exists {
818-
jww.TRACE.Println(key, "found in defaults:", val)
821+
jww.TRACE.Printf("%q found in defaults: ", key, val)
819822
return val
820823
}
821824

0 commit comments

Comments
 (0)