@@ -127,11 +127,11 @@ You can handle the specific case where no config file is found like this:
127
127
128
128
``` go
129
129
if err := viper.ReadInConfig (); err != nil {
130
- if _ , ok := err.(viper.ConfigFileNotFoundError ); ok {
131
- // Config file not found; ignore error if desired
132
- } else {
133
- // Config file was found but another error was produced
134
- }
130
+ if _ , ok := err.(viper.ConfigFileNotFoundError ); ok {
131
+ // Config file not found; ignore error if desired
132
+ } else {
133
+ // Config file was found but another error was produced
134
+ }
135
135
}
136
136
137
137
// Config file found and successfully parsed
@@ -354,7 +354,7 @@ func main() {
354
354
355
355
i := viper.GetInt (" flagname" ) // retrieve value from viper
356
356
357
- ...
357
+ // ...
358
358
}
359
359
```
360
360
@@ -503,18 +503,18 @@ runtime_viper.Unmarshal(&runtime_conf)
503
503
// open a goroutine to watch remote changes forever
504
504
go func (){
505
505
for {
506
- time.Sleep (time.Second * 5 ) // delay after each request
507
-
508
- // currently, only tested with etcd support
509
- err := runtime_viper.WatchRemoteConfig ()
510
- if err != nil {
511
- log.Errorf (" unable to read remote config: %v " , err)
512
- continue
513
- }
514
-
515
- // unmarshal new config into our runtime config struct. you can also use channel
516
- // to implement a signal to notify the system of the changes
517
- runtime_viper.Unmarshal (&runtime_conf)
506
+ time.Sleep (time.Second * 5 ) // delay after each request
507
+
508
+ // currently, only tested with etcd support
509
+ err := runtime_viper.WatchRemoteConfig ()
510
+ if err != nil {
511
+ log.Errorf (" unable to read remote config: %v " , err)
512
+ continue
513
+ }
514
+
515
+ // unmarshal new config into our runtime config struct. you can also use channel
516
+ // to implement a signal to notify the system of the changes
517
+ runtime_viper.Unmarshal (&runtime_conf)
518
518
}
519
519
}()
520
520
```
@@ -546,7 +546,7 @@ Example:
546
546
``` go
547
547
viper.GetString (" logfile" ) // case-insensitive Setting & Getting
548
548
if viper.GetBool (" verbose" ) {
549
- fmt.Println (" verbose enabled" )
549
+ fmt.Println (" verbose enabled" )
550
550
}
551
551
```
552
552
### Accessing nested keys
@@ -669,7 +669,7 @@ So instead of doing that let's pass a Viper instance to the constructor that rep
669
669
` ` ` go
670
670
cache1Config := viper.Sub("cache.cache1")
671
671
if cache1Config == nil { // Sub returns nil if the key cannot be found
672
- panic("cache configuration not found")
672
+ panic("cache configuration not found")
673
673
}
674
674
675
675
cache1 := NewCache(cache1Config)
@@ -681,10 +681,10 @@ Internally, the `NewCache` function can address `max-items` and `item-size` keys
681
681
682
682
` ` ` go
683
683
func NewCache(v *Viper) *Cache {
684
- return &Cache{
685
- MaxItems: v.GetInt("max-items"),
686
- ItemSize: v.GetInt("item-size"),
687
- }
684
+ return &Cache{
685
+ MaxItems: v.GetInt("max-items"),
686
+ ItemSize: v.GetInt("item-size"),
687
+ }
688
688
}
689
689
` ` `
690
690
@@ -726,18 +726,18 @@ you have to change the delimiter:
726
726
v := viper.NewWithOptions (viper.KeyDelimiter (" ::" ))
727
727
728
728
v.SetDefault (" chart::values" , map [string ]interface {}{
729
- " ingress" : map [string ]interface {}{
730
- " annotations" : map [string ]interface {}{
731
- " traefik.frontend.rule.type" : " PathPrefix" ,
732
- " traefik.ingress.kubernetes.io/ssl-redirect" : " true" ,
733
- },
734
- },
729
+ " ingress" : map [string ]interface {}{
730
+ " annotations" : map [string ]interface {}{
731
+ " traefik.frontend.rule.type" : " PathPrefix" ,
732
+ " traefik.ingress.kubernetes.io/ssl-redirect" : " true" ,
733
+ },
734
+ },
735
735
})
736
736
737
737
type config struct {
738
738
Chart struct {
739
- Values map [string ]interface {}
740
- }
739
+ Values map [string ]interface {}
740
+ }
741
741
}
742
742
743
743
var C config
@@ -794,17 +794,17 @@ You can use your favorite format's marshaller with the config returned by `AllSe
794
794
795
795
``` go
796
796
import (
797
- yaml " gopkg.in/yaml.v2"
798
- // ...
797
+ yaml " gopkg.in/yaml.v2"
798
+ // ...
799
799
)
800
800
801
801
func yamlStringSettings () string {
802
- c := viper.AllSettings ()
803
- bs , err := yaml.Marshal (c)
804
- if err != nil {
805
- log.Fatalf (" unable to marshal config to YAML: %v " , err)
806
- }
807
- return string (bs)
802
+ c := viper.AllSettings ()
803
+ bs , err := yaml.Marshal (c)
804
+ if err != nil {
805
+ log.Fatalf (" unable to marshal config to YAML: %v " , err)
806
+ }
807
+ return string (bs)
808
808
}
809
809
```
810
810
0 commit comments