micro/config/cmd/cmd.go

496 lines
13 KiB
Go
Raw Normal View History

2016-12-14 18:41:48 +03:00
// Package cmd is an interface for parsing the command line
package cmd
import (
2015-08-30 04:24:16 +03:00
"fmt"
2016-01-01 05:45:15 +03:00
"io"
2015-12-05 22:25:36 +03:00
"math/rand"
"os"
"strings"
2015-12-05 22:25:36 +03:00
"time"
2016-01-01 04:16:21 +03:00
"github.com/micro/cli"
"github.com/micro/go-micro/client"
2019-06-08 21:40:44 +03:00
cgrpc "github.com/micro/go-micro/client/grpc"
cmucp "github.com/micro/go-micro/client/mucp"
"github.com/micro/go-micro/server"
2019-06-08 21:40:44 +03:00
sgrpc "github.com/micro/go-micro/server/grpc"
2019-06-18 13:04:06 +03:00
smucp "github.com/micro/go-micro/server/mucp"
2019-05-31 02:38:05 +03:00
"github.com/micro/go-micro/util/log"
// brokers
2015-11-20 19:17:33 +03:00
"github.com/micro/go-micro/broker"
"github.com/micro/go-micro/broker/http"
2019-01-14 18:27:25 +03:00
"github.com/micro/go-micro/broker/memory"
2019-05-29 18:46:57 +03:00
"github.com/micro/go-micro/broker/nats"
// registries
2015-11-20 19:17:33 +03:00
"github.com/micro/go-micro/registry"
"github.com/micro/go-micro/registry/consul"
2018-12-04 20:22:20 +03:00
"github.com/micro/go-micro/registry/gossip"
2016-04-27 20:21:05 +03:00
"github.com/micro/go-micro/registry/mdns"
2019-01-14 18:27:25 +03:00
rmem "github.com/micro/go-micro/registry/memory"
// selectors
2019-06-21 17:13:54 +03:00
"github.com/micro/go-micro/client/selector"
"github.com/micro/go-micro/client/selector/dns"
"github.com/micro/go-micro/client/selector/router"
2019-06-21 17:13:54 +03:00
"github.com/micro/go-micro/client/selector/static"
// transports
2019-07-09 20:41:26 +03:00
"github.com/micro/go-micro/transport"
tgrpc "github.com/micro/go-micro/transport/grpc"
thttp "github.com/micro/go-micro/transport/http"
tmem "github.com/micro/go-micro/transport/memory"
)
2016-01-01 04:16:21 +03:00
type Cmd interface {
// The cli app within this cmd
App() *cli.App
// Adds options, parses flags and initialise
// exits on error
2016-01-02 22:12:17 +03:00
Init(opts ...Option) error
2016-01-01 04:16:21 +03:00
// Options set within this command
Options() Options
}
type cmd struct {
opts Options
app *cli.App
}
type Option func(o *Options)
var (
2016-01-01 04:16:21 +03:00
DefaultCmd = newCmd()
2015-12-24 01:45:40 +03:00
2016-01-01 04:16:21 +03:00
DefaultFlags = []cli.Flag{
cli.StringFlag{
Name: "client",
EnvVar: "MICRO_CLIENT",
Usage: "Client for go-micro; rpc",
},
cli.StringFlag{
Name: "client_request_timeout",
EnvVar: "MICRO_CLIENT_REQUEST_TIMEOUT",
Usage: "Sets the client request timeout. e.g 500ms, 5s, 1m. Default: 5s",
},
cli.IntFlag{
Name: "client_retries",
EnvVar: "MICRO_CLIENT_RETRIES",
Value: client.DefaultRetries,
Usage: "Sets the client retries. Default: 1",
},
2016-06-07 02:46:14 +03:00
cli.IntFlag{
Name: "client_pool_size",
EnvVar: "MICRO_CLIENT_POOL_SIZE",
Usage: "Sets the client connection pool size. Default: 1",
2016-06-07 02:46:14 +03:00
},
cli.StringFlag{
Name: "client_pool_ttl",
EnvVar: "MICRO_CLIENT_POOL_TTL",
Usage: "Sets the client connection pool ttl. e.g 500ms, 5s, 1m. Default: 1m",
},
cli.IntFlag{
Name: "register_ttl",
EnvVar: "MICRO_REGISTER_TTL",
Usage: "Register TTL in seconds",
},
cli.IntFlag{
Name: "register_interval",
EnvVar: "MICRO_REGISTER_INTERVAL",
Usage: "Register interval in seconds",
},
cli.StringFlag{
Name: "server",
EnvVar: "MICRO_SERVER",
Usage: "Server for go-micro; rpc",
},
2015-05-27 00:39:48 +03:00
cli.StringFlag{
Name: "server_name",
EnvVar: "MICRO_SERVER_NAME",
Usage: "Name of the server. go.micro.srv.example",
},
2015-11-08 04:48:48 +03:00
cli.StringFlag{
Name: "server_version",
EnvVar: "MICRO_SERVER_VERSION",
Usage: "Version of the server. 1.1.0",
},
2015-05-27 00:39:48 +03:00
cli.StringFlag{
Name: "server_id",
EnvVar: "MICRO_SERVER_ID",
Usage: "Id of the server. Auto-generated if not specified",
},
cli.StringFlag{
Name: "server_address",
EnvVar: "MICRO_SERVER_ADDRESS",
Usage: "Bind address for the server. 127.0.0.1:8080",
},
2015-11-11 21:22:04 +03:00
cli.StringFlag{
Name: "server_advertise",
EnvVar: "MICRO_SERVER_ADVERTISE",
Usage: "Used instead of the server_address when registering with discovery. 127.0.0.1:8080",
},
2015-05-27 00:39:48 +03:00
cli.StringSliceFlag{
Name: "server_metadata",
EnvVar: "MICRO_SERVER_METADATA",
Value: &cli.StringSlice{},
Usage: "A list of key-value pairs defining metadata. version=1.0.0",
},
cli.StringFlag{
Name: "broker",
EnvVar: "MICRO_BROKER",
Usage: "Broker for pub/sub. http, nats, rabbitmq",
},
cli.StringFlag{
Name: "broker_address",
EnvVar: "MICRO_BROKER_ADDRESS",
Usage: "Comma-separated list of broker addresses",
},
cli.StringFlag{
Name: "registry",
EnvVar: "MICRO_REGISTRY",
2016-06-29 00:41:44 +03:00
Usage: "Registry for discovery. consul, mdns",
},
cli.StringFlag{
Name: "registry_address",
EnvVar: "MICRO_REGISTRY_ADDRESS",
Usage: "Comma-separated list of registry addresses",
},
2015-12-09 22:32:10 +03:00
cli.StringFlag{
Name: "selector",
EnvVar: "MICRO_SELECTOR",
2016-05-04 00:11:42 +03:00
Usage: "Selector used to pick nodes for querying",
2015-12-09 22:32:10 +03:00
},
2015-05-21 00:57:19 +03:00
cli.StringFlag{
Name: "transport",
EnvVar: "MICRO_TRANSPORT",
2016-06-29 00:41:44 +03:00
Usage: "Transport mechanism used; http",
2015-05-21 00:57:19 +03:00
},
cli.StringFlag{
Name: "transport_address",
EnvVar: "MICRO_TRANSPORT_ADDRESS",
Usage: "Comma-separated list of transport addresses",
},
}
2016-03-16 01:12:28 +03:00
DefaultBrokers = map[string]func(...broker.Option) broker.Broker{
2019-01-14 18:27:25 +03:00
"http": http.NewBroker,
"memory": memory.NewBroker,
2019-05-29 18:46:57 +03:00
"nats": nats.NewBroker,
}
DefaultClients = map[string]func(...client.Option) client.Client{
2019-06-18 13:04:06 +03:00
"rpc": client.NewClient,
2019-06-08 21:40:44 +03:00
"mucp": cmucp.NewClient,
"grpc": cgrpc.NewClient,
}
2016-03-16 01:20:21 +03:00
DefaultRegistries = map[string]func(...registry.Option) registry.Registry{
"consul": consul.NewRegistry,
2018-12-04 20:22:20 +03:00
"gossip": gossip.NewRegistry,
2016-04-30 02:20:05 +03:00
"mdns": mdns.NewRegistry,
2019-01-14 18:27:25 +03:00
"memory": rmem.NewRegistry,
}
2016-01-01 04:16:21 +03:00
DefaultSelectors = map[string]func(...selector.Option) selector.Selector{
2016-05-05 23:14:57 +03:00
"default": selector.NewSelector,
2019-01-07 10:41:26 +03:00
"dns": dns.NewSelector,
"cache": selector.NewSelector,
"router": router.NewSelector,
2019-01-08 18:38:25 +03:00
"static": static.NewSelector,
2015-12-09 22:32:10 +03:00
}
DefaultServers = map[string]func(...server.Option) server.Server{
2019-06-18 13:04:06 +03:00
"rpc": server.NewServer,
2019-06-08 21:40:44 +03:00
"mucp": smucp.NewServer,
"grpc": sgrpc.NewServer,
}
2016-03-16 01:25:32 +03:00
DefaultTransports = map[string]func(...transport.Option) transport.Transport{
2019-01-14 18:27:25 +03:00
"memory": tmem.NewTransport,
"http": thttp.NewTransport,
2019-05-24 19:15:59 +03:00
"grpc": tgrpc.NewTransport,
}
// used for default selection as the fall back
defaultClient = "rpc"
defaultServer = "rpc"
defaultBroker = "http"
2019-01-15 19:50:37 +03:00
defaultRegistry = "mdns"
defaultSelector = "registry"
defaultTransport = "http"
)
2015-12-05 22:25:36 +03:00
func init() {
rand.Seed(time.Now().Unix())
2016-01-01 05:45:15 +03:00
help := cli.HelpPrinter
cli.HelpPrinter = func(writer io.Writer, templ string, data interface{}) {
help(writer, templ, data)
os.Exit(0)
}
2015-12-05 22:25:36 +03:00
}
2016-01-01 04:16:21 +03:00
func newCmd(opts ...Option) Cmd {
options := Options{
2016-01-02 03:38:57 +03:00
Broker: &broker.DefaultBroker,
Client: &client.DefaultClient,
Registry: &registry.DefaultRegistry,
Server: &server.DefaultServer,
Selector: &selector.DefaultSelector,
Transport: &transport.DefaultTransport,
2016-01-01 04:16:21 +03:00
Brokers: DefaultBrokers,
Clients: DefaultClients,
2016-01-01 04:16:21 +03:00
Registries: DefaultRegistries,
Selectors: DefaultSelectors,
Servers: DefaultServers,
2016-01-01 04:16:21 +03:00
Transports: DefaultTransports,
}
2016-01-01 04:16:21 +03:00
for _, o := range opts {
o(&options)
}
2016-01-01 05:45:15 +03:00
if len(options.Description) == 0 {
options.Description = "a go-micro service"
}
2016-01-01 04:16:21 +03:00
cmd := new(cmd)
cmd.opts = options
cmd.app = cli.NewApp()
cmd.app.Name = cmd.opts.Name
cmd.app.Version = cmd.opts.Version
cmd.app.Usage = cmd.opts.Description
cmd.app.Before = cmd.Before
cmd.app.Flags = DefaultFlags
cmd.app.Action = func(c *cli.Context) {}
2016-01-01 05:45:15 +03:00
if len(options.Version) == 0 {
cmd.app.HideVersion = true
}
2016-01-01 04:16:21 +03:00
return cmd
}
func (c *cmd) App() *cli.App {
return c.app
}
func (c *cmd) Options() Options {
return c.opts
}
func (c *cmd) Before(ctx *cli.Context) error {
2016-01-02 03:38:57 +03:00
// If flags are set then use them otherwise do nothing
var serverOpts []server.Option
var clientOpts []client.Option
// Set the client
if name := ctx.String("client"); len(name) > 0 {
// only change if we have the client and type differs
if cl, ok := c.opts.Clients[name]; ok && (*c.opts.Client).String() != name {
*c.opts.Client = cl()
}
}
// Set the server
if name := ctx.String("server"); len(name) > 0 {
// only change if we have the server and type differs
if s, ok := c.opts.Servers[name]; ok && (*c.opts.Server).String() != name {
*c.opts.Server = s()
}
}
2016-01-02 03:38:57 +03:00
// Set the broker
if name := ctx.String("broker"); len(name) > 0 && (*c.opts.Broker).String() != name {
b, ok := c.opts.Brokers[name]
if !ok {
return fmt.Errorf("Broker %s not found", name)
2016-01-02 03:38:57 +03:00
}
*c.opts.Broker = b()
2016-01-02 03:38:57 +03:00
serverOpts = append(serverOpts, server.Broker(*c.opts.Broker))
clientOpts = append(clientOpts, client.Broker(*c.opts.Broker))
}
2016-01-02 03:38:57 +03:00
// Set the registry
if name := ctx.String("registry"); len(name) > 0 && (*c.opts.Registry).String() != name {
r, ok := c.opts.Registries[name]
if !ok {
return fmt.Errorf("Registry %s not found", name)
2016-01-02 03:38:57 +03:00
}
*c.opts.Registry = r()
2016-01-02 03:38:57 +03:00
serverOpts = append(serverOpts, server.Registry(*c.opts.Registry))
clientOpts = append(clientOpts, client.Registry(*c.opts.Registry))
2016-01-05 02:07:56 +03:00
2018-12-06 21:19:05 +03:00
if err := (*c.opts.Selector).Init(selector.Registry(*c.opts.Registry)); err != nil {
log.Fatalf("Error configuring registry: %v", err)
}
2016-01-05 02:07:56 +03:00
clientOpts = append(clientOpts, client.Selector(*c.opts.Selector))
2016-01-20 18:22:44 +03:00
2018-12-06 21:19:05 +03:00
if err := (*c.opts.Broker).Init(broker.Registry(*c.opts.Registry)); err != nil {
log.Fatalf("Error configuring broker: %v", err)
}
}
2015-05-21 00:57:19 +03:00
2016-01-02 03:38:57 +03:00
// Set the selector
if name := ctx.String("selector"); len(name) > 0 && (*c.opts.Selector).String() != name {
s, ok := c.opts.Selectors[name]
if !ok {
return fmt.Errorf("Selector %s not found", name)
2016-01-02 03:38:57 +03:00
}
*c.opts.Selector = s(selector.Registry(*c.opts.Registry))
2016-01-02 03:38:57 +03:00
// No server option here. Should there be?
clientOpts = append(clientOpts, client.Selector(*c.opts.Selector))
2015-12-09 22:32:10 +03:00
}
2016-01-02 03:38:57 +03:00
// Set the transport
if name := ctx.String("transport"); len(name) > 0 && (*c.opts.Transport).String() != name {
t, ok := c.opts.Transports[name]
if !ok {
return fmt.Errorf("Transport %s not found", name)
2016-01-02 03:38:57 +03:00
}
*c.opts.Transport = t()
2016-01-02 03:38:57 +03:00
serverOpts = append(serverOpts, server.Transport(*c.opts.Transport))
clientOpts = append(clientOpts, client.Transport(*c.opts.Transport))
}
2016-01-02 03:38:57 +03:00
// Parse the server options
2015-05-27 00:39:48 +03:00
metadata := make(map[string]string)
2016-01-01 04:16:21 +03:00
for _, d := range ctx.StringSlice("server_metadata") {
2015-05-27 00:39:48 +03:00
var key, val string
parts := strings.Split(d, "=")
key = parts[0]
if len(parts) > 1 {
val = strings.Join(parts[1:], "=")
}
metadata[key] = val
}
2016-01-02 03:38:57 +03:00
if len(metadata) > 0 {
serverOpts = append(serverOpts, server.Metadata(metadata))
}
if len(ctx.String("broker_address")) > 0 {
2018-12-06 21:19:05 +03:00
if err := (*c.opts.Broker).Init(broker.Addrs(strings.Split(ctx.String("broker_address"), ",")...)); err != nil {
log.Fatalf("Error configuring broker: %v", err)
}
}
if len(ctx.String("registry_address")) > 0 {
2018-12-06 21:19:05 +03:00
if err := (*c.opts.Registry).Init(registry.Addrs(strings.Split(ctx.String("registry_address"), ",")...)); err != nil {
log.Fatalf("Error configuring registry: %v", err)
}
}
if len(ctx.String("transport_address")) > 0 {
2018-12-06 21:19:05 +03:00
if err := (*c.opts.Transport).Init(transport.Addrs(strings.Split(ctx.String("transport_address"), ",")...)); err != nil {
log.Fatalf("Error configuring transport: %v", err)
}
}
2016-01-02 03:38:57 +03:00
if len(ctx.String("server_name")) > 0 {
serverOpts = append(serverOpts, server.Name(ctx.String("server_name")))
}
2015-05-27 00:39:48 +03:00
2016-01-02 03:38:57 +03:00
if len(ctx.String("server_version")) > 0 {
serverOpts = append(serverOpts, server.Version(ctx.String("server_version")))
}
if len(ctx.String("server_id")) > 0 {
serverOpts = append(serverOpts, server.Id(ctx.String("server_id")))
}
if len(ctx.String("server_address")) > 0 {
serverOpts = append(serverOpts, server.Address(ctx.String("server_address")))
}
if len(ctx.String("server_advertise")) > 0 {
serverOpts = append(serverOpts, server.Advertise(ctx.String("server_advertise")))
}
if ttl := time.Duration(ctx.GlobalInt("register_ttl")); ttl > 0 {
serverOpts = append(serverOpts, server.RegisterTTL(ttl*time.Second))
}
2019-01-24 16:22:17 +03:00
if val := time.Duration(ctx.GlobalInt("register_interval")); val > 0 {
serverOpts = append(serverOpts, server.RegisterInterval(val*time.Second))
}
// client opts
2018-07-20 09:20:23 +03:00
if r := ctx.Int("client_retries"); r >= 0 {
clientOpts = append(clientOpts, client.Retries(r))
}
if t := ctx.String("client_request_timeout"); len(t) > 0 {
d, err := time.ParseDuration(t)
if err != nil {
return fmt.Errorf("failed to parse client_request_timeout: %v", t)
}
clientOpts = append(clientOpts, client.RequestTimeout(d))
}
2016-06-07 02:46:14 +03:00
if r := ctx.Int("client_pool_size"); r > 0 {
clientOpts = append(clientOpts, client.PoolSize(r))
}
if t := ctx.String("client_pool_ttl"); len(t) > 0 {
d, err := time.ParseDuration(t)
if err != nil {
return fmt.Errorf("failed to parse client_pool_ttl: %v", t)
}
clientOpts = append(clientOpts, client.PoolTTL(d))
}
2016-01-02 03:38:57 +03:00
// We have some command line opts for the server.
// Lets set it up
if len(serverOpts) > 0 {
2018-12-06 21:19:05 +03:00
if err := (*c.opts.Server).Init(serverOpts...); err != nil {
log.Fatalf("Error configuring server: %v", err)
}
2016-01-02 03:38:57 +03:00
}
// Use an init option?
if len(clientOpts) > 0 {
2018-12-06 21:19:05 +03:00
if err := (*c.opts.Client).Init(clientOpts...); err != nil {
log.Fatalf("Error configuring client: %v", err)
}
2016-01-02 03:38:57 +03:00
}
2015-08-30 23:22:35 +03:00
return nil
}
2016-01-02 22:12:17 +03:00
func (c *cmd) Init(opts ...Option) error {
2016-01-01 04:16:21 +03:00
for _, o := range opts {
o(&c.opts)
}
2016-01-01 04:16:21 +03:00
c.app.Name = c.opts.Name
c.app.Version = c.opts.Version
2017-05-09 10:49:59 +03:00
c.app.HideVersion = len(c.opts.Version) == 0
2016-01-01 04:16:21 +03:00
c.app.Usage = c.opts.Description
c.app.RunAndExitOnError()
2016-01-02 22:12:17 +03:00
return nil
2016-01-01 04:16:21 +03:00
}
func DefaultOptions() Options {
return DefaultCmd.Options()
}
func App() *cli.App {
return DefaultCmd.App()
}
2016-01-02 22:12:17 +03:00
func Init(opts ...Option) error {
return DefaultCmd.Init(opts...)
2016-01-01 04:16:21 +03:00
}
func NewCmd(opts ...Option) Cmd {
return newCmd(opts...)
}