Codebase list golang-github-spf13-viper / b682660
Merge tag 'upstream/1.2.1' Upstream version 1.2.1 Dr. Tobias Quathamer 5 years ago
6 changed file(s) with 71 addition(s) and 12 deletion(s). Raw diff Collapse all Expand all
11
22 language: go
33 go:
4 - 1.9.x
54 - 1.10.x
5 - 1.11.x
66 - tip
77
88 os:
372372
373373 ### Remote Key/Value Store Example - Unencrypted
374374
375 #### etcd
375376 ```go
376377 viper.AddRemoteProvider("etcd", "http://127.0.0.1:4001","/config/hugo.json")
377378 viper.SetConfigType("json") // because there is no file extension in a stream of bytes, supported extensions are "json", "toml", "yaml", "yml", "properties", "props", "prop"
378379 err := viper.ReadRemoteConfig()
380 ```
381
382 #### Consul
383 You need to set a key to Consul key/value storage with JSON value containing your desired config.
384 For example, create a Consul key/value store key `MY_CONSUL_KEY` with value:
385
386 ```json
387 {
388 "port": 8080,
389 "hostname": "myhostname.com"
390 }
391 ```
392
393 ```go
394 viper.AddRemoteProvider("consul", "localhost:8500", "MY_CONSUL_KEY")
395 viper.SetConfigType("json") // Need to explicitly set this to json
396 err := viper.ReadRemoteConfig()
397
398 fmt.Println(viper.Get("port")) // 8080
399 fmt.Println(viper.Get("hostname")) // myhostname.com
379400 ```
380401
381402 ### Remote Key/Value Store Example - Encrypted
0 module github.com/spf13/viper
1
2 require (
3 github.com/fsnotify/fsnotify v1.4.7
4 github.com/hashicorp/hcl v1.0.0
5 github.com/magiconair/properties v1.8.0
6 github.com/mitchellh/mapstructure v1.0.0
7 github.com/pelletier/go-toml v1.2.0
8 github.com/spf13/afero v1.1.2
9 github.com/spf13/cast v1.2.0
10 github.com/spf13/jwalterweatherman v1.0.0
11 github.com/spf13/pflag v1.0.2
12 golang.org/x/sys v0.0.0-20180906133057-8cf3aee42992 // indirect
13 golang.org/x/text v0.3.0 // indirect
14 gopkg.in/yaml.v2 v2.2.1
15 )
0 github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
1 github.com/fsnotify/fsnotify v1.4.7 h1:IXs+QLmnXW2CcXuY+8Mzv/fWEsPGWxqefPtCP5CnV9I=
2 github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
3 github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4=
4 github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
5 github.com/magiconair/properties v1.8.0 h1:LLgXmsheXeRoUOBOjtwPQCWIYqM/LU1ayDtDePerRcY=
6 github.com/magiconair/properties v1.8.0/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ=
7 github.com/mitchellh/mapstructure v1.0.0 h1:vVpGvMXJPqSDh2VYHF7gsfQj8Ncx+Xw5Y1KHeTRY+7I=
8 github.com/mitchellh/mapstructure v1.0.0/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
9 github.com/pelletier/go-toml v1.2.0 h1:T5zMGML61Wp+FlcbWjRDT7yAxhJNAiPPLOFECq181zc=
10 github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic=
11 github.com/spf13/afero v1.1.2 h1:m8/z1t7/fwjysjQRYbP0RD+bUIF/8tJwPdEZsI83ACI=
12 github.com/spf13/afero v1.1.2/go.mod h1:j4pytiNVoe2o6bmDsKpLACNPDBIoEAkihy7loJ1B0CQ=
13 github.com/spf13/cast v1.2.0 h1:HHl1DSRbEQN2i8tJmtS6ViPyHx35+p51amrdsiTCrkg=
14 github.com/spf13/cast v1.2.0/go.mod h1:r2rcYCSwa1IExKTDiTfzaxqT2FNHs8hODu4LnUfgKEg=
15 github.com/spf13/jwalterweatherman v1.0.0 h1:XHEdyB+EcvlqZamSM4ZOMGlc93t6AcsBEu9Gc1vn7yk=
16 github.com/spf13/jwalterweatherman v1.0.0/go.mod h1:cQK4TGJAtQXfYWX+Ddv3mKDzgVb68N+wFjFa4jdeBTo=
17 github.com/spf13/pflag v1.0.2 h1:Fy0orTDgHdbnzHcsOgfCN4LtHf0ec3wwtiwJqwvf3Gc=
18 github.com/spf13/pflag v1.0.2/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4=
19 golang.org/x/sys v0.0.0-20180906133057-8cf3aee42992 h1:BH3eQWeGbwRU2+wxxuuPOdFBmaiBH81O8BugSjHeTFg=
20 golang.org/x/sys v0.0.0-20180906133057-8cf3aee42992/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
21 golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=
22 golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
23 gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
24 gopkg.in/yaml.v2 v2.2.1 h1:mUhvW9EsL+naU5Q3cakzfE91YhliOondGd6ZrsDBHQE=
25 gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
305305 if err != nil {
306306 log.Println("error:", err)
307307 }
308 v.onConfigChange(event)
308 if v.onConfigChange != nil {
309 v.onConfigChange(event)
310 }
309311 }
310312 }
311313 case err := <-watcher.Errors:
647649 return cast.ToBool(val)
648650 case string:
649651 return cast.ToString(val)
650 case int64, int32, int16, int8, int:
652 case int32, int16, int8, int:
651653 return cast.ToInt(val)
654 case int64:
655 return cast.ToInt64(val)
652656 case float64, float32:
653657 return cast.ToFloat64(val)
654658 case time.Time:
11381142 deepestMap[lastKey] = value
11391143 }
11401144
1141 // Set sets the value for the key in the override regiser.
1145 // Set sets the value for the key in the override register.
11421146 // Set is case-insensitive for a key.
11431147 // Will be used instead of values obtained via
11441148 // flags, config file, ENV, default, or key/value store.
11011101 t.Fatalf("pop != 37890, = %d", pop)
11021102 }
11031103
1104 if pop := v.GetInt("hello.lagrenum"); pop != 765432101234567 {
1105 t.Fatalf("lagrenum != 765432101234567, = %d", pop)
1106 }
1107
11081104 if pop := v.GetInt32("hello.pop"); pop != int32(37890) {
11091105 t.Fatalf("pop != 37890, = %d", pop)
11101106 }
11271123
11281124 if pop := v.GetInt("hello.pop"); pop != 45000 {
11291125 t.Fatalf("pop != 45000, = %d", pop)
1130 }
1131
1132 if pop := v.GetInt("hello.lagrenum"); pop != 7654321001234567 {
1133 t.Fatalf("lagrenum != 7654321001234567, = %d", pop)
11341126 }
11351127
11361128 if pop := v.GetInt32("hello.pop"); pop != int32(45000) {