Skip to content

Commit cb46674

Browse files
committed
fix: set DefaultX for cli flag and service option
1 parent 45fe92d commit cb46674

File tree

3 files changed

+36
-0
lines changed

3 files changed

+36
-0
lines changed

cmd/cmd.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,7 @@ func (c *cmd) Before(ctx *cli.Context) error {
367367
// only change if we have the client and type differs
368368
if cl, ok := c.opts.Clients[name]; ok && (*c.opts.Client).String() != name {
369369
*c.opts.Client = cl()
370+
client.DefaultClient = *c.opts.Client
370371
}
371372
}
372373

@@ -375,6 +376,7 @@ func (c *cmd) Before(ctx *cli.Context) error {
375376
// only change if we have the server and type differs
376377
if s, ok := c.opts.Servers[name]; ok && (*c.opts.Server).String() != name {
377378
*c.opts.Server = s()
379+
server.DefaultServer = *c.opts.Server
378380
}
379381
}
380382

@@ -386,6 +388,7 @@ func (c *cmd) Before(ctx *cli.Context) error {
386388
}
387389

388390
*c.opts.Store = s(store.WithClient(*c.opts.Client))
391+
store.DefaultStore = *c.opts.Store
389392
}
390393

391394
// Set the tracer
@@ -396,6 +399,7 @@ func (c *cmd) Before(ctx *cli.Context) error {
396399
}
397400

398401
*c.opts.Tracer = r()
402+
trace.DefaultTracer = *c.opts.Tracer
399403
}
400404

401405
// Setup auth
@@ -422,6 +426,7 @@ func (c *cmd) Before(ctx *cli.Context) error {
422426
}
423427

424428
*c.opts.Auth = r(authOpts...)
429+
auth.DefaultAuth = *c.opts.Auth
425430
}
426431

427432
// Set the registry
@@ -444,6 +449,7 @@ func (c *cmd) Before(ctx *cli.Context) error {
444449
if err := (*c.opts.Broker).Init(broker.Registry(*c.opts.Registry)); err != nil {
445450
logger.Fatalf("Error configuring broker: %v", err)
446451
}
452+
registry.DefaultRegistry = *c.opts.Registry
447453
}
448454

449455
// Set the profile
@@ -454,6 +460,7 @@ func (c *cmd) Before(ctx *cli.Context) error {
454460
}
455461

456462
*c.opts.Profile = p()
463+
profile.DefaultProfile = *c.opts.Profile
457464
}
458465

459466
// Set the broker
@@ -466,6 +473,7 @@ func (c *cmd) Before(ctx *cli.Context) error {
466473
*c.opts.Broker = b()
467474
serverOpts = append(serverOpts, server.Broker(*c.opts.Broker))
468475
clientOpts = append(clientOpts, client.Broker(*c.opts.Broker))
476+
broker.DefaultBroker = *c.opts.Broker
469477
}
470478

471479
// Set the selector
@@ -479,6 +487,7 @@ func (c *cmd) Before(ctx *cli.Context) error {
479487

480488
// No server option here. Should there be?
481489
clientOpts = append(clientOpts, client.Selector(*c.opts.Selector))
490+
selector.DefaultSelector = *c.opts.Selector
482491
}
483492

484493
// Set the transport
@@ -491,6 +500,7 @@ func (c *cmd) Before(ctx *cli.Context) error {
491500
*c.opts.Transport = t()
492501
serverOpts = append(serverOpts, server.Transport(*c.opts.Transport))
493502
clientOpts = append(clientOpts, client.Transport(*c.opts.Transport))
503+
transport.DefaultTransport = *c.opts.Transport
494504
}
495505

496506
// Parse the server options
@@ -630,6 +640,7 @@ func (c *cmd) Before(ctx *cli.Context) error {
630640
logger.Fatalf("Error configuring config: %v", err)
631641
}
632642
*c.opts.Config = rc
643+
config.DefaultConfig = *c.opts.Config
633644
}
634645
}
635646

