Set option and cli args to the service profile (#1259)

This commit is contained in:
Di Wu
2020-02-26 00:42:43 +08:00
committed by GitHub
parent 53c3bff819
commit 603d37b135
5 changed files with 61 additions and 21 deletions

View File

@@ -11,6 +11,9 @@ import (
"github.com/micro/go-micro/v2/broker"
"github.com/micro/go-micro/v2/client"
"github.com/micro/go-micro/v2/client/selector"
"github.com/micro/go-micro/v2/debug/profile"
"github.com/micro/go-micro/v2/debug/profile/http"
"github.com/micro/go-micro/v2/debug/profile/pprof"
"github.com/micro/go-micro/v2/debug/trace"
log "github.com/micro/go-micro/v2/logger"
"github.com/micro/go-micro/v2/registry"
@@ -25,6 +28,7 @@ import (
// servers
"github.com/micro/cli/v2"
sgrpc "github.com/micro/go-micro/v2/server/grpc"
smucp "github.com/micro/go-micro/v2/server/mucp"
@@ -318,6 +322,11 @@ var (
"store": storeAuth.NewAuth,
"jwt": jwtAuth.NewAuth,
}
DefaultProfiles = map[string]func(...profile.Option) profile.Profile{
"http": http.NewProfile,
"pprof": pprof.NewProfile,
}
)
func init() {
@@ -336,6 +345,7 @@ func newCmd(opts ...Option) Cmd {
Runtime: &runtime.DefaultRuntime,
Store: &store.DefaultStore,
Tracer: &trace.DefaultTracer,
Profile: &profile.DefaultProfile,
Brokers: DefaultBrokers,
Clients: DefaultClients,
@@ -347,6 +357,7 @@ func newCmd(opts ...Option) Cmd {
Stores: DefaultStores,
Tracers: DefaultTracers,
Auths: DefaultAuths,
Profiles: DefaultProfiles,
}
for _, o := range opts {
@@ -430,6 +441,16 @@ func (c *cmd) Before(ctx *cli.Context) error {
*c.opts.Auth = a()
}
// Set the profile
if name := ctx.String("profile"); len(name) > 0 {
p, ok := c.opts.Profiles[name]
if !ok {
return fmt.Errorf("Unsupported profile: %s", name)
}
*c.opts.Profile = p()
}
// Set the client
if name := ctx.String("client"); len(name) > 0 {
// only change if we have the client and type differs

View File

@@ -7,6 +7,7 @@ import (
"github.com/micro/go-micro/v2/broker"
"github.com/micro/go-micro/v2/client"
"github.com/micro/go-micro/v2/client/selector"
"github.com/micro/go-micro/v2/debug/profile"
"github.com/micro/go-micro/v2/debug/trace"
"github.com/micro/go-micro/v2/registry"
"github.com/micro/go-micro/v2/runtime"
@@ -32,6 +33,7 @@ type Options struct {
Store *store.Store
Tracer *trace.Tracer
Auth *auth.Auth
Profile *profile.Profile
Brokers map[string]func(...broker.Option) broker.Broker
Clients map[string]func(...client.Option) client.Client
@@ -43,6 +45,7 @@ type Options struct {
Stores map[string]func(...store.Option) store.Store
Tracers map[string]func(...trace.Option) trace.Tracer
Auths map[string]func(...auth.Option) auth.Auth
Profiles map[string]func(...profile.Option) profile.Profile
// Other options for implementations of the interface
// can be stored in a context
@@ -118,6 +121,12 @@ func Auth(a *auth.Auth) Option {
}
}
func Profile(p *profile.Profile) Option {
return func(o *Options) {
o.Profile = p
}
}
// New broker func
func NewBroker(name string, b func(...broker.Option) broker.Broker) Option {
return func(o *Options) {