2019-05-31 01:11:13 +03:00
|
|
|
package consul
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
2019-06-25 13:31:32 +03:00
|
|
|
"github.com/hashicorp/consul/api"
|
2019-05-31 01:11:13 +03:00
|
|
|
"github.com/micro/go-micro/config/source"
|
|
|
|
)
|
|
|
|
|
|
|
|
type addressKey struct{}
|
|
|
|
type prefixKey struct{}
|
|
|
|
type stripPrefixKey struct{}
|
|
|
|
type dcKey struct{}
|
|
|
|
type tokenKey struct{}
|
2019-06-25 17:41:31 +03:00
|
|
|
type configKey struct{}
|
2019-05-31 01:11:13 +03:00
|
|
|
|
|
|
|
// WithAddress sets the consul address
|
|
|
|
func WithAddress(a string) source.Option {
|
|
|
|
return func(o *source.Options) {
|
|
|
|
if o.Context == nil {
|
|
|
|
o.Context = context.Background()
|
|
|
|
}
|
|
|
|
o.Context = context.WithValue(o.Context, addressKey{}, a)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithPrefix sets the key prefix to use
|
|
|
|
func WithPrefix(p string) source.Option {
|
|
|
|
return func(o *source.Options) {
|
|
|
|
if o.Context == nil {
|
|
|
|
o.Context = context.Background()
|
|
|
|
}
|
|
|
|
o.Context = context.WithValue(o.Context, prefixKey{}, p)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// StripPrefix indicates whether to remove the prefix from config entries, or leave it in place.
|
|
|
|
func StripPrefix(strip bool) source.Option {
|
|
|
|
return func(o *source.Options) {
|
|
|
|
if o.Context == nil {
|
|
|
|
o.Context = context.Background()
|
|
|
|
}
|
|
|
|
|
|
|
|
o.Context = context.WithValue(o.Context, stripPrefixKey{}, strip)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func WithDatacenter(p string) source.Option {
|
|
|
|
return func(o *source.Options) {
|
|
|
|
if o.Context == nil {
|
|
|
|
o.Context = context.Background()
|
|
|
|
}
|
|
|
|
o.Context = context.WithValue(o.Context, dcKey{}, p)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithToken sets the key token to use
|
|
|
|
func WithToken(p string) source.Option {
|
|
|
|
return func(o *source.Options) {
|
|
|
|
if o.Context == nil {
|
|
|
|
o.Context = context.Background()
|
|
|
|
}
|
|
|
|
o.Context = context.WithValue(o.Context, tokenKey{}, p)
|
|
|
|
}
|
|
|
|
}
|
2019-06-25 13:31:32 +03:00
|
|
|
|
2019-06-25 17:41:31 +03:00
|
|
|
// WithConfig set consul-specific options
|
|
|
|
func WithConfig(c *api.Config) source.Option {
|
2019-06-25 13:31:32 +03:00
|
|
|
return func(o *source.Options) {
|
|
|
|
if o.Context == nil {
|
|
|
|
o.Context = context.Background()
|
|
|
|
}
|
2019-06-25 17:41:31 +03:00
|
|
|
o.Context = context.WithValue(o.Context, configKey{}, c)
|
2019-06-25 13:31:32 +03:00
|
|
|
}
|
|
|
|
}
|