mirror of
https://github.com/spf13/viper
synced 2024-12-22 11:37:02 +00:00
Merge branch 'master' into mergefix/Issue284_Kubernetes_config
This commit is contained in:
commit
8e194e8ad2
6 changed files with 68 additions and 11 deletions
|
@ -2,8 +2,8 @@ go_import_path: github.com/spf13/viper
|
||||||
|
|
||||||
language: go
|
language: go
|
||||||
go:
|
go:
|
||||||
- 1.9.x
|
|
||||||
- 1.10.x
|
- 1.10.x
|
||||||
|
- 1.11.x
|
||||||
- tip
|
- tip
|
||||||
|
|
||||||
os:
|
os:
|
||||||
|
|
21
README.md
21
README.md
|
@ -373,12 +373,33 @@ how to use Consul.
|
||||||
|
|
||||||
### Remote Key/Value Store Example - Unencrypted
|
### Remote Key/Value Store Example - Unencrypted
|
||||||
|
|
||||||
|
#### etcd
|
||||||
```go
|
```go
|
||||||
viper.AddRemoteProvider("etcd", "http://127.0.0.1:4001","/config/hugo.json")
|
viper.AddRemoteProvider("etcd", "http://127.0.0.1:4001","/config/hugo.json")
|
||||||
viper.SetConfigType("json") // because there is no file extension in a stream of bytes, supported extensions are "json", "toml", "yaml", "yml", "properties", "props", "prop"
|
viper.SetConfigType("json") // because there is no file extension in a stream of bytes, supported extensions are "json", "toml", "yaml", "yml", "properties", "props", "prop"
|
||||||
err := viper.ReadRemoteConfig()
|
err := viper.ReadRemoteConfig()
|
||||||
```
|
```
|
||||||
|
|
||||||
|
#### Consul
|
||||||
|
You need to set a key to Consul key/value storage with JSON value containing your desired config.
|
||||||
|
For example, create a Consul key/value store key `MY_CONSUL_KEY` with value:
|
||||||
|
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"port": 8080,
|
||||||
|
"hostname": "myhostname.com"
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
```go
|
||||||
|
viper.AddRemoteProvider("consul", "localhost:8500", "MY_CONSUL_KEY")
|
||||||
|
viper.SetConfigType("json") // Need to explicitly set this to json
|
||||||
|
err := viper.ReadRemoteConfig()
|
||||||
|
|
||||||
|
fmt.Println(viper.Get("port")) // 8080
|
||||||
|
fmt.Println(viper.Get("hostname")) // myhostname.com
|
||||||
|
```
|
||||||
|
|
||||||
### Remote Key/Value Store Example - Encrypted
|
### Remote Key/Value Store Example - Encrypted
|
||||||
|
|
||||||
```go
|
```go
|
||||||
|
|
16
go.mod
Normal file
16
go.mod
Normal file
|
@ -0,0 +1,16 @@
|
||||||
|
module github.com/spf13/viper
|
||||||
|
|
||||||
|
require (
|
||||||
|
github.com/fsnotify/fsnotify v1.4.7
|
||||||
|
github.com/hashicorp/hcl v1.0.0
|
||||||
|
github.com/magiconair/properties v1.8.0
|
||||||
|
github.com/mitchellh/mapstructure v1.0.0
|
||||||
|
github.com/pelletier/go-toml v1.2.0
|
||||||
|
github.com/spf13/afero v1.1.2
|
||||||
|
github.com/spf13/cast v1.2.0
|
||||||
|
github.com/spf13/jwalterweatherman v1.0.0
|
||||||
|
github.com/spf13/pflag v1.0.2
|
||||||
|
golang.org/x/sys v0.0.0-20180906133057-8cf3aee42992 // indirect
|
||||||
|
golang.org/x/text v0.3.0 // indirect
|
||||||
|
gopkg.in/yaml.v2 v2.2.1
|
||||||
|
)
|
26
go.sum
Normal file
26
go.sum
Normal file
|
@ -0,0 +1,26 @@
|
||||||
|
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||||
|
github.com/fsnotify/fsnotify v1.4.7 h1:IXs+QLmnXW2CcXuY+8Mzv/fWEsPGWxqefPtCP5CnV9I=
|
||||||
|
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
|
||||||
|
github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4=
|
||||||
|
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
|
||||||
|
github.com/magiconair/properties v1.8.0 h1:LLgXmsheXeRoUOBOjtwPQCWIYqM/LU1ayDtDePerRcY=
|
||||||
|
github.com/magiconair/properties v1.8.0/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ=
|
||||||
|
github.com/mitchellh/mapstructure v1.0.0 h1:vVpGvMXJPqSDh2VYHF7gsfQj8Ncx+Xw5Y1KHeTRY+7I=
|
||||||
|
github.com/mitchellh/mapstructure v1.0.0/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
|
||||||
|
github.com/pelletier/go-toml v1.2.0 h1:T5zMGML61Wp+FlcbWjRDT7yAxhJNAiPPLOFECq181zc=
|
||||||
|
github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic=
|
||||||
|
github.com/spf13/afero v1.1.2 h1:m8/z1t7/fwjysjQRYbP0RD+bUIF/8tJwPdEZsI83ACI=
|
||||||
|
github.com/spf13/afero v1.1.2/go.mod h1:j4pytiNVoe2o6bmDsKpLACNPDBIoEAkihy7loJ1B0CQ=
|
||||||
|
github.com/spf13/cast v1.2.0 h1:HHl1DSRbEQN2i8tJmtS6ViPyHx35+p51amrdsiTCrkg=
|
||||||
|
github.com/spf13/cast v1.2.0/go.mod h1:r2rcYCSwa1IExKTDiTfzaxqT2FNHs8hODu4LnUfgKEg=
|
||||||
|
github.com/spf13/jwalterweatherman v1.0.0 h1:XHEdyB+EcvlqZamSM4ZOMGlc93t6AcsBEu9Gc1vn7yk=
|
||||||
|
github.com/spf13/jwalterweatherman v1.0.0/go.mod h1:cQK4TGJAtQXfYWX+Ddv3mKDzgVb68N+wFjFa4jdeBTo=
|
||||||
|
github.com/spf13/pflag v1.0.2 h1:Fy0orTDgHdbnzHcsOgfCN4LtHf0ec3wwtiwJqwvf3Gc=
|
||||||
|
github.com/spf13/pflag v1.0.2/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4=
|
||||||
|
golang.org/x/sys v0.0.0-20180906133057-8cf3aee42992 h1:BH3eQWeGbwRU2+wxxuuPOdFBmaiBH81O8BugSjHeTFg=
|
||||||
|
golang.org/x/sys v0.0.0-20180906133057-8cf3aee42992/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||||
|
golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=
|
||||||
|
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||||
|
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||||
|
gopkg.in/yaml.v2 v2.2.1 h1:mUhvW9EsL+naU5Q3cakzfE91YhliOondGd6ZrsDBHQE=
|
||||||
|
gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
6
viper.go
6
viper.go
|
@ -674,8 +674,10 @@ func (v *Viper) Get(key string) interface{} {
|
||||||
return cast.ToBool(val)
|
return cast.ToBool(val)
|
||||||
case string:
|
case string:
|
||||||
return cast.ToString(val)
|
return cast.ToString(val)
|
||||||
case int64, int32, int16, int8, int:
|
case int32, int16, int8, int:
|
||||||
return cast.ToInt(val)
|
return cast.ToInt(val)
|
||||||
|
case int64:
|
||||||
|
return cast.ToInt64(val)
|
||||||
case float64, float32:
|
case float64, float32:
|
||||||
return cast.ToFloat64(val)
|
return cast.ToFloat64(val)
|
||||||
case time.Time:
|
case time.Time:
|
||||||
|
@ -1165,7 +1167,7 @@ func (v *Viper) SetDefault(key string, value interface{}) {
|
||||||
deepestMap[lastKey] = value
|
deepestMap[lastKey] = value
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set sets the value for the key in the override regiser.
|
// Set sets the value for the key in the override register.
|
||||||
// Set is case-insensitive for a key.
|
// Set is case-insensitive for a key.
|
||||||
// Will be used instead of values obtained via
|
// Will be used instead of values obtained via
|
||||||
// flags, config file, ENV, default, or key/value store.
|
// flags, config file, ENV, default, or key/value store.
|
||||||
|
|
|
@ -1107,10 +1107,6 @@ func TestMergeConfig(t *testing.T) {
|
||||||
t.Fatalf("pop != 37890, = %d", pop)
|
t.Fatalf("pop != 37890, = %d", pop)
|
||||||
}
|
}
|
||||||
|
|
||||||
if pop := v.GetInt("hello.lagrenum"); pop != 765432101234567 {
|
|
||||||
t.Fatalf("lagrenum != 765432101234567, = %d", pop)
|
|
||||||
}
|
|
||||||
|
|
||||||
if pop := v.GetInt32("hello.pop"); pop != int32(37890) {
|
if pop := v.GetInt32("hello.pop"); pop != int32(37890) {
|
||||||
t.Fatalf("pop != 37890, = %d", pop)
|
t.Fatalf("pop != 37890, = %d", pop)
|
||||||
}
|
}
|
||||||
|
@ -1135,10 +1131,6 @@ func TestMergeConfig(t *testing.T) {
|
||||||
t.Fatalf("pop != 45000, = %d", pop)
|
t.Fatalf("pop != 45000, = %d", pop)
|
||||||
}
|
}
|
||||||
|
|
||||||
if pop := v.GetInt("hello.lagrenum"); pop != 7654321001234567 {
|
|
||||||
t.Fatalf("lagrenum != 7654321001234567, = %d", pop)
|
|
||||||
}
|
|
||||||
|
|
||||||
if pop := v.GetInt32("hello.pop"); pop != int32(45000) {
|
if pop := v.GetInt32("hello.pop"); pop != int32(45000) {
|
||||||
t.Fatalf("pop != 45000, = %d", pop)
|
t.Fatalf("pop != 45000, = %d", pop)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue