github.com/annwntech/go-micro/v2@v2.9.5/config/source/etcd/options.go (about) 1 package etcd 2 3 import ( 4 "context" 5 "time" 6 7 "github.com/annwntech/go-micro/v2/config/source" 8 ) 9 10 type addressKey struct{} 11 type prefixKey struct{} 12 type stripPrefixKey struct{} 13 type authKey struct{} 14 type dialTimeoutKey struct{} 15 16 type authCreds struct { 17 Username string 18 Password string 19 } 20 21 // WithAddress sets the etcd address 22 func WithAddress(a ...string) source.Option { 23 return func(o *source.Options) { 24 if o.Context == nil { 25 o.Context = context.Background() 26 } 27 o.Context = context.WithValue(o.Context, addressKey{}, a) 28 } 29 } 30 31 // WithPrefix sets the key prefix to use 32 func WithPrefix(p string) source.Option { 33 return func(o *source.Options) { 34 if o.Context == nil { 35 o.Context = context.Background() 36 } 37 o.Context = context.WithValue(o.Context, prefixKey{}, p) 38 } 39 } 40 41 // StripPrefix indicates whether to remove the prefix from config entries, or leave it in place. 42 func StripPrefix(strip bool) source.Option { 43 return func(o *source.Options) { 44 if o.Context == nil { 45 o.Context = context.Background() 46 } 47 48 o.Context = context.WithValue(o.Context, stripPrefixKey{}, strip) 49 } 50 } 51 52 // Auth allows you to specify username/password 53 func Auth(username, password string) source.Option { 54 return func(o *source.Options) { 55 if o.Context == nil { 56 o.Context = context.Background() 57 } 58 o.Context = context.WithValue(o.Context, authKey{}, &authCreds{Username: username, Password: password}) 59 } 60 } 61 62 // WithDialTimeout set the time out for dialing to etcd 63 func WithDialTimeout(timeout time.Duration) source.Option { 64 return func(o *source.Options) { 65 if o.Context == nil { 66 o.Context = context.Background() 67 } 68 o.Context = context.WithValue(o.Context, dialTimeoutKey{}, timeout) 69 } 70 }