Merge pull request #542 from magodo/config_consul_source_opt

Add consul-specific option for config (as registry)
This commit is contained in:
Asim Aslam 2019-06-25 16:14:03 +01:00 committed by GitHub
commit 4cad7697cc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 0 deletions

View File

@ -74,6 +74,11 @@ func NewSource(opts ...source.Option) source.Source {
// use default config
config := api.DefaultConfig()
// use the consul config passed in the options if any
if co, ok := options.Context.Value(configKey{}).(*api.Config); ok {
config = co
}
// check if there are any addrs
a, ok := options.Context.Value(addressKey{}).(string)
if ok {

View File

@ -3,6 +3,7 @@ package consul
import (
"context"
"github.com/hashicorp/consul/api"
"github.com/micro/go-micro/config/source"
)
@ -11,6 +12,7 @@ type prefixKey struct{}
type stripPrefixKey struct{}
type dcKey struct{}
type tokenKey struct{}
type configKey struct{}
// WithAddress sets the consul address
func WithAddress(a string) source.Option {
@ -61,3 +63,13 @@ func WithToken(p string) source.Option {
o.Context = context.WithValue(o.Context, tokenKey{}, p)
}
}
// WithConfig set consul-specific options
func WithConfig(c *api.Config) source.Option {
return func(o *source.Options) {
if o.Context == nil {
o.Context = context.Background()
}
o.Context = context.WithValue(o.Context, configKey{}, c)
}
}