1
0
mirror of https://github.com/go-micro/go-micro.git synced 2024-11-24 08:02:32 +02:00

Update the way flags are used

This commit is contained in:
Asim 2016-01-02 19:12:17 +00:00
parent 1f1bc27421
commit 3b56a62589
9 changed files with 66 additions and 35 deletions

View File

@ -26,6 +26,8 @@ import (
)
type Client interface {
Init(...Option) error
Options() Options
NewPublication(topic string, msg interface{}) Publication
NewRequest(service, method string, req interface{}, reqOpts ...RequestOption) Request
NewProtoRequest(service, method string, req interface{}, reqOpts ...RequestOption) Request

View File

@ -149,6 +149,17 @@ func (r *rpcClient) stream(ctx context.Context, address string, req Request) (St
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 {
return r.call(ctx, address, request, response)
}

View File

@ -23,7 +23,7 @@ type Cmd interface {
App() *cli.App
// Adds options, parses flags and initialise
// exits on error
Init(opts ...Option)
Init(opts ...Option) error
// Options set within this command
Options() Options
}
@ -323,13 +323,13 @@ func (c *cmd) Before(ctx *cli.Context) error {
// Use an init option?
if len(clientOpts) > 0 {
*c.opts.Client = client.NewClient(clientOpts...)
(*c.opts.Client).Init(clientOpts...)
}
return nil
}
func (c *cmd) Init(opts ...Option) {
func (c *cmd) Init(opts ...Option) error {
for _, o := range opts {
o(&c.opts)
}
@ -337,10 +337,11 @@ func (c *cmd) Init(opts ...Option) {
c.app.Version = c.opts.Version
c.app.Usage = c.opts.Description
c.app.RunAndExitOnError()
return nil
}
func Init(opts ...Option) {
DefaultCmd.Init(opts...)
func Init(opts ...Option) error {
return DefaultCmd.Init(opts...)
}
func NewCmd(opts ...Option) Cmd {

View File

@ -38,25 +38,6 @@ func client(service micro.Service) {
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() {
// Create a new service. Optionally include some options here.
service := micro.NewService(
@ -65,15 +46,30 @@ func main() {
micro.Metadata(map[string]string{
"type": "helloworld",
}),
)
// Setup some flags. Specify --client to run the client
flags(service)
// Setup some flags. Specify --client to run the client
// 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
// override the above settings. Options defined here will
// 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

View File

@ -21,7 +21,6 @@ package gomicro
import (
"github.com/micro/go-micro/client"
"github.com/micro/go-micro/cmd"
"github.com/micro/go-micro/server"
)
@ -30,7 +29,7 @@ import (
// and initialising services.
type Service interface {
Init(...Option)
Cmd() cmd.Cmd
Options() Options
Client() client.Client
Server() server.Server
Run() error

View File

@ -1,6 +1,7 @@
package gomicro
import (
"github.com/micro/cli"
"github.com/micro/go-micro/broker"
"github.com/micro/go-micro/client"
"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
func Metadata(md map[string]string) Option {
return func(o *Options) {

View File

@ -105,12 +105,13 @@ func (s *rpcServer) Options() Options {
return opts
}
func (s *rpcServer) Init(opts ...Option) {
func (s *rpcServer) Init(opts ...Option) error {
s.Lock()
for _, opt := range opts {
opt(&s.opts)
}
s.Unlock()
return nil
}
func (s *rpcServer) NewHandler(h interface{}) Handler {

View File

@ -40,7 +40,7 @@ import (
type Server interface {
Options() Options
Init(...Option)
Init(...Option) error
Handle(Handler) error
NewHandler(interface{}) Handler
NewSubscriber(string, interface{}) Subscriber

View File

@ -6,7 +6,6 @@ import (
"syscall"
"github.com/micro/go-micro/client"
"github.com/micro/go-micro/cmd"
"github.com/micro/go-micro/context"
"github.com/micro/go-micro/server"
)
@ -31,6 +30,15 @@ func newService(opts ...Option) Service {
}
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
s.opts.Cmd.Init()
@ -40,8 +48,8 @@ func (s *service) Init(opts ...Option) {
}
}
func (s *service) Cmd() cmd.Cmd {
return s.opts.Cmd
func (s *service) Options() Options {
return s.opts
}
func (s *service) Client() client.Client {