cmd/options.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,72 +81,84 @@ func Version(v string) Option {
8181
func Broker(b *broker.Broker) Option {
8282
return func(o *Options) {
8383
o.Broker = b
84+
broker.DefaultBroker = *b
8485
}
8586
}
8687

8788
func Cache(c *cache.Cache) Option {
8889
return func(o *Options) {
8990
o.Cache = c
91+
cache.DefaultCache = *c
9092
}
9193
}
9294

9395
func Config(c *config.Config) Option {
9496
return func(o *Options) {
9597
o.Config = c
98+
config.DefaultConfig = *c
9699
}
97100
}
98101

99102
func Selector(s *selector.Selector) Option {
100103
return func(o *Options) {
101104
o.Selector = s
105+
selector.DefaultSelector = *s
102106
}
103107
}
104108

105109
func Registry(r *registry.Registry) Option {
106110
return func(o *Options) {
107111
o.Registry = r
112+
registry.DefaultRegistry = *r
108113
}
109114
}
110115

111116
func Transport(t *transport.Transport) Option {
112117
return func(o *Options) {
113118
o.Transport = t
119+
transport.DefaultTransport = *t
114120
}
115121
}
116122

117123
func Client(c *client.Client) Option {
118124
return func(o *Options) {
119125
o.Client = c
126+
client.DefaultClient = *c
120127
}
121128
}
122129

123130
func Server(s *server.Server) Option {
124131
return func(o *Options) {
125132
o.Server = s
133+
server.DefaultServer = *s
126134
}
127135
}
128136

129137
func Store(s *store.Store) Option {
130138
return func(o *Options) {
131139
o.Store = s
140+
store.DefaultStore = *s
132141
}
133142
}
134143

135144
func Tracer(t *trace.Tracer) Option {
136145
return func(o *Options) {
137146
o.Tracer = t
147+
trace.DefaultTracer = *t
138148
}
139149
}
140150

141151
func Auth(a *auth.Auth) Option {
142152
return func(o *Options) {
143153
o.Auth = a
154+
auth.DefaultAuth = *a
144155
}
145156
}
146157

147158
func Profile(p *profile.Profile) Option {
148159
return func(o *Options) {
149160
o.Profile = p
161+
profile.DefaultProfile = *p
150162
}
151163
}
152164

service/options.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,12 +83,14 @@ func Broker(b broker.Broker) Option {
8383
// Update Client and Server
8484
o.Client.Init(client.Broker(b))
8585
o.Server.Init(server.Broker(b))
86+
broker.DefaultBroker = b
8687
}
8788
}
8889

8990
func Cache(c cache.Cache) Option {
9091
return func(o *Options) {
9192
o.Cache = c
93+
cache.DefaultCache = c
9294
}
9395
}
9496

@@ -102,6 +104,7 @@ func Cmd(c cmd.Cmd) Option {
102104
func Client(c client.Client) Option {
103105
return func(o *Options) {
104106
o.Client = c
107+
client.DefaultClient = c
105108
}
106109
}
107110

@@ -135,20 +138,23 @@ func HandleSignal(b bool) Option {
135138
func Profile(p profile.Profile) Option {
136139
return func(o *Options) {
137140
o.Profile = p
141+
profile.DefaultProfile = p
138142
}
139143
}
140144

141145
// Server to be used for service.
142146
func Server(s server.Server) Option {
143147
return func(o *Options) {
144148
o.Server = s
149+
server.DefaultServer = s
145150
}
146151
}
147152

148153
// Store sets the store to use.
149154
func Store(s store.Store) Option {
150155
return func(o *Options) {
151156
o.Store = s
157+
store.DefaultStore = s
152158
}
153159
}
154160

@@ -162,6 +168,7 @@ func Registry(r registry.Registry) Option {
162168
o.Server.Init(server.Registry(r))
163169
// Update Broker
164170
o.Broker.Init(broker.Registry(r))
171+
broker.DefaultBroker = o.Broker
165172
}
166173
}
167174

@@ -170,26 +177,31 @@ func Tracer(t trace.Tracer) Option {
170177
return func(o *Options) {
171178
o.Server.Init(server.Tracer(t))
172179
}
180+
173181
}
174182

175183
// Auth sets the auth for the service.
176184
func Auth(a auth.Auth) Option {
177185
return func(o *Options) {
178186
o.Auth = a
187+
auth.DefaultAuth = a
188+
179189
}
180190
}
181191

182192
// Config sets the config for the service.
183193
func Config(c config.Config) Option {
184194
return func(o *Options) {
185195
o.Config = c
196+
config.DefaultConfig = c
186197
}
187198
}
188199

189200
// Selector sets the selector for the service client.
190201
func Selector(s selector.Selector) Option {
191202
return func(o *Options) {
192203
o.Client.Init(client.Selector(s))
204+
selector.DefaultSelector = s
193205
}
194206
}
195207

@@ -201,6 +213,7 @@ func Transport(t transport.Transport) Option {
201213
// Update Client and Server
202214
o.Client.Init(client.Transport(t))
203215
o.Server.Init(server.Transport(t))
216+
transport.DefaultTransport = t
204217
}
205218
}
206219

0 commit comments

Comments
 (0)