2019-05-31 12:38:49 +01:00
|
|
|
package etcd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2019-06-11 16:18:37 +08:00
|
|
|
"time"
|
2019-05-31 12:38:49 +01:00
|
|
|
|
|
|
|
"github.com/micro/go-micro/config/source"
|
|
|
|
)
|
|
|
|
|
|
|
|
type addressKey struct{}
|
|
|
|
type prefixKey struct{}
|
|
|
|
type stripPrefixKey struct{}
|
|
|
|
type authKey struct{}
|
2019-06-11 16:18:37 +08:00
|
|
|
type dialTimeoutKey struct{}
|
2019-05-31 12:38:49 +01:00
|
|
|
|
|
|
|
type authCreds struct {
|
|
|
|
Username string
|
|
|
|
Password string
|
|
|
|
}
|
|
|
|
|
2019-06-11 16:18:37 +08:00
|
|
|
// WithAddress sets the etcd address
|
2019-05-31 12:38:49 +01:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Auth allows you to specify username/password
|
|
|
|
func Auth(username, password string) source.Option {
|
|
|
|
return func(o *source.Options) {
|
|
|
|
if o.Context == nil {
|
|
|
|
o.Context = context.Background()
|
|
|
|
}
|
|
|
|
o.Context = context.WithValue(o.Context, authKey{}, &authCreds{Username: username, Password: password})
|
|
|
|
}
|
|
|
|
}
|
2019-06-11 16:18:37 +08:00
|
|
|
|
|
|
|
// WithDialTimeout set the time out for dialing to etcd
|
|
|
|
func WithDialTimeout(timeout time.Duration) source.Option {
|
|
|
|
return func(o *source.Options) {
|
|
|
|
if o.Context == nil {
|
|
|
|
o.Context = context.Background()
|
|
|
|
}
|
|
|
|
o.Context = context.WithValue(o.Context, dialTimeoutKey{}, timeout)
|
|
|
|
}
|
|
|
|
}
|