2016-12-14 15:41:48 +00:00
|
|
|
// Package cmd is an interface for parsing the command line
|
2015-02-14 23:33:21 +00:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
2015-08-30 02:24:16 +01:00
|
|
|
"fmt"
|
2015-12-05 19:25:36 +00:00
|
|
|
"math/rand"
|
2015-05-16 00:34:02 +01:00
|
|
|
"strings"
|
2015-12-05 19:25:36 +00:00
|
|
|
"time"
|
2015-02-14 23:33:21 +00:00
|
|
|
|
2020-01-30 14:39:00 +03:00
|
|
|
"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/trace"
|
|
|
|
"github.com/micro/go-micro/v2/registry"
|
|
|
|
"github.com/micro/go-micro/v2/runtime"
|
|
|
|
"github.com/micro/go-micro/v2/server"
|
|
|
|
"github.com/micro/go-micro/v2/store"
|
|
|
|
"github.com/micro/go-micro/v2/transport"
|
|
|
|
"github.com/micro/go-micro/v2/util/log"
|
2020-01-19 00:55:01 +00:00
|
|
|
|
|
|
|
// clients
|
2020-01-30 14:39:00 +03:00
|
|
|
cgrpc "github.com/micro/go-micro/v2/client/grpc"
|
|
|
|
cmucp "github.com/micro/go-micro/v2/client/mucp"
|
2020-01-19 00:55:01 +00:00
|
|
|
|
|
|
|
// servers
|
2020-01-29 15:45:11 +00:00
|
|
|
"github.com/micro/cli/v2"
|
2020-01-30 14:39:00 +03:00
|
|
|
sgrpc "github.com/micro/go-micro/v2/server/grpc"
|
|
|
|
smucp "github.com/micro/go-micro/v2/server/mucp"
|
2016-04-26 18:49:02 +01:00
|
|
|
|
|
|
|
// brokers
|
2020-01-30 14:39:00 +03:00
|
|
|
"github.com/micro/go-micro/v2/broker/memory"
|
|
|
|
"github.com/micro/go-micro/v2/broker/nats"
|
|
|
|
brokerSrv "github.com/micro/go-micro/v2/broker/service"
|
2016-04-26 18:49:02 +01:00
|
|
|
|
|
|
|
// registries
|
2020-01-30 14:39:00 +03:00
|
|
|
"github.com/micro/go-micro/v2/registry/etcd"
|
|
|
|
kreg "github.com/micro/go-micro/v2/registry/kubernetes"
|
|
|
|
"github.com/micro/go-micro/v2/registry/mdns"
|
|
|
|
rmem "github.com/micro/go-micro/v2/registry/memory"
|
|
|
|
regSrv "github.com/micro/go-micro/v2/registry/service"
|
2016-04-26 18:49:02 +01:00
|
|
|
|
|
|
|
// selectors
|
2020-01-30 14:39:00 +03:00
|
|
|
"github.com/micro/go-micro/v2/client/selector/dns"
|
|
|
|
"github.com/micro/go-micro/v2/client/selector/router"
|
|
|
|
"github.com/micro/go-micro/v2/client/selector/static"
|
2016-04-26 18:49:02 +01:00
|
|
|
|
|
|
|
// transports
|
2020-01-30 14:39:00 +03:00
|
|
|
thttp "github.com/micro/go-micro/v2/transport/http"
|
|
|
|
tmem "github.com/micro/go-micro/v2/transport/memory"
|
2019-11-02 13:25:10 +00:00
|
|
|
|
2020-01-06 17:44:32 +00:00
|
|
|
// stores
|
2020-01-30 14:39:00 +03:00
|
|
|
memStore "github.com/micro/go-micro/v2/store/memory"
|
|
|
|
svcStore "github.com/micro/go-micro/v2/store/service"
|
2020-01-29 15:45:11 +00:00
|
|
|
|
|
|
|
// tracers
|
2020-01-30 14:39:00 +03:00
|
|
|
// jTracer "github.com/micro/go-micro/v2/debug/trace/jaeger"
|
|
|
|
memTracer "github.com/micro/go-micro/v2/debug/trace/memory"
|
2015-02-14 23:33:21 +00:00
|
|
|
)
|
|
|
|
|
2016-01-01 01:16:21 +00: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 19:12:17 +00:00
|
|
|
Init(opts ...Option) error
|
2016-01-01 01:16:21 +00:00
|
|
|
// Options set within this command
|
|
|
|
Options() Options
|
|
|
|
}
|
|
|
|
|
|
|
|
type cmd struct {
|
|
|
|
opts Options
|
|
|
|
app *cli.App
|
|
|
|
}
|
|
|
|
|
|
|
|
type Option func(o *Options)
|
|
|
|
|
2015-02-14 23:33:21 +00:00
|
|
|
var (
|
2016-01-01 01:16:21 +00:00
|
|
|
DefaultCmd = newCmd()
|
2015-12-23 22:45:40 +00:00
|
|
|
|
2016-01-01 01:16:21 +00:00
|
|
|
DefaultFlags = []cli.Flag{
|
2020-01-25 13:41:25 +03:00
|
|
|
&cli.StringFlag{
|
|
|
|
Name: "client",
|
|
|
|
EnvVars: []string{"MICRO_CLIENT"},
|
|
|
|
Usage: "Client for go-micro; rpc",
|
2016-11-18 17:29:26 +00:00
|
|
|
},
|
2020-01-25 13:41:25 +03:00
|
|
|
&cli.StringFlag{
|
|
|
|
Name: "client_request_timeout",
|
|
|
|
EnvVars: []string{"MICRO_CLIENT_REQUEST_TIMEOUT"},
|
|
|
|
Usage: "Sets the client request timeout. e.g 500ms, 5s, 1m. Default: 5s",
|
2016-03-29 17:18:39 +01:00
|
|
|
},
|
2020-01-25 13:41:25 +03:00
|
|
|
&cli.IntFlag{
|
|
|
|
Name: "client_retries",
|
|
|
|
EnvVars: []string{"MICRO_CLIENT_RETRIES"},
|
|
|
|
Value: client.DefaultRetries,
|
|
|
|
Usage: "Sets the client retries. Default: 1",
|
2016-03-29 17:18:39 +01:00
|
|
|
},
|
2020-01-25 13:41:25 +03:00
|
|
|
&cli.IntFlag{
|
|
|
|
Name: "client_pool_size",
|
|
|
|
EnvVars: []string{"MICRO_CLIENT_POOL_SIZE"},
|
|
|
|
Usage: "Sets the client connection pool size. Default: 1",
|
2016-06-07 00:46:14 +01:00
|
|
|
},
|
2020-01-25 13:41:25 +03:00
|
|
|
&cli.StringFlag{
|
|
|
|
Name: "client_pool_ttl",
|
|
|
|
EnvVars: []string{"MICRO_CLIENT_POOL_TTL"},
|
|
|
|
Usage: "Sets the client connection pool ttl. e.g 500ms, 5s, 1m. Default: 1m",
|
2016-06-07 00:46:14 +01:00
|
|
|
},
|
2020-01-25 13:41:25 +03:00
|
|
|
&cli.IntFlag{
|
|
|
|
Name: "register_ttl",
|
|
|
|
EnvVars: []string{"MICRO_REGISTER_TTL"},
|
|
|
|
Value: 60,
|
|
|
|
Usage: "Register TTL in seconds",
|
2018-04-06 14:03:39 +01:00
|
|
|
},
|
2020-01-25 13:41:25 +03:00
|
|
|
&cli.IntFlag{
|
|
|
|
Name: "register_interval",
|
|
|
|
EnvVars: []string{"MICRO_REGISTER_INTERVAL"},
|
|
|
|
Value: 30,
|
|
|
|
Usage: "Register interval in seconds",
|
2018-04-06 14:03:39 +01:00
|
|
|
},
|
2020-01-25 13:41:25 +03:00
|
|
|
&cli.StringFlag{
|
|
|
|
Name: "server",
|
|
|
|
EnvVars: []string{"MICRO_SERVER"},
|
|
|
|
Usage: "Server for go-micro; rpc",
|
2018-09-13 19:02:08 +03:00
|
|
|
},
|
2020-01-25 13:41:25 +03:00
|
|
|
&cli.StringFlag{
|
|
|
|
Name: "server_name",
|
|
|
|
EnvVars: []string{"MICRO_SERVER_NAME"},
|
|
|
|
Usage: "Name of the server. go.micro.srv.example",
|
2015-05-26 22:39:48 +01:00
|
|
|
},
|
2020-01-25 13:41:25 +03:00
|
|
|
&cli.StringFlag{
|
|
|
|
Name: "server_version",
|
|
|
|
EnvVars: []string{"MICRO_SERVER_VERSION"},
|
|
|
|
Usage: "Version of the server. 1.1.0",
|
2015-11-08 01:48:48 +00:00
|
|
|
},
|
2020-01-25 13:41:25 +03:00
|
|
|
&cli.StringFlag{
|
|
|
|
Name: "server_id",
|
|
|
|
EnvVars: []string{"MICRO_SERVER_ID"},
|
|
|
|
Usage: "Id of the server. Auto-generated if not specified",
|
2015-05-26 22:39:48 +01:00
|
|
|
},
|
2020-01-25 13:41:25 +03:00
|
|
|
&cli.StringFlag{
|
|
|
|
Name: "server_address",
|
|
|
|
EnvVars: []string{"MICRO_SERVER_ADDRESS"},
|
|
|
|
Usage: "Bind address for the server. 127.0.0.1:8080",
|
2015-05-16 00:34:02 +01:00
|
|
|
},
|
2020-01-25 13:41:25 +03:00
|
|
|
&cli.StringFlag{
|
|
|
|
Name: "server_advertise",
|
|
|
|
EnvVars: []string{"MICRO_SERVER_ADVERTISE"},
|
|
|
|
Usage: "Used instead of the server_address when registering with discovery. 127.0.0.1:8080",
|
2015-11-11 18:22:04 +00:00
|
|
|
},
|
2020-01-25 13:41:25 +03:00
|
|
|
&cli.StringSliceFlag{
|
|
|
|
Name: "server_metadata",
|
|
|
|
EnvVars: []string{"MICRO_SERVER_METADATA"},
|
|
|
|
Value: &cli.StringSlice{},
|
|
|
|
Usage: "A list of key-value pairs defining metadata. version=1.0.0",
|
2015-05-26 22:39:48 +01:00
|
|
|
},
|
2020-01-25 13:41:25 +03:00
|
|
|
&cli.StringFlag{
|
|
|
|
Name: "broker",
|
|
|
|
EnvVars: []string{"MICRO_BROKER"},
|
|
|
|
Usage: "Broker for pub/sub. http, nats, rabbitmq",
|
2015-05-16 00:34:02 +01:00
|
|
|
},
|
2020-01-25 13:41:25 +03:00
|
|
|
&cli.StringFlag{
|
|
|
|
Name: "broker_address",
|
|
|
|
EnvVars: []string{"MICRO_BROKER_ADDRESS"},
|
|
|
|
Usage: "Comma-separated list of broker addresses",
|
2015-05-16 00:34:02 +01:00
|
|
|
},
|
2020-01-25 13:41:25 +03:00
|
|
|
&cli.StringFlag{
|
|
|
|
Name: "profile",
|
|
|
|
Usage: "Debug profiler for cpu and memory stats",
|
|
|
|
EnvVars: []string{"MICRO_DEBUG_PROFILE"},
|
2019-11-06 19:36:04 +00:00
|
|
|
},
|
2020-01-25 13:41:25 +03:00
|
|
|
&cli.StringFlag{
|
|
|
|
Name: "registry",
|
|
|
|
EnvVars: []string{"MICRO_REGISTRY"},
|
|
|
|
Usage: "Registry for discovery. etcd, mdns",
|
2015-05-16 00:34:02 +01:00
|
|
|
},
|
2020-01-25 13:41:25 +03:00
|
|
|
&cli.StringFlag{
|
|
|
|
Name: "registry_address",
|
|
|
|
EnvVars: []string{"MICRO_REGISTRY_ADDRESS"},
|
|
|
|
Usage: "Comma-separated list of registry addresses",
|
2015-05-16 00:34:02 +01:00
|
|
|
},
|
2020-01-25 13:41:25 +03:00
|
|
|
&cli.StringFlag{
|
|
|
|
Name: "runtime",
|
|
|
|
Usage: "Runtime for building and running services e.g local, kubernetes",
|
|
|
|
EnvVars: []string{"MICRO_RUNTIME"},
|
|
|
|
Value: "local",
|
2019-11-06 19:36:04 +00:00
|
|
|
},
|
2020-01-25 13:41:25 +03:00
|
|
|
&cli.StringFlag{
|
|
|
|
Name: "selector",
|
|
|
|
EnvVars: []string{"MICRO_SELECTOR"},
|
|
|
|
Usage: "Selector used to pick nodes for querying",
|
2015-12-09 19:32:10 +00:00
|
|
|
},
|
2020-01-25 13:41:25 +03:00
|
|
|
&cli.StringFlag{
|
|
|
|
Name: "store",
|
|
|
|
EnvVars: []string{"MICRO_STORE"},
|
|
|
|
Usage: "Store used for key-value storage",
|
2020-01-08 12:11:31 +00:00
|
|
|
},
|
2020-01-25 13:41:25 +03:00
|
|
|
&cli.StringFlag{
|
|
|
|
Name: "store_address",
|
|
|
|
EnvVars: []string{"MICRO_STORE_ADDRESS"},
|
|
|
|
Usage: "Comma-separated list of store addresses",
|
2020-01-08 12:11:31 +00:00
|
|
|
},
|
2020-01-25 13:41:25 +03:00
|
|
|
&cli.StringFlag{
|
|
|
|
Name: "store_namespace",
|
|
|
|
EnvVars: []string{"MICRO_STORE_NAMESPACE"},
|
|
|
|
Usage: "Namespace for store data",
|
2020-01-08 12:11:31 +00:00
|
|
|
},
|
2020-01-25 13:41:25 +03:00
|
|
|
&cli.StringFlag{
|
|
|
|
Name: "transport",
|
|
|
|
EnvVars: []string{"MICRO_TRANSPORT"},
|
|
|
|
Usage: "Transport mechanism used; http",
|
2015-05-20 22:57:19 +01:00
|
|
|
},
|
2020-01-25 13:41:25 +03:00
|
|
|
&cli.StringFlag{
|
|
|
|
Name: "transport_address",
|
|
|
|
EnvVars: []string{"MICRO_TRANSPORT_ADDRESS"},
|
|
|
|
Usage: "Comma-separated list of transport addresses",
|
2015-05-20 22:57:19 +01:00
|
|
|
},
|
2020-01-29 15:45:11 +00:00
|
|
|
&cli.StringFlag{
|
|
|
|
Name: "tracer",
|
|
|
|
EnvVars: []string{"MICRO_TRACER"},
|
|
|
|
Usage: "Tracer for distributed tracing, e.g. memory, jaeger",
|
2020-01-30 07:53:31 +00:00
|
|
|
Value: "memory",
|
2020-01-29 15:45:11 +00:00
|
|
|
},
|
|
|
|
&cli.StringFlag{
|
|
|
|
Name: "tracer_address",
|
|
|
|
EnvVars: []string{"MICRO_TRACER_ADDRESS"},
|
|
|
|
Usage: "Comma-separated list of tracer addresses",
|
|
|
|
},
|
2015-04-27 22:21:56 +01:00
|
|
|
}
|
2015-02-14 23:33:21 +00:00
|
|
|
|
2016-03-15 22:12:28 +00:00
|
|
|
DefaultBrokers = map[string]func(...broker.Option) broker.Broker{
|
2019-12-10 18:12:29 +00:00
|
|
|
"service": brokerSrv.NewBroker,
|
|
|
|
"memory": memory.NewBroker,
|
|
|
|
"nats": nats.NewBroker,
|
2015-06-01 18:55:27 +01:00
|
|
|
}
|
2015-05-16 00:34:02 +01:00
|
|
|
|
2016-11-18 17:29:26 +00:00
|
|
|
DefaultClients = map[string]func(...client.Option) client.Client{
|
2019-06-08 19:40:44 +01:00
|
|
|
"mucp": cmucp.NewClient,
|
|
|
|
"grpc": cgrpc.NewClient,
|
2016-11-18 17:29:26 +00:00
|
|
|
}
|
|
|
|
|
2016-03-15 22:20:21 +00:00
|
|
|
DefaultRegistries = map[string]func(...registry.Option) registry.Registry{
|
2019-12-30 17:29:45 +00:00
|
|
|
"service": regSrv.NewRegistry,
|
|
|
|
"etcd": etcd.NewRegistry,
|
|
|
|
"mdns": mdns.NewRegistry,
|
|
|
|
"memory": rmem.NewRegistry,
|
2019-12-27 20:08:46 +00:00
|
|
|
"kubernetes": kreg.NewRegistry,
|
2015-05-16 00:34:02 +01:00
|
|
|
}
|
|
|
|
|
2016-01-01 01:16:21 +00:00
|
|
|
DefaultSelectors = map[string]func(...selector.Option) selector.Selector{
|
2020-01-19 13:32:24 +00:00
|
|
|
"dns": dns.NewSelector,
|
|
|
|
"router": router.NewSelector,
|
|
|
|
"static": static.NewSelector,
|
2015-12-09 19:32:10 +00:00
|
|
|
}
|
|
|
|
|
2016-11-18 17:29:26 +00:00
|
|
|
DefaultServers = map[string]func(...server.Option) server.Server{
|
2019-06-08 19:40:44 +01:00
|
|
|
"mucp": smucp.NewServer,
|
|
|
|
"grpc": sgrpc.NewServer,
|
2016-11-18 17:29:26 +00:00
|
|
|
}
|
|
|
|
|
2016-03-15 22:25:32 +00:00
|
|
|
DefaultTransports = map[string]func(...transport.Option) transport.Transport{
|
2019-01-14 15:27:25 +00:00
|
|
|
"memory": tmem.NewTransport,
|
|
|
|
"http": thttp.NewTransport,
|
2015-06-01 18:55:27 +01:00
|
|
|
}
|
2016-01-27 01:13:31 +00:00
|
|
|
|
2019-11-02 13:25:10 +00:00
|
|
|
DefaultRuntimes = map[string]func(...runtime.Option) runtime.Runtime{
|
2020-01-24 21:31:57 +00:00
|
|
|
"local": runtime.NewRuntime,
|
2019-11-02 13:25:10 +00:00
|
|
|
}
|
|
|
|
|
2020-01-06 17:44:32 +00:00
|
|
|
DefaultStores = map[string]func(...store.Option) store.Store{
|
2020-01-24 21:31:57 +00:00
|
|
|
"memory": memStore.NewStore,
|
|
|
|
"service": svcStore.NewStore,
|
2020-01-06 17:44:32 +00:00
|
|
|
}
|
|
|
|
|
2020-01-29 15:45:11 +00:00
|
|
|
DefaultTracers = map[string]func(...trace.Option) trace.Tracer{
|
|
|
|
"memory": memTracer.NewTracer,
|
|
|
|
// "jaeger": jTracer.NewTracer,
|
|
|
|
}
|
|
|
|
|
2016-01-27 01:13:31 +00:00
|
|
|
// used for default selection as the fall back
|
2019-12-29 21:07:55 +00:00
|
|
|
defaultClient = "grpc"
|
|
|
|
defaultServer = "grpc"
|
2020-01-29 23:14:05 +00:00
|
|
|
defaultBroker = "eats"
|
2019-01-15 16:50:37 +00:00
|
|
|
defaultRegistry = "mdns"
|
|
|
|
defaultSelector = "registry"
|
2016-01-27 01:13:31 +00:00
|
|
|
defaultTransport = "http"
|
2019-11-02 13:25:10 +00:00
|
|
|
defaultRuntime = "local"
|
2020-01-10 21:54:36 +00:00
|
|
|
defaultStore = "memory"
|
2015-06-01 18:55:27 +01:00
|
|
|
)
|
2015-02-14 23:33:21 +00:00
|
|
|
|
2015-12-05 19:25:36 +00:00
|
|
|
func init() {
|
|
|
|
rand.Seed(time.Now().Unix())
|
|
|
|
}
|
|
|
|
|
2016-01-01 01:16:21 +00:00
|
|
|
func newCmd(opts ...Option) Cmd {
|
|
|
|
options := Options{
|
2016-01-02 00:38:57 +00:00
|
|
|
Broker: &broker.DefaultBroker,
|
|
|
|
Client: &client.DefaultClient,
|
|
|
|
Registry: ®istry.DefaultRegistry,
|
|
|
|
Server: &server.DefaultServer,
|
|
|
|
Selector: &selector.DefaultSelector,
|
|
|
|
Transport: &transport.DefaultTransport,
|
2019-11-02 13:25:10 +00:00
|
|
|
Runtime: &runtime.DefaultRuntime,
|
2020-01-06 17:44:32 +00:00
|
|
|
Store: &store.DefaultStore,
|
2020-01-29 15:45:11 +00:00
|
|
|
Tracer: &trace.DefaultTracer,
|
2016-01-02 00:38:57 +00:00
|
|
|
|
2016-01-01 01:16:21 +00:00
|
|
|
Brokers: DefaultBrokers,
|
2016-11-18 17:29:26 +00:00
|
|
|
Clients: DefaultClients,
|
2016-01-01 01:16:21 +00:00
|
|
|
Registries: DefaultRegistries,
|
|
|
|
Selectors: DefaultSelectors,
|
2016-11-18 17:29:26 +00:00
|
|
|
Servers: DefaultServers,
|
2016-01-01 01:16:21 +00:00
|
|
|
Transports: DefaultTransports,
|
2019-11-02 13:25:10 +00:00
|
|
|
Runtimes: DefaultRuntimes,
|
2020-01-06 17:44:32 +00:00
|
|
|
Stores: DefaultStores,
|
2020-01-29 15:45:11 +00:00
|
|
|
Tracers: DefaultTracers,
|
2016-01-01 01:16:21 +00:00
|
|
|
}
|
2015-10-18 00:01:43 +01:00
|
|
|
|
2016-01-01 01:16:21 +00:00
|
|
|
for _, o := range opts {
|
|
|
|
o(&options)
|
|
|
|
}
|
2015-10-18 00:01:43 +01:00
|
|
|
|
2016-01-01 02:45:15 +00:00
|
|
|
if len(options.Description) == 0 {
|
|
|
|
options.Description = "a go-micro service"
|
|
|
|
}
|
|
|
|
|
2016-01-01 01:16:21 +00: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
|
2020-01-25 13:41:25 +03:00
|
|
|
cmd.app.Action = func(c *cli.Context) error {
|
|
|
|
return nil
|
|
|
|
}
|
2016-01-01 02:45:15 +00:00
|
|
|
|
|
|
|
if len(options.Version) == 0 {
|
|
|
|
cmd.app.HideVersion = true
|
|
|
|
}
|
|
|
|
|
2016-01-01 01:16:21 +00: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 00:38:57 +00:00
|
|
|
// If flags are set then use them otherwise do nothing
|
|
|
|
var serverOpts []server.Option
|
|
|
|
var clientOpts []client.Option
|
|
|
|
|
2020-01-29 15:45:11 +00:00
|
|
|
// Set the store
|
2020-01-06 17:44:32 +00:00
|
|
|
if name := ctx.String("store"); len(name) > 0 {
|
|
|
|
s, ok := c.opts.Stores[name]
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("Unsupported store: %s", name)
|
|
|
|
}
|
|
|
|
|
|
|
|
*c.opts.Store = s()
|
|
|
|
}
|
|
|
|
|
2019-11-02 13:25:10 +00:00
|
|
|
// Set the runtime
|
|
|
|
if name := ctx.String("runtime"); len(name) > 0 {
|
|
|
|
r, ok := c.opts.Runtimes[name]
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("Unsupported runtime: %s", name)
|
|
|
|
}
|
|
|
|
|
|
|
|
*c.opts.Runtime = r()
|
|
|
|
}
|
|
|
|
|
2020-01-29 15:45:11 +00:00
|
|
|
// Set the tracer
|
|
|
|
if name := ctx.String("tracer"); len(name) > 0 {
|
|
|
|
r, ok := c.opts.Tracers[name]
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("Unsupported tracer: %s", name)
|
|
|
|
}
|
|
|
|
|
|
|
|
*c.opts.Tracer = r()
|
|
|
|
}
|
|
|
|
|
2016-11-18 17:29:26 +00:00
|
|
|
// Set the client
|
|
|
|
if name := ctx.String("client"); len(name) > 0 {
|
2018-08-08 18:57:29 +01:00
|
|
|
// only change if we have the client and type differs
|
|
|
|
if cl, ok := c.opts.Clients[name]; ok && (*c.opts.Client).String() != name {
|
2016-11-18 17:29:26 +00:00
|
|
|
*c.opts.Client = cl()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set the server
|
|
|
|
if name := ctx.String("server"); len(name) > 0 {
|
2018-08-08 18:57:29 +01:00
|
|
|
// only change if we have the server and type differs
|
|
|
|
if s, ok := c.opts.Servers[name]; ok && (*c.opts.Server).String() != name {
|
2016-11-18 17:29:26 +00:00
|
|
|
*c.opts.Server = s()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-02 00:38:57 +00:00
|
|
|
// Set the broker
|
2018-08-08 18:57:29 +01:00
|
|
|
if name := ctx.String("broker"); len(name) > 0 && (*c.opts.Broker).String() != name {
|
|
|
|
b, ok := c.opts.Brokers[name]
|
|
|
|
if !ok {
|
2016-01-27 01:13:31 +00:00
|
|
|
return fmt.Errorf("Broker %s not found", name)
|
2016-01-02 00:38:57 +00:00
|
|
|
}
|
|
|
|
|
2018-08-08 18:57:29 +01:00
|
|
|
*c.opts.Broker = b()
|
2016-01-02 00:38:57 +00:00
|
|
|
serverOpts = append(serverOpts, server.Broker(*c.opts.Broker))
|
|
|
|
clientOpts = append(clientOpts, client.Broker(*c.opts.Broker))
|
2015-02-14 23:33:21 +00:00
|
|
|
}
|
|
|
|
|
2016-01-02 00:38:57 +00:00
|
|
|
// Set the registry
|
2018-08-08 18:57:29 +01:00
|
|
|
if name := ctx.String("registry"); len(name) > 0 && (*c.opts.Registry).String() != name {
|
|
|
|
r, ok := c.opts.Registries[name]
|
|
|
|
if !ok {
|
2016-01-27 01:13:31 +00:00
|
|
|
return fmt.Errorf("Registry %s not found", name)
|
2016-01-02 00:38:57 +00:00
|
|
|
}
|
|
|
|
|
2018-08-08 18:57:29 +01:00
|
|
|
*c.opts.Registry = r()
|
2016-01-02 00:38:57 +00:00
|
|
|
serverOpts = append(serverOpts, server.Registry(*c.opts.Registry))
|
|
|
|
clientOpts = append(clientOpts, client.Registry(*c.opts.Registry))
|
2016-01-04 23:07:56 +00:00
|
|
|
|
2018-12-06 18:19:05 +00:00
|
|
|
if err := (*c.opts.Selector).Init(selector.Registry(*c.opts.Registry)); err != nil {
|
|
|
|
log.Fatalf("Error configuring registry: %v", err)
|
|
|
|
}
|
|
|
|
|
2016-01-04 23:07:56 +00:00
|
|
|
clientOpts = append(clientOpts, client.Selector(*c.opts.Selector))
|
2016-01-20 15:22:44 +00:00
|
|
|
|
2018-12-06 18:19:05 +00:00
|
|
|
if err := (*c.opts.Broker).Init(broker.Registry(*c.opts.Registry)); err != nil {
|
|
|
|
log.Fatalf("Error configuring broker: %v", err)
|
|
|
|
}
|
2015-06-01 18:55:27 +01:00
|
|
|
}
|
2015-05-20 22:57:19 +01:00
|
|
|
|
2016-01-02 00:38:57 +00:00
|
|
|
// Set the selector
|
2018-08-08 18:57:29 +01:00
|
|
|
if name := ctx.String("selector"); len(name) > 0 && (*c.opts.Selector).String() != name {
|
|
|
|
s, ok := c.opts.Selectors[name]
|
|
|
|
if !ok {
|
2016-01-27 01:13:31 +00:00
|
|
|
return fmt.Errorf("Selector %s not found", name)
|
2016-01-02 00:38:57 +00:00
|
|
|
}
|
|
|
|
|
2018-08-08 18:57:29 +01:00
|
|
|
*c.opts.Selector = s(selector.Registry(*c.opts.Registry))
|
|
|
|
|
2016-01-02 00:38:57 +00:00
|
|
|
// No server option here. Should there be?
|
|
|
|
clientOpts = append(clientOpts, client.Selector(*c.opts.Selector))
|
2015-12-09 19:32:10 +00:00
|
|
|
}
|
|
|
|
|
2016-01-02 00:38:57 +00:00
|
|
|
// Set the transport
|
2018-08-08 18:57:29 +01:00
|
|
|
if name := ctx.String("transport"); len(name) > 0 && (*c.opts.Transport).String() != name {
|
|
|
|
t, ok := c.opts.Transports[name]
|
|
|
|
if !ok {
|
2016-01-27 01:13:31 +00:00
|
|
|
return fmt.Errorf("Transport %s not found", name)
|
2016-01-02 00:38:57 +00:00
|
|
|
}
|
|
|
|
|
2018-08-08 18:57:29 +01:00
|
|
|
*c.opts.Transport = t()
|
2016-01-02 00:38:57 +00:00
|
|
|
serverOpts = append(serverOpts, server.Transport(*c.opts.Transport))
|
|
|
|
clientOpts = append(clientOpts, client.Transport(*c.opts.Transport))
|
2015-02-14 23:33:21 +00:00
|
|
|
}
|
2015-04-27 22:21:56 +01:00
|
|
|
|
2016-01-02 00:38:57 +00:00
|
|
|
// Parse the server options
|
2015-05-26 22:39:48 +01:00
|
|
|
metadata := make(map[string]string)
|
2016-01-01 01:16:21 +00:00
|
|
|
for _, d := range ctx.StringSlice("server_metadata") {
|
2015-05-26 22:39:48 +01: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 00:38:57 +00:00
|
|
|
if len(metadata) > 0 {
|
|
|
|
serverOpts = append(serverOpts, server.Metadata(metadata))
|
|
|
|
}
|
|
|
|
|
2018-08-08 18:57:29 +01:00
|
|
|
if len(ctx.String("broker_address")) > 0 {
|
2018-12-06 18:19:05 +00:00
|
|
|
if err := (*c.opts.Broker).Init(broker.Addrs(strings.Split(ctx.String("broker_address"), ",")...)); err != nil {
|
|
|
|
log.Fatalf("Error configuring broker: %v", err)
|
|
|
|
}
|
2018-08-08 18:57:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(ctx.String("registry_address")) > 0 {
|
2018-12-06 18:19:05 +00:00
|
|
|
if err := (*c.opts.Registry).Init(registry.Addrs(strings.Split(ctx.String("registry_address"), ",")...)); err != nil {
|
|
|
|
log.Fatalf("Error configuring registry: %v", err)
|
|
|
|
}
|
2018-08-08 18:57:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(ctx.String("transport_address")) > 0 {
|
2018-12-06 18:19:05 +00:00
|
|
|
if err := (*c.opts.Transport).Init(transport.Addrs(strings.Split(ctx.String("transport_address"), ",")...)); err != nil {
|
|
|
|
log.Fatalf("Error configuring transport: %v", err)
|
|
|
|
}
|
2018-08-08 18:57:29 +01:00
|
|
|
}
|
|
|
|
|
2020-01-08 12:11:31 +00:00
|
|
|
if len(ctx.String("store_address")) > 0 {
|
|
|
|
if err := (*c.opts.Store).Init(store.Nodes(strings.Split(ctx.String("store_address"), ",")...)); err != nil {
|
|
|
|
log.Fatalf("Error configuring store: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(ctx.String("store_namespace")) > 0 {
|
|
|
|
if err := (*c.opts.Store).Init(store.Namespace(ctx.String("store_address"))); err != nil {
|
|
|
|
log.Fatalf("Error configuring store: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-02 00:38:57 +00:00
|
|
|
if len(ctx.String("server_name")) > 0 {
|
|
|
|
serverOpts = append(serverOpts, server.Name(ctx.String("server_name")))
|
|
|
|
}
|
2015-05-26 22:39:48 +01:00
|
|
|
|
2016-01-02 00:38:57 +00: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")))
|
|
|
|
}
|
|
|
|
|
2020-01-25 13:41:25 +03:00
|
|
|
if ttl := time.Duration(ctx.Int("register_ttl")); ttl >= 0 {
|
2018-04-06 14:03:39 +01:00
|
|
|
serverOpts = append(serverOpts, server.RegisterTTL(ttl*time.Second))
|
|
|
|
}
|
|
|
|
|
2020-01-25 13:41:25 +03:00
|
|
|
if val := time.Duration(ctx.Int("register_interval")); val >= 0 {
|
2019-01-24 13:22:17 +00:00
|
|
|
serverOpts = append(serverOpts, server.RegisterInterval(val*time.Second))
|
|
|
|
}
|
|
|
|
|
2016-03-29 17:18:39 +01:00
|
|
|
// client opts
|
2018-07-20 14:20:23 +08:00
|
|
|
if r := ctx.Int("client_retries"); r >= 0 {
|
2016-03-29 17:18:39 +01:00
|
|
|
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 00:46:14 +01: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 00:38:57 +00:00
|
|
|
// We have some command line opts for the server.
|
|
|
|
// Lets set it up
|
|
|
|
if len(serverOpts) > 0 {
|
2018-12-06 18:19:05 +00:00
|
|
|
if err := (*c.opts.Server).Init(serverOpts...); err != nil {
|
|
|
|
log.Fatalf("Error configuring server: %v", err)
|
|
|
|
}
|
2016-01-02 00:38:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Use an init option?
|
|
|
|
if len(clientOpts) > 0 {
|
2018-12-06 18:19:05 +00:00
|
|
|
if err := (*c.opts.Client).Init(clientOpts...); err != nil {
|
|
|
|
log.Fatalf("Error configuring client: %v", err)
|
|
|
|
}
|
2016-01-02 00:38:57 +00:00
|
|
|
}
|
2015-08-30 21:22:35 +01:00
|
|
|
|
2015-04-27 22:21:56 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-01-02 19:12:17 +00:00
|
|
|
func (c *cmd) Init(opts ...Option) error {
|
2016-01-01 01:16:21 +00:00
|
|
|
for _, o := range opts {
|
|
|
|
o(&c.opts)
|
2015-04-27 22:21:56 +01:00
|
|
|
}
|
2020-01-19 00:55:01 +00:00
|
|
|
if len(c.opts.Name) > 0 {
|
|
|
|
c.app.Name = c.opts.Name
|
|
|
|
}
|
|
|
|
if len(c.opts.Version) > 0 {
|
|
|
|
c.app.Version = c.opts.Version
|
|
|
|
}
|
2017-05-09 15:49:59 +08:00
|
|
|
c.app.HideVersion = len(c.opts.Version) == 0
|
2016-01-01 01:16:21 +00:00
|
|
|
c.app.Usage = c.opts.Description
|
|
|
|
c.app.RunAndExitOnError()
|
2016-01-02 19:12:17 +00:00
|
|
|
return nil
|
2016-01-01 01:16:21 +00:00
|
|
|
}
|
2015-04-27 22:21:56 +01:00
|
|
|
|
2016-01-04 11:25:58 +00:00
|
|
|
func DefaultOptions() Options {
|
|
|
|
return DefaultCmd.Options()
|
|
|
|
}
|
|
|
|
|
|
|
|
func App() *cli.App {
|
|
|
|
return DefaultCmd.App()
|
|
|
|
}
|
|
|
|
|
2016-01-02 19:12:17 +00:00
|
|
|
func Init(opts ...Option) error {
|
|
|
|
return DefaultCmd.Init(opts...)
|
2016-01-01 01:16:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewCmd(opts ...Option) Cmd {
|
|
|
|
return newCmd(opts...)
|
2015-02-14 23:33:21 +00:00
|
|
|
}
|