Skip to content

Commit 1127e4f

Browse files
committed
fix: better initialization for profiles
1 parent f9e7429 commit 1127e4f

File tree

1 file changed

+80
-45
lines changed

1 file changed

+80
-45
lines changed

cmd/cmd.go

Lines changed: 80 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ var (
283283

284284
DefaultAuths = map[string]func(...auth.Option) auth.Auth{}
285285

286-
DefaultProfiles = map[string]func(...profile.Option) profile.Profile{
286+
DefaultDebugProfiles = map[string]func(...profile.Option) profile.Profile{
287287
"http": http.NewProfile,
288288
"pprof": pprof.NewProfile,
289289
}
@@ -323,7 +323,7 @@ func newCmd(opts ...Option) Cmd {
323323
Stores: DefaultStores,
324324
Tracers: DefaultTracers,
325325
Auths: DefaultAuths,
326-
DebugProfiles: DefaultProfiles,
326+
DebugProfiles: DefaultDebugProfiles,
327327
Configs: DefaultConfigs,
328328
Caches: DefaultCaches,
329329
}
@@ -364,12 +364,11 @@ func (c *cmd) Options() Options {
364364
}
365365

366366
func (c *cmd) Before(ctx *cli.Context) error {
367-
fmt.Println("cmd.Before")
368367
// If flags are set then use them otherwise do nothing
369368
var serverOpts []server.Option
370369
var clientOpts []client.Option
371370
// --- Profile Grouping Extension ---
372-
// Check for new profile flag/env (not just debug profiler)
371+
373372
profileName := ctx.String("profile")
374373
if profileName == "" {
375374
profileName = os.Getenv("MICRO_PROFILE")
@@ -388,24 +387,25 @@ func (c *cmd) Before(ctx *cli.Context) error {
388387
transport.DefaultTransport = imported.Transport
389388
case "nats":
390389
imported := mprofile.NatsProfile()
391-
*c.opts.Registry = imported.Registry
392-
// these need to move out to a function so they can be merged with below ctx.String("Registry")
393-
serverOpts = append(serverOpts, server.Registry(*c.opts.Registry))
394-
clientOpts = append(clientOpts, client.Registry(*c.opts.Registry))
395-
registry.DefaultRegistry = imported.Registry
396-
397-
*c.opts.Broker = imported.Broker
398-
broker.DefaultBroker = imported.Broker
399-
serverOpts = append(serverOpts, server.Broker(*c.opts.Broker))
400-
clientOpts = append(clientOpts, client.Broker(*c.opts.Broker))
401-
402-
*c.opts.Store = imported.Store
403-
store.DefaultStore = imported.Store
404-
*c.opts.Transport = imported.Transport
405-
406-
transport.DefaultTransport = imported.Transport
407-
serverOpts = append(serverOpts, server.Transport(*c.opts.Transport))
408-
clientOpts = append(clientOpts, client.Transport(*c.opts.Transport))
390+
// Set the registry
391+
sopts, clopts := c.setRegistry(imported.Registry)
392+
serverOpts = append(serverOpts, sopts...)
393+
clientOpts = append(clientOpts, clopts...)
394+
395+
// set the store
396+
sopts, clopts = c.setStore(imported.Store)
397+
serverOpts = append(serverOpts, sopts...)
398+
clientOpts = append(clientOpts, clopts...)
399+
400+
// set the transport
401+
sopts, clopts = c.setTransport(imported.Transport)
402+
serverOpts = append(serverOpts, sopts...)
403+
clientOpts = append(clientOpts, clopts...)
404+
405+
// Set the broker
406+
sopts, clopts = c.setBroker(imported.Broker)
407+
serverOpts = append(serverOpts, sopts...)
408+
clientOpts = append(clientOpts, clopts...)
409409

410410
// Add more profiles as needed
411411
default:
@@ -486,20 +486,9 @@ func (c *cmd) Before(ctx *cli.Context) error {
486486
return fmt.Errorf("Registry %s not found", name)
487487
}
488488

489-
*c.opts.Registry = r()
490-
serverOpts = append(serverOpts, server.Registry(*c.opts.Registry))
491-
clientOpts = append(clientOpts, client.Registry(*c.opts.Registry))
492-
493-
if err := (*c.opts.Selector).Init(selector.Registry(*c.opts.Registry)); err != nil {
494-
logger.Fatalf("Error configuring registry: %v", err)
495-
}
496-
497-
clientOpts = append(clientOpts, client.Selector(*c.opts.Selector))
498-
499-
if err := (*c.opts.Broker).Init(broker.Registry(*c.opts.Registry)); err != nil {
500-
logger.Fatalf("Error configuring broker: %v", err)
501-
}
502-
registry.DefaultRegistry = *c.opts.Registry
489+
sopts, clopts := c.setRegistry(r())
490+
serverOpts = append(serverOpts, sopts...)
491+
clientOpts = append(clientOpts, clopts...)
503492
}
504493

505494
// Set the debug profile
@@ -518,11 +507,9 @@ func (c *cmd) Before(ctx *cli.Context) error {
518507
if !ok {
519508
return fmt.Errorf("Broker %s not found", name)
520509
}
521-
522-
*c.opts.Broker = b()
523-
serverOpts = append(serverOpts, server.Broker(*c.opts.Broker))
524-
clientOpts = append(clientOpts, client.Broker(*c.opts.Broker))
525-
broker.DefaultBroker = *c.opts.Broker
510+
sopts, clopts := c.setBroker(b())
511+
serverOpts = append(serverOpts, sopts...)
512+
clientOpts = append(clientOpts, clopts...)
526513
}
527514

528515
// Set the selector
@@ -546,10 +533,10 @@ func (c *cmd) Before(ctx *cli.Context) error {
546533
return fmt.Errorf("Transport %s not found", name)
547534
}
548535

549-
*c.opts.Transport = t()
550-
serverOpts = append(serverOpts, server.Transport(*c.opts.Transport))
551-
clientOpts = append(clientOpts, client.Transport(*c.opts.Transport))
552-
transport.DefaultTransport = *c.opts.Transport
536+
sopts, clopts := c.setTransport(t())
537+
serverOpts = append(serverOpts, sopts...)
538+
clientOpts = append(clientOpts, clopts...)
539+
553540
}
554541

555542
// Parse the server options
@@ -695,6 +682,54 @@ func (c *cmd) Before(ctx *cli.Context) error {
695682
return nil
696683
}
697684

685+
func (c *cmd) setRegistry(r registry.Registry) ([]server.Option, []client.Option) {
686+
var serverOpts []server.Option
687+
var clientOpts []client.Option
688+
*c.opts.Registry = r
689+
serverOpts = append(serverOpts, server.Registry(*c.opts.Registry))
690+
clientOpts = append(clientOpts, client.Registry(*c.opts.Registry))
691+
692+
if err := (*c.opts.Selector).Init(selector.Registry(*c.opts.Registry)); err != nil {
693+
logger.Fatalf("Error configuring registry: %v", err)
694+
}
695+
696+
clientOpts = append(clientOpts, client.Selector(*c.opts.Selector))
697+
698+
if err := (*c.opts.Broker).Init(broker.Registry(*c.opts.Registry)); err != nil {
699+
logger.Fatalf("Error configuring broker: %v", err)
700+
}
701+
registry.DefaultRegistry = *c.opts.Registry
702+
return serverOpts, clientOpts
703+
}
704+
705+
func (c *cmd) setBroker(b broker.Broker) ([]server.Option, []client.Option) {
706+
var serverOpts []server.Option
707+
var clientOpts []client.Option
708+
*c.opts.Broker = b
709+
serverOpts = append(serverOpts, server.Broker(*c.opts.Broker))
710+
clientOpts = append(clientOpts, client.Broker(*c.opts.Broker))
711+
broker.DefaultBroker = *c.opts.Broker
712+
return serverOpts, clientOpts
713+
}
714+
715+
func (c *cmd) setStore(s store.Store) ([]server.Option, []client.Option) {
716+
var serverOpts []server.Option
717+
var clientOpts []client.Option
718+
*c.opts.Store = s
719+
store.DefaultStore = *c.opts.Store
720+
return serverOpts, clientOpts
721+
}
722+
723+
func (c *cmd) setTransport(t transport.Transport) ([]server.Option, []client.Option) {
724+
var serverOpts []server.Option
725+
var clientOpts []client.Option
726+
*c.opts.Transport = t
727+
serverOpts = append(serverOpts, server.Transport(*c.opts.Transport))
728+
clientOpts = append(clientOpts, client.Transport(*c.opts.Transport))
729+
transport.DefaultTransport = *c.opts.Transport
730+
return serverOpts, clientOpts
731+
}
732+
698733
func (c *cmd) Init(opts ...Option) error {
699734
for _, o := range opts {
700735
o(&c.opts)

0 commit comments

Comments
 (0)