2019-10-03 13:22:35 +03:00
|
|
|
package etcd
|
2019-05-31 01:11:13 +03:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2019-10-03 13:22:35 +03:00
|
|
|
"time"
|
2019-05-31 01:11:13 +03:00
|
|
|
|
2020-01-30 14:39:00 +03:00
|
|
|
"github.com/micro/go-micro/v2/config/source"
|
2019-05-31 01:11:13 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
type addressKey struct{}
|
|
|
|
type prefixKey struct{}
|
|
|
|
type stripPrefixKey struct{}
|
2019-10-03 13:22:35 +03:00
|
|
|
type authKey struct{}
|
|
|
|
type dialTimeoutKey struct{}
|
2019-05-31 01:11:13 +03:00
|
|
|
|
2019-10-03 13:22:35 +03:00
|
|
|
type authCreds struct {
|
|
|
|
Username string
|
|
|
|
Password string
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithAddress sets the etcd address
|
|
|
|
func WithAddress(a ...string) source.Option {
|
2019-05-31 01:11:13 +03:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-03 13:22:35 +03:00
|
|
|
// Auth allows you to specify username/password
|
|
|
|
func Auth(username, password string) source.Option {
|
2019-05-31 01:11:13 +03:00
|
|
|
return func(o *source.Options) {
|
|
|
|
if o.Context == nil {
|
|
|
|
o.Context = context.Background()
|
|
|
|
}
|
2019-10-03 13:22:35 +03:00
|
|
|
o.Context = context.WithValue(o.Context, authKey{}, &authCreds{Username: username, Password: password})
|
2019-05-31 01:11:13 +03:00
|
|
|
}
|
|
|
|
}
|
2019-06-25 13:31:32 +03:00
|
|
|
|
2019-10-03 13:22:35 +03:00
|
|
|
// WithDialTimeout set the time out for dialing to etcd
|
|
|
|
func WithDialTimeout(timeout time.Duration) source.Option {
|
2019-06-25 13:31:32 +03:00
|
|
|
return func(o *source.Options) {
|
|
|
|
if o.Context == nil {
|
|
|
|
o.Context = context.Background()
|
|
|
|
}
|
2019-10-03 13:22:35 +03:00
|
|
|
o.Context = context.WithValue(o.Context, dialTimeoutKey{}, timeout)
|
2019-06-25 13:31:32 +03:00
|
|
|
}
|
|
|
|
}
|