e.g I want get the hobbies(in README) value from env.
This is a good question. So we can retrieve this list like this: viper.GetStringSlice("hobbies").
Example Code:
package main
import (
"fmt"
"github.com/spf13/viper"
"bytes"
"strings"
)
var yamlExample = []byte(`
name: steve
hobbies:
- skateboarding
- snowboarding
`)
func main(){
viper.SetConfigType("yaml")
viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
viper.AutomaticEnv()
viper.ReadConfig(bytes.NewBuffer(yamlExample))
t := viper.GetStringSlice("hobbies")
fmt.Printf( "Type: %T, Size: %d \n", t, len(t) )
for i, v := range t {
fmt.Printf("Index: %d, Value: %v\n", i, v )
}
}
When I run this without setting an environment variable override I get:
$ go run main.go
Type: []string, Size: 2
Index: 0, Value: skateboarding
Index: 1, Value: snowboarding
Overriding this through env:
$ HOBBIES="devops go docker ansible" go run main.go
Type: []string, Size: 4
Index: 0, Value: devops
Index: 1, Value: go
Index: 2, Value: docker
Index: 3, Value: ansible
@spf13 perhaps we should include this in the main README.md? Also what if we have a list of maps, how can we override that?
Also what if we have a list of maps, how can we override that?
Is it possible?
@spf13 perhaps we should include this in the main README.md? Also what if we have a list of maps, how can we override that?
Definitely, just spent quite some time going through the code to find how to pass stringslices through ENV...
Also some way to support spaces in the slices would be nice
Also what if we have a list of maps, how can we override that?
Has anyone figured this out?
Also what if we have a list of maps, how can we override that?
Has anyone figured this out?
going back to code to find that as I needed as well (but haven't tested yet) https://github.com/spf13/cast/blob/master/caste.go#L876
So if we're using GetStringMap it ends there where, if value is string, it will be parsed as JSON... 馃憤 for doc updates again...
Also what if we have a list of maps, how can we override that?
Has anyone figured this out?
going back to code to find that as I needed as well (but haven't tested yet) https://github.com/spf13/cast/blob/master/caste.go#L876
So if we're using
GetStringMapit ends there where, if value is string, it will be parsed as JSON... 馃憤 for doc updates again...
Hey we tried to use 'GetStringMap' an dalso 'GetStringMapStringSlice' for the list of maps variables but not able to get any luck. Please help us by giving some example that how can we fetch the value of list of map variable.
For Example- variable will look like-
backend-pool-ip-addresses = [{"IpAddress" = "10.0.0.5"}, {"IpAddress" = "10.0.0.6"}]
which is reading like- backend-pool-ip-addresses:[map[IpAddress:10.0.0.5] map[IpAddress:10.0.0.6]]
If anyone can help us as this is imp and urgent project delivery is pending.
Most helpful comment
This is a good question. So we can retrieve this list like this:
viper.GetStringSlice("hobbies").Example Code:
When I run this without setting an environment variable override I get:
Overriding this through env:
@spf13 perhaps we should include this in the main README.md? Also what if we have a list of maps, how can we override that?