Merge tag 'upstream/1.2.1'
Upstream version 1.2.1
Dr. Tobias Quathamer
5 years ago
372 | 372 | |
373 | 373 | ### Remote Key/Value Store Example - Unencrypted |
374 | 374 | |
375 | #### etcd | |
375 | 376 | ```go |
376 | 377 | viper.AddRemoteProvider("etcd", "http://127.0.0.1:4001","/config/hugo.json") |
377 | 378 | viper.SetConfigType("json") // because there is no file extension in a stream of bytes, supported extensions are "json", "toml", "yaml", "yml", "properties", "props", "prop" |
378 | 379 | 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 | |
379 | 400 | ``` |
380 | 401 | |
381 | 402 | ### 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= |
305 | 305 | if err != nil { |
306 | 306 | log.Println("error:", err) |
307 | 307 | } |
308 | v.onConfigChange(event) | |
308 | if v.onConfigChange != nil { | |
309 | v.onConfigChange(event) | |
310 | } | |
309 | 311 | } |
310 | 312 | } |
311 | 313 | case err := <-watcher.Errors: |
647 | 649 | return cast.ToBool(val) |
648 | 650 | case string: |
649 | 651 | return cast.ToString(val) |
650 | case int64, int32, int16, int8, int: | |
652 | case int32, int16, int8, int: | |
651 | 653 | return cast.ToInt(val) |
654 | case int64: | |
655 | return cast.ToInt64(val) | |
652 | 656 | case float64, float32: |
653 | 657 | return cast.ToFloat64(val) |
654 | 658 | case time.Time: |
1138 | 1142 | deepestMap[lastKey] = value |
1139 | 1143 | } |
1140 | 1144 | |
1141 | // Set sets the value for the key in the override regiser. | |
1145 | // Set sets the value for the key in the override register. | |
1142 | 1146 | // Set is case-insensitive for a key. |
1143 | 1147 | // Will be used instead of values obtained via |
1144 | 1148 | // flags, config file, ENV, default, or key/value store. |
1101 | 1101 | t.Fatalf("pop != 37890, = %d", pop) |
1102 | 1102 | } |
1103 | 1103 | |
1104 | if pop := v.GetInt("hello.lagrenum"); pop != 765432101234567 { | |
1105 | t.Fatalf("lagrenum != 765432101234567, = %d", pop) | |
1106 | } | |
1107 | ||
1108 | 1104 | if pop := v.GetInt32("hello.pop"); pop != int32(37890) { |
1109 | 1105 | t.Fatalf("pop != 37890, = %d", pop) |
1110 | 1106 | } |
1127 | 1123 | |
1128 | 1124 | if pop := v.GetInt("hello.pop"); pop != 45000 { |
1129 | 1125 | t.Fatalf("pop != 45000, = %d", pop) |
1130 | } | |
1131 | ||
1132 | if pop := v.GetInt("hello.lagrenum"); pop != 7654321001234567 { | |
1133 | t.Fatalf("lagrenum != 7654321001234567, = %d", pop) | |
1134 | 1126 | } |
1135 | 1127 | |
1136 | 1128 | if pop := v.GetInt32("hello.pop"); pop != int32(45000) { |