Skip to content

Commit f9e7429

Browse files
committed
chore: still ugly, not ready
1 parent 3d6aa46 commit f9e7429

File tree

4 files changed

+115
-91
lines changed

4 files changed

+115
-91
lines changed

cmd/cmd.go

Lines changed: 84 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,11 @@ var (
149149
Usage: "Plugin profile to use. (local, nats, etc)",
150150
EnvVars: []string{"MICRO_PROFILE"},
151151
},
152+
&cli.StringFlag{
153+
Name: "debug-profile",
154+
Usage: "Debug Plugin profile to use.",
155+
EnvVars: []string{"MICRO_DEBUG_PROFILE"},
156+
},
152157
&cli.StringFlag{
153158
Name: "registry",
154159
EnvVars: []string{"MICRO_REGISTRY"},
@@ -243,9 +248,9 @@ var (
243248
}
244249

245250
DefaultBrokers = map[string]func(...broker.Option) broker.Broker{
246-
"memory": broker.NewMemoryBroker,
247-
"http": broker.NewHttpBroker,
248-
"nats": nbroker.NewNatsBroker,
251+
"memory": broker.NewMemoryBroker,
252+
"http": broker.NewHttpBroker,
253+
"nats": nbroker.NewNatsBroker,
249254
"rabbitmq": rabbit.NewBroker,
250255
}
251256

@@ -268,8 +273,8 @@ var (
268273
}
269274

270275
DefaultStores = map[string]func(...store.Option) store.Store{
271-
"memory": store.NewMemoryStore,
272-
"mysql": mysql.NewMysqlStore,
276+
"memory": store.NewMemoryStore,
277+
"mysql": mysql.NewMysqlStore,
273278
"natsjskv": natsjskv.NewStore,
274279
"postgres": postgres.NewStore,
275280
}
@@ -296,31 +301,31 @@ func init() {
296301

297302
func newCmd(opts ...Option) Cmd {
298303
options := Options{
299-
Auth: &auth.DefaultAuth,
300-
Broker: &broker.DefaultBroker,
301-
Client: &client.DefaultClient,
302-
Registry: &registry.DefaultRegistry,
303-
Server: &server.DefaultServer,
304-
Selector: &selector.DefaultSelector,
305-
Transport: &transport.DefaultTransport,
306-
Store: &store.DefaultStore,
307-
Tracer: &trace.DefaultTracer,
308-
Profile: &profile.DefaultProfile,
309-
Config: &config.DefaultConfig,
310-
Cache: &cache.DefaultCache,
311-
312-
Brokers: DefaultBrokers,
313-
Clients: DefaultClients,
314-
Registries: DefaultRegistries,
315-
Selectors: DefaultSelectors,
316-
Servers: DefaultServers,
317-
Transports: DefaultTransports,
318-
Stores: DefaultStores,
319-
Tracers: DefaultTracers,
320-
Auths: DefaultAuths,
321-
Profiles: DefaultProfiles,
322-
Configs: DefaultConfigs,
323-
Caches: DefaultCaches,
304+
Auth: &auth.DefaultAuth,
305+
Broker: &broker.DefaultBroker,
306+
Client: &client.DefaultClient,
307+
Registry: &registry.DefaultRegistry,
308+
Server: &server.DefaultServer,
309+
Selector: &selector.DefaultSelector,
310+
Transport: &transport.DefaultTransport,
311+
Store: &store.DefaultStore,
312+
Tracer: &trace.DefaultTracer,
313+
DebugProfile: &profile.DefaultProfile,
314+
Config: &config.DefaultConfig,
315+
Cache: &cache.DefaultCache,
316+
317+
Brokers: DefaultBrokers,
318+
Clients: DefaultClients,
319+
Registries: DefaultRegistries,
320+
Selectors: DefaultSelectors,
321+
Servers: DefaultServers,
322+
Transports: DefaultTransports,
323+
Stores: DefaultStores,
324+
Tracers: DefaultTracers,
325+
Auths: DefaultAuths,
326+
DebugProfiles: DefaultProfiles,
327+
Configs: DefaultConfigs,
328+
Caches: DefaultCaches,
324329
}
325330

326331
for _, o := range opts {
@@ -359,10 +364,54 @@ func (c *cmd) Options() Options {
359364
}
360365

361366
func (c *cmd) Before(ctx *cli.Context) error {
367+
fmt.Println("cmd.Before")
362368
// If flags are set then use them otherwise do nothing
363369
var serverOpts []server.Option
364370
var clientOpts []client.Option
371+
// --- Profile Grouping Extension ---
372+
// Check for new profile flag/env (not just debug profiler)
373+
profileName := ctx.String("profile")
374+
if profileName == "" {
375+
profileName = os.Getenv("MICRO_PROFILE")
376+
}
377+
if profileName != "" {
378+
switch profileName {
379+
case "local":
380+
imported := mprofile.LocalProfile()
381+
*c.opts.Registry = imported.Registry
382+
registry.DefaultRegistry = imported.Registry
383+
*c.opts.Broker = imported.Broker
384+
broker.DefaultBroker = imported.Broker
385+
*c.opts.Store = imported.Store
386+
store.DefaultStore = imported.Store
387+
*c.opts.Transport = imported.Transport
388+
transport.DefaultTransport = imported.Transport
389+
case "nats":
390+
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))
365409

410+
// Add more profiles as needed
411+
default:
412+
return fmt.Errorf("unsupported profile: %s", profileName)
413+
}
414+
}
366415
// Set the client
367416
if name := ctx.String("client"); len(name) > 0 {
368417
// only change if we have the client and type differs
@@ -453,49 +502,14 @@ func (c *cmd) Before(ctx *cli.Context) error {
453502
registry.DefaultRegistry = *c.opts.Registry
454503
}
455504

456-
// --- Profile Grouping Extension ---
457-
// Check for new profile flag/env (not just debug profiler)
458-
profileName := ctx.String("profile")
459-
if profileName == "" {
460-
profileName = os.Getenv("MICRO_PROFILE")
461-
}
462-
if profileName != "" {
463-
switch profileName {
464-
case "local":
465-
imported := mprofile.LocalProfile()
466-
*c.opts.Registry = imported.Registry
467-
registry.DefaultRegistry = imported.Registry
468-
*c.opts.Broker = imported.Broker
469-
broker.DefaultBroker = imported.Broker
470-
*c.opts.Store = imported.Store
471-
store.DefaultStore = imported.Store
472-
*c.opts.Transport = imported.Transport
473-
transport.DefaultTransport = imported.Transport
474-
case "nats":
475-
imported := mprofile.NatsProfile()
476-
*c.opts.Registry = imported.Registry
477-
registry.DefaultRegistry = imported.Registry
478-
*c.opts.Broker = imported.Broker
479-
broker.DefaultBroker = imported.Broker
480-
*c.opts.Store = imported.Store
481-
store.DefaultStore = imported.Store
482-
*c.opts.Transport = imported.Transport
483-
transport.DefaultTransport = imported.Transport
484-
// Add more profiles as needed
485-
default:
486-
return fmt.Errorf("unsupported profile: %s", profileName)
487-
}
488-
}
489-
490-
// Set the profile
491-
if name := ctx.String("profile"); len(name) > 0 {
492-
p, ok := c.opts.Profiles[name]
505+
// Set the debug profile
506+
if name := ctx.String("debug-profile"); len(name) > 0 {
507+
p, ok := c.opts.DebugProfiles[name]
493508
if !ok {
494509
return fmt.Errorf("unsupported profile: %s", name)
495510
}
496-
497-
*c.opts.Profile = p()
498-
profile.DefaultProfile = *c.opts.Profile
511+
*c.opts.DebugProfile = p()
512+
profile.DefaultProfile = *c.opts.DebugProfile
499513
}
500514

501515
// Set the broker
@@ -678,7 +692,6 @@ func (c *cmd) Before(ctx *cli.Context) error {
678692
config.DefaultConfig = *c.opts.Config
679693
}
680694
}
681-
682695
return nil
683696
}
684697

cmd/options.go

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -21,22 +21,22 @@ type Options struct {
2121

2222
// Other options for implementations of the interface
2323
// can be stored in a context
24-
Context context.Context
25-
Auth *auth.Auth
26-
Selector *selector.Selector
27-
Profile *profile.Profile
24+
Context context.Context
25+
Auth *auth.Auth
26+
Selector *selector.Selector
27+
DebugProfile *profile.Profile
2828

2929
Registry *registry.Registry
3030

31-
Brokers map[string]func(...broker.Option) broker.Broker
32-
Transport *transport.Transport
33-
Cache *cache.Cache
34-
Config *config.Config
35-
Client *client.Client
36-
Server *server.Server
37-
Caches map[string]func(...cache.Option) cache.Cache
38-
Tracer *trace.Tracer
39-
Profiles map[string]func(...profile.Option) profile.Profile
31+
Brokers map[string]func(...broker.Option) broker.Broker
32+
Transport *transport.Transport
33+
Cache *cache.Cache
34+
Config *config.Config
35+
Client *client.Client
36+
Server *server.Server
37+
Caches map[string]func(...cache.Option) cache.Cache
38+
Tracer *trace.Tracer
39+
DebugProfiles map[string]func(...profile.Option) profile.Profile
4040

4141
// We need pointers to things so we can swap them out if needed.
4242
Broker *broker.Broker
@@ -157,7 +157,7 @@ func Auth(a *auth.Auth) Option {
157157

158158
func Profile(p *profile.Profile) Option {
159159
return func(o *Options) {
160-
o.Profile = p
160+
o.DebugProfile = p
161161
profile.DefaultProfile = *p
162162
}
163163
}
@@ -235,6 +235,6 @@ func NewConfig(name string, t func(...config.Option) (config.Config, error)) Opt
235235
// New profile func.
236236
func NewProfile(name string, t func(...profile.Option) profile.Profile) Option {
237237
return func(o *Options) {
238-
o.Profiles[name] = t
238+
o.DebugProfiles[name] = t
239239
}
240240
}

profile/profile.go

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,24 @@ func LocalProfile() Profile {
3434

3535
func NatsProfile() Profile {
3636
addr := os.Getenv("MICRO_NATS_ADDRESS")
37+
reg := nreg.NewNatsRegistry(registry.Addrs(addr))
38+
brok := nats.NewNatsBroker(broker.Addrs(addr))
39+
// this might be wrong, look for a better way to set this up
40+
st := nstore.NewStore(nstore.NatsOptions(natslib.Options{Url: addr}))
41+
// same, double check for single url vs slice of Server
42+
tx := ntx.NewTransport(ntx.Options(natslib.Options{Url: addr}))
43+
44+
registry.DefaultRegistry = reg
45+
broker.DefaultBroker = brok
46+
store.DefaultStore = st
47+
transport.DefaultTransport = tx
3748
return Profile{
38-
Registry: nreg.NewNatsRegistry(registry.Addrs(addr)),
39-
Broker: nats.NewNatsBroker(broker.Addrs(addr)),
49+
Registry: reg,
50+
Broker: brok,
4051
// this might be wrong, look for a better way to set this up
41-
Store: nstore.NewStore(nstore.NatsOptions(natslib.Options{Url: addr})),
52+
Store: st,
4253
// same, double check for single url vs slice of Server
43-
Transport: ntx.NewTransport(ntx.Options(natslib.Options{Url: addr})),
54+
Transport: tx,
4455
}
4556
}
4657

service/options.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ func Tracer(t trace.Tracer) Option {
184184
func Auth(a auth.Auth) Option {
185185
return func(o *Options) {
186186
o.Auth = a
187-
auth.DefaultAuth = a
187+
auth.DefaultAuth = a
188188

189189
}
190190
}

0 commit comments

Comments
 (0)