mirror of
https://github.com/spf13/viper
synced 2024-12-23 03:57:01 +00:00
docs: fix indentation in code samples
Signed-off-by: Mark Sagi-Kazar <mark.sagikazar@gmail.com>
This commit is contained in:
parent
0d7e8034ee
commit
65f16c1738
1 changed files with 38 additions and 38 deletions
76
README.md
76
README.md
|
@ -127,11 +127,11 @@ You can handle the specific case where no config file is found like this:
|
||||||
|
|
||||||
```go
|
```go
|
||||||
if err := viper.ReadInConfig(); err != nil {
|
if err := viper.ReadInConfig(); err != nil {
|
||||||
if _, ok := err.(viper.ConfigFileNotFoundError); ok {
|
if _, ok := err.(viper.ConfigFileNotFoundError); ok {
|
||||||
// Config file not found; ignore error if desired
|
// Config file not found; ignore error if desired
|
||||||
} else {
|
} else {
|
||||||
// Config file was found but another error was produced
|
// Config file was found but another error was produced
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Config file found and successfully parsed
|
// Config file found and successfully parsed
|
||||||
|
@ -354,7 +354,7 @@ func main() {
|
||||||
|
|
||||||
i := viper.GetInt("flagname") // retrieve value from viper
|
i := viper.GetInt("flagname") // retrieve value from viper
|
||||||
|
|
||||||
...
|
// ...
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -503,18 +503,18 @@ runtime_viper.Unmarshal(&runtime_conf)
|
||||||
// open a goroutine to watch remote changes forever
|
// open a goroutine to watch remote changes forever
|
||||||
go func(){
|
go func(){
|
||||||
for {
|
for {
|
||||||
time.Sleep(time.Second * 5) // delay after each request
|
time.Sleep(time.Second * 5) // delay after each request
|
||||||
|
|
||||||
// currently, only tested with etcd support
|
// currently, only tested with etcd support
|
||||||
err := runtime_viper.WatchRemoteConfig()
|
err := runtime_viper.WatchRemoteConfig()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Errorf("unable to read remote config: %v", err)
|
log.Errorf("unable to read remote config: %v", err)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
// unmarshal new config into our runtime config struct. you can also use channel
|
// unmarshal new config into our runtime config struct. you can also use channel
|
||||||
// to implement a signal to notify the system of the changes
|
// to implement a signal to notify the system of the changes
|
||||||
runtime_viper.Unmarshal(&runtime_conf)
|
runtime_viper.Unmarshal(&runtime_conf)
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
```
|
```
|
||||||
|
@ -546,7 +546,7 @@ Example:
|
||||||
```go
|
```go
|
||||||
viper.GetString("logfile") // case-insensitive Setting & Getting
|
viper.GetString("logfile") // case-insensitive Setting & Getting
|
||||||
if viper.GetBool("verbose") {
|
if viper.GetBool("verbose") {
|
||||||
fmt.Println("verbose enabled")
|
fmt.Println("verbose enabled")
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
### Accessing nested keys
|
### Accessing nested keys
|
||||||
|
@ -669,7 +669,7 @@ So instead of doing that let's pass a Viper instance to the constructor that rep
|
||||||
```go
|
```go
|
||||||
cache1Config := viper.Sub("cache.cache1")
|
cache1Config := viper.Sub("cache.cache1")
|
||||||
if cache1Config == nil { // Sub returns nil if the key cannot be found
|
if cache1Config == nil { // Sub returns nil if the key cannot be found
|
||||||
panic("cache configuration not found")
|
panic("cache configuration not found")
|
||||||
}
|
}
|
||||||
|
|
||||||
cache1 := NewCache(cache1Config)
|
cache1 := NewCache(cache1Config)
|
||||||
|
@ -681,10 +681,10 @@ Internally, the `NewCache` function can address `max-items` and `item-size` keys
|
||||||
|
|
||||||
```go
|
```go
|
||||||
func NewCache(v *Viper) *Cache {
|
func NewCache(v *Viper) *Cache {
|
||||||
return &Cache{
|
return &Cache{
|
||||||
MaxItems: v.GetInt("max-items"),
|
MaxItems: v.GetInt("max-items"),
|
||||||
ItemSize: v.GetInt("item-size"),
|
ItemSize: v.GetInt("item-size"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -726,18 +726,18 @@ you have to change the delimiter:
|
||||||
v := viper.NewWithOptions(viper.KeyDelimiter("::"))
|
v := viper.NewWithOptions(viper.KeyDelimiter("::"))
|
||||||
|
|
||||||
v.SetDefault("chart::values", map[string]interface{}{
|
v.SetDefault("chart::values", map[string]interface{}{
|
||||||
"ingress": map[string]interface{}{
|
"ingress": map[string]interface{}{
|
||||||
"annotations": map[string]interface{}{
|
"annotations": map[string]interface{}{
|
||||||
"traefik.frontend.rule.type": "PathPrefix",
|
"traefik.frontend.rule.type": "PathPrefix",
|
||||||
"traefik.ingress.kubernetes.io/ssl-redirect": "true",
|
"traefik.ingress.kubernetes.io/ssl-redirect": "true",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
|
||||||
type config struct {
|
type config struct {
|
||||||
Chart struct{
|
Chart struct{
|
||||||
Values map[string]interface{}
|
Values map[string]interface{}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var C config
|
var C config
|
||||||
|
@ -794,17 +794,17 @@ You can use your favorite format's marshaller with the config returned by `AllSe
|
||||||
|
|
||||||
```go
|
```go
|
||||||
import (
|
import (
|
||||||
yaml "gopkg.in/yaml.v2"
|
yaml "gopkg.in/yaml.v2"
|
||||||
// ...
|
// ...
|
||||||
)
|
)
|
||||||
|
|
||||||
func yamlStringSettings() string {
|
func yamlStringSettings() string {
|
||||||
c := viper.AllSettings()
|
c := viper.AllSettings()
|
||||||
bs, err := yaml.Marshal(c)
|
bs, err := yaml.Marshal(c)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("unable to marshal config to YAML: %v", err)
|
log.Fatalf("unable to marshal config to YAML: %v", err)
|
||||||
}
|
}
|
||||||
return string(bs)
|
return string(bs)
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue