Update the way flags are used
This commit is contained in:
parent
1f1bc27421
commit
3b56a62589
@ -26,6 +26,8 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type Client interface {
|
type Client interface {
|
||||||
|
Init(...Option) error
|
||||||
|
Options() Options
|
||||||
NewPublication(topic string, msg interface{}) Publication
|
NewPublication(topic string, msg interface{}) Publication
|
||||||
NewRequest(service, method string, req interface{}, reqOpts ...RequestOption) Request
|
NewRequest(service, method string, req interface{}, reqOpts ...RequestOption) Request
|
||||||
NewProtoRequest(service, method string, req interface{}, reqOpts ...RequestOption) Request
|
NewProtoRequest(service, method string, req interface{}, reqOpts ...RequestOption) Request
|
||||||
|
@ -149,6 +149,17 @@ func (r *rpcClient) stream(ctx context.Context, address string, req Request) (St
|
|||||||
return stream, err
|
return stream, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (r *rpcClient) Init(opts ...Option) error {
|
||||||
|
for _, o := range opts {
|
||||||
|
o(&r.opts)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *rpcClient) Options() Options {
|
||||||
|
return r.opts
|
||||||
|
}
|
||||||
|
|
||||||
func (r *rpcClient) CallRemote(ctx context.Context, address string, request Request, response interface{}, opts ...CallOption) error {
|
func (r *rpcClient) CallRemote(ctx context.Context, address string, request Request, response interface{}, opts ...CallOption) error {
|
||||||
return r.call(ctx, address, request, response)
|
return r.call(ctx, address, request, response)
|
||||||
}
|
}
|
||||||
|
11
cmd/cmd.go
11
cmd/cmd.go
@ -23,7 +23,7 @@ type Cmd interface {
|
|||||||
App() *cli.App
|
App() *cli.App
|
||||||
// Adds options, parses flags and initialise
|
// Adds options, parses flags and initialise
|
||||||
// exits on error
|
// exits on error
|
||||||
Init(opts ...Option)
|
Init(opts ...Option) error
|
||||||
// Options set within this command
|
// Options set within this command
|
||||||
Options() Options
|
Options() Options
|
||||||
}
|
}
|
||||||
@ -323,13 +323,13 @@ func (c *cmd) Before(ctx *cli.Context) error {
|
|||||||
|
|
||||||
// Use an init option?
|
// Use an init option?
|
||||||
if len(clientOpts) > 0 {
|
if len(clientOpts) > 0 {
|
||||||
*c.opts.Client = client.NewClient(clientOpts...)
|
(*c.opts.Client).Init(clientOpts...)
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *cmd) Init(opts ...Option) {
|
func (c *cmd) Init(opts ...Option) error {
|
||||||
for _, o := range opts {
|
for _, o := range opts {
|
||||||
o(&c.opts)
|
o(&c.opts)
|
||||||
}
|
}
|
||||||
@ -337,10 +337,11 @@ func (c *cmd) Init(opts ...Option) {
|
|||||||
c.app.Version = c.opts.Version
|
c.app.Version = c.opts.Version
|
||||||
c.app.Usage = c.opts.Description
|
c.app.Usage = c.opts.Description
|
||||||
c.app.RunAndExitOnError()
|
c.app.RunAndExitOnError()
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func Init(opts ...Option) {
|
func Init(opts ...Option) error {
|
||||||
DefaultCmd.Init(opts...)
|
return DefaultCmd.Init(opts...)
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewCmd(opts ...Option) Cmd {
|
func NewCmd(opts ...Option) Cmd {
|
||||||
|
@ -38,25 +38,6 @@ func client(service micro.Service) {
|
|||||||
fmt.Println(rsp.Greeting)
|
fmt.Println(rsp.Greeting)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Setup some command line flags
|
|
||||||
func flags(service micro.Service) {
|
|
||||||
app := service.Cmd().App()
|
|
||||||
app.Flags = append(app.Flags,
|
|
||||||
&cli.BoolFlag{
|
|
||||||
Name: "client",
|
|
||||||
Usage: "Launch the client",
|
|
||||||
},
|
|
||||||
)
|
|
||||||
|
|
||||||
// Let's launch the server or the client
|
|
||||||
app.Action = func(c *cli.Context) {
|
|
||||||
if c.Bool("client") {
|
|
||||||
client(service)
|
|
||||||
os.Exit(0)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
// Create a new service. Optionally include some options here.
|
// Create a new service. Optionally include some options here.
|
||||||
service := micro.NewService(
|
service := micro.NewService(
|
||||||
@ -65,15 +46,30 @@ func main() {
|
|||||||
micro.Metadata(map[string]string{
|
micro.Metadata(map[string]string{
|
||||||
"type": "helloworld",
|
"type": "helloworld",
|
||||||
}),
|
}),
|
||||||
)
|
|
||||||
|
|
||||||
// Setup some flags. Specify --client to run the client
|
// Setup some flags. Specify --client to run the client
|
||||||
flags(service)
|
|
||||||
|
// Add runtime flags
|
||||||
|
// We could do this below too
|
||||||
|
micro.Flags(cli.BoolFlag{
|
||||||
|
Name: "client",
|
||||||
|
Usage: "Launch the client",
|
||||||
|
}),
|
||||||
|
)
|
||||||
|
|
||||||
// Init will parse the command line flags. Any flags set will
|
// Init will parse the command line flags. Any flags set will
|
||||||
// override the above settings. Options defined here will
|
// override the above settings. Options defined here will
|
||||||
// override anything set on the command line.
|
// override anything set on the command line.
|
||||||
service.Init()
|
service.Init(
|
||||||
|
// Add runtime action
|
||||||
|
// We could actually do this above
|
||||||
|
micro.Action(func(c *cli.Context) {
|
||||||
|
if c.Bool("client") {
|
||||||
|
client(service)
|
||||||
|
os.Exit(0)
|
||||||
|
}
|
||||||
|
}),
|
||||||
|
)
|
||||||
|
|
||||||
// By default we'll run the server unless the flags catch us
|
// By default we'll run the server unless the flags catch us
|
||||||
|
|
||||||
|
@ -21,7 +21,6 @@ package gomicro
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/micro/go-micro/client"
|
"github.com/micro/go-micro/client"
|
||||||
"github.com/micro/go-micro/cmd"
|
|
||||||
"github.com/micro/go-micro/server"
|
"github.com/micro/go-micro/server"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -30,7 +29,7 @@ import (
|
|||||||
// and initialising services.
|
// and initialising services.
|
||||||
type Service interface {
|
type Service interface {
|
||||||
Init(...Option)
|
Init(...Option)
|
||||||
Cmd() cmd.Cmd
|
Options() Options
|
||||||
Client() client.Client
|
Client() client.Client
|
||||||
Server() server.Server
|
Server() server.Server
|
||||||
Run() error
|
Run() error
|
||||||
|
13
options.go
13
options.go
@ -1,6 +1,7 @@
|
|||||||
package gomicro
|
package gomicro
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"github.com/micro/cli"
|
||||||
"github.com/micro/go-micro/broker"
|
"github.com/micro/go-micro/broker"
|
||||||
"github.com/micro/go-micro/client"
|
"github.com/micro/go-micro/client"
|
||||||
"github.com/micro/go-micro/cmd"
|
"github.com/micro/go-micro/cmd"
|
||||||
@ -95,6 +96,18 @@ func Version(v string) Option {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func Flags(flags ...cli.Flag) Option {
|
||||||
|
return func(o *Options) {
|
||||||
|
o.Cmd.App().Flags = append(o.Cmd.App().Flags, flags...)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func Action(a func(*cli.Context)) Option {
|
||||||
|
return func(o *Options) {
|
||||||
|
o.Cmd.App().Action = a
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Metadata associated with the service
|
// Metadata associated with the service
|
||||||
func Metadata(md map[string]string) Option {
|
func Metadata(md map[string]string) Option {
|
||||||
return func(o *Options) {
|
return func(o *Options) {
|
||||||
|
@ -105,12 +105,13 @@ func (s *rpcServer) Options() Options {
|
|||||||
return opts
|
return opts
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *rpcServer) Init(opts ...Option) {
|
func (s *rpcServer) Init(opts ...Option) error {
|
||||||
s.Lock()
|
s.Lock()
|
||||||
for _, opt := range opts {
|
for _, opt := range opts {
|
||||||
opt(&s.opts)
|
opt(&s.opts)
|
||||||
}
|
}
|
||||||
s.Unlock()
|
s.Unlock()
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *rpcServer) NewHandler(h interface{}) Handler {
|
func (s *rpcServer) NewHandler(h interface{}) Handler {
|
||||||
|
@ -40,7 +40,7 @@ import (
|
|||||||
|
|
||||||
type Server interface {
|
type Server interface {
|
||||||
Options() Options
|
Options() Options
|
||||||
Init(...Option)
|
Init(...Option) error
|
||||||
Handle(Handler) error
|
Handle(Handler) error
|
||||||
NewHandler(interface{}) Handler
|
NewHandler(interface{}) Handler
|
||||||
NewSubscriber(string, interface{}) Subscriber
|
NewSubscriber(string, interface{}) Subscriber
|
||||||
|
14
service.go
14
service.go
@ -6,7 +6,6 @@ import (
|
|||||||
"syscall"
|
"syscall"
|
||||||
|
|
||||||
"github.com/micro/go-micro/client"
|
"github.com/micro/go-micro/client"
|
||||||
"github.com/micro/go-micro/cmd"
|
|
||||||
"github.com/micro/go-micro/context"
|
"github.com/micro/go-micro/context"
|
||||||
"github.com/micro/go-micro/server"
|
"github.com/micro/go-micro/server"
|
||||||
)
|
)
|
||||||
@ -31,6 +30,15 @@ func newService(opts ...Option) Service {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s *service) Init(opts ...Option) {
|
func (s *service) Init(opts ...Option) {
|
||||||
|
// We might get more command flags or the action here
|
||||||
|
// This is pretty ugly, find a better way
|
||||||
|
options := newOptions()
|
||||||
|
options.Cmd = s.opts.Cmd
|
||||||
|
for _, o := range opts {
|
||||||
|
o(&options)
|
||||||
|
}
|
||||||
|
s.opts.Cmd = options.Cmd
|
||||||
|
|
||||||
// Initialise the command flags, overriding new service
|
// Initialise the command flags, overriding new service
|
||||||
s.opts.Cmd.Init()
|
s.opts.Cmd.Init()
|
||||||
|
|
||||||
@ -40,8 +48,8 @@ func (s *service) Init(opts ...Option) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *service) Cmd() cmd.Cmd {
|
func (s *service) Options() Options {
|
||||||
return s.opts.Cmd
|
return s.opts
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *service) Client() client.Client {
|
func (s *service) Client() client.Client {
|
||||||
|
Loading…
Reference in New Issue
Block a user