Skip to content

Commit

Permalink
Merge pull request #542 from magodo/config_consul_source_opt
Browse files Browse the repository at this point in the history
Add consul-specific option for config (as registry)
  • Loading branch information
asim committed Jun 25, 2019
2 parents 68764eb + a8dbca7 commit 4cad769
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
5 changes: 5 additions & 0 deletions config/source/consul/consul.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
12 changes: 12 additions & 0 deletions config/source/consul/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package consul
import (
"context"

"github.com/hashicorp/consul/api"
"github.com/micro/go-micro/config/source"
)

Expand All @@ -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 {
Expand Down Expand Up @@ -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)
}
}

0 comments on commit 4cad769

Please sign in to comment.