2020-07-19 19:20:05 +03:00
// Package cmd is an interface for building a command line binary
2015-02-15 02:33:21 +03:00
package cmd
import (
2020-07-08 10:50:08 +03:00
"crypto/tls"
"crypto/x509"
"io/ioutil"
2015-12-05 22:25:36 +03:00
"math/rand"
2020-07-19 20:14:18 +03:00
"os"
2015-05-16 02:34:02 +03:00
"strings"
2015-12-05 22:25:36 +03:00
"time"
2015-02-15 02:33:21 +03:00
2020-07-19 21:48:11 +03:00
"github.com/micro/cli/v2"
2020-02-03 11:16:02 +03:00
"github.com/micro/go-micro/v2/auth"
2020-01-30 14:39:00 +03:00
"github.com/micro/go-micro/v2/broker"
2020-07-19 21:48:11 +03:00
brokerSrv "github.com/micro/go-micro/v2/broker/service"
2020-01-30 14:39:00 +03:00
"github.com/micro/go-micro/v2/client"
2020-05-14 13:25:19 +03:00
"github.com/micro/go-micro/v2/client/grpc"
2020-03-12 21:13:03 +03:00
"github.com/micro/go-micro/v2/config"
2020-02-25 19:42:43 +03:00
"github.com/micro/go-micro/v2/debug/profile"
2020-01-30 14:39:00 +03:00
"github.com/micro/go-micro/v2/debug/trace"
2020-03-11 20:55:39 +03:00
"github.com/micro/go-micro/v2/logger"
2020-01-30 14:39:00 +03:00
"github.com/micro/go-micro/v2/registry"
2020-05-14 13:06:22 +03:00
registrySrv "github.com/micro/go-micro/v2/registry/service"
2020-06-24 13:46:51 +03:00
"github.com/micro/go-micro/v2/router"
2020-07-19 21:48:11 +03:00
srvRouter "github.com/micro/go-micro/v2/router/service"
2020-01-30 14:39:00 +03:00
"github.com/micro/go-micro/v2/runtime"
2020-07-01 19:06:59 +03:00
"github.com/micro/go-micro/v2/selector"
2020-01-30 14:39:00 +03:00
"github.com/micro/go-micro/v2/server"
"github.com/micro/go-micro/v2/store"
"github.com/micro/go-micro/v2/transport"
2020-06-03 11:43:20 +03:00
authutil "github.com/micro/go-micro/v2/util/auth"
2020-05-14 13:25:19 +03:00
"github.com/micro/go-micro/v2/util/wrapper"
2020-01-19 03:55:01 +03:00
2020-07-19 21:48:11 +03:00
configSrc "github.com/micro/go-micro/v2/config/source"
configSrv "github.com/micro/go-micro/v2/config/source/service"
2015-02-15 02:33:21 +03:00
)
2016-01-01 04:16:21 +03:00
type Cmd interface {
2020-07-19 20:14:18 +03:00
// Init initialises options
// Note: Use Run to parse command line
2020-07-19 19:20:05 +03:00
Init ( opts ... Option ) error
2016-01-01 04:16:21 +03:00
// Options set within this command
Options ( ) Options
2020-07-19 20:14:18 +03:00
// The cli app within this cmd
App ( ) * cli . App
// Run executes the command
Run ( ) error
2020-07-19 19:20:05 +03:00
// Implementation
String ( ) string
2016-01-01 04:16:21 +03:00
}
type cmd struct {
opts Options
app * cli . App
}
type Option func ( o * Options )
2015-02-15 02:33:21 +03:00
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 {
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 20:29:26 +03: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 19:18:39 +03: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 19:18:39 +03: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 02:46:14 +03: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 02:46:14 +03: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 16:03:39 +03: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 16:03:39 +03: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-27 00:39:48 +03: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 04:48:48 +03: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-27 00:39:48 +03: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 02:34:02 +03: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 21:22:04 +03: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-27 00:39:48 +03: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 02:34:02 +03: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 02:34:02 +03:00
} ,
2020-07-09 20:02:24 +03:00
& cli . StringFlag {
Name : "broker_tls_ca" ,
Usage : "Certificate authority for TLS with broker" ,
EnvVars : [ ] string { "MICRO_BROKER_TLS_CA" } ,
} ,
& cli . StringFlag {
Name : "broker_tls_cert" ,
Usage : "Client cert for TLS with broker" ,
EnvVars : [ ] string { "MICRO_BROKER_TLS_CERT" } ,
} ,
& cli . StringFlag {
Name : "broker_tls_key" ,
Usage : "Client key for TLS with broker" ,
EnvVars : [ ] string { "MICRO_BROKER_TLS_KEY" } ,
2020-07-08 18:22:48 +03: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 22:36:04 +03: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 02:34:02 +03: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 02:34:02 +03:00
} ,
2020-07-09 20:02:24 +03:00
& cli . StringFlag {
Name : "registry_tls_ca" ,
Usage : "Certificate authority for TLS with registry" ,
EnvVars : [ ] string { "MICRO_REGISTRY_TLS_CA" } ,
} ,
& cli . StringFlag {
Name : "registry_tls_cert" ,
Usage : "Client cert for TLS with registry" ,
EnvVars : [ ] string { "MICRO_REGISTRY_TLS_CERT" } ,
2020-07-08 10:50:08 +03:00
} ,
2020-01-25 13:41:25 +03:00
& cli . StringFlag {
2020-07-09 20:02:24 +03:00
Name : "registry_tls_key" ,
Usage : "Client key for TLS with registry" ,
EnvVars : [ ] string { "MICRO_REGISTRY_TLS_KEY" } ,
} , & cli . StringFlag {
2020-01-25 13:41:25 +03:00
Name : "runtime" ,
Usage : "Runtime for building and running services e.g local, kubernetes" ,
EnvVars : [ ] string { "MICRO_RUNTIME" } ,
Value : "local" ,
2019-11-06 22:36:04 +03:00
} ,
2020-02-05 16:59:35 +03:00
& cli . StringFlag {
Name : "runtime_source" ,
Usage : "Runtime source for building and running services e.g github.com/micro/service" ,
EnvVars : [ ] string { "MICRO_RUNTIME_SOURCE" } ,
Value : "github.com/micro/services" ,
} ,
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 22:32:10 +03: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 15:11:31 +03: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 15:11:31 +03:00
} ,
2020-01-25 13:41:25 +03:00
& cli . StringFlag {
2020-04-06 18:45:55 +03:00
Name : "store_database" ,
EnvVars : [ ] string { "MICRO_STORE_DATABASE" } ,
Usage : "Database option for the underlying store" ,
} ,
& cli . StringFlag {
Name : "store_table" ,
2020-04-08 21:44:49 +03:00
EnvVars : [ ] string { "MICRO_STORE_TABLE" } ,
2020-04-06 18:45:55 +03:00
Usage : "Table option for the underlying store" ,
2020-01-08 15:11:31 +03:00
} ,
2020-01-25 13:41:25 +03:00
& cli . StringFlag {
Name : "transport" ,
EnvVars : [ ] string { "MICRO_TRANSPORT" } ,
Usage : "Transport mechanism used; http" ,
2015-05-21 00:57:19 +03: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-21 00:57:19 +03:00
} ,
2020-01-29 18:45:11 +03:00
& cli . StringFlag {
Name : "tracer" ,
EnvVars : [ ] string { "MICRO_TRACER" } ,
Usage : "Tracer for distributed tracing, e.g. memory, jaeger" ,
} ,
& cli . StringFlag {
Name : "tracer_address" ,
EnvVars : [ ] string { "MICRO_TRACER_ADDRESS" } ,
Usage : "Comma-separated list of tracer addresses" ,
} ,
2020-02-03 11:16:02 +03:00
& cli . StringFlag {
Name : "auth" ,
EnvVars : [ ] string { "MICRO_AUTH" } ,
Usage : "Auth for role based access control, e.g. service" ,
} ,
2020-06-24 15:47:43 +03:00
& cli . StringFlag {
Name : "auth_address" ,
EnvVars : [ ] string { "MICRO_AUTH_ADDRESS" } ,
Usage : "Comma-separated list of auth addresses" ,
} ,
2020-02-26 01:15:44 +03:00
& cli . StringFlag {
2020-03-31 14:44:34 +03:00
Name : "auth_id" ,
EnvVars : [ ] string { "MICRO_AUTH_ID" } ,
Usage : "Account ID used for client authentication" ,
} ,
& cli . StringFlag {
Name : "auth_secret" ,
EnvVars : [ ] string { "MICRO_AUTH_SECRET" } ,
Usage : "Account secret used for client authentication" ,
2020-02-26 01:15:44 +03:00
} ,
2020-05-12 18:41:29 +03:00
& cli . StringFlag {
2020-06-17 14:26:27 +03:00
Name : "service_namespace" ,
EnvVars : [ ] string { "MICRO_NAMESPACE" } ,
Usage : "Namespace the service is operating in" ,
2020-06-19 11:24:32 +03:00
Value : "micro" ,
2020-05-12 18:41:29 +03:00
} ,
2020-02-03 11:16:02 +03:00
& cli . StringFlag {
Name : "auth_public_key" ,
EnvVars : [ ] string { "MICRO_AUTH_PUBLIC_KEY" } ,
Usage : "Public key for JWT auth (base64 encoded PEM)" ,
} ,
& cli . StringFlag {
Name : "auth_private_key" ,
EnvVars : [ ] string { "MICRO_AUTH_PRIVATE_KEY" } ,
Usage : "Private key for JWT auth (base64 encoded PEM)" ,
} ,
2020-03-12 21:13:03 +03:00
& cli . StringFlag {
Name : "config" ,
EnvVars : [ ] string { "MICRO_CONFIG" } ,
Usage : "The source of the config to be used to get configuration" ,
} ,
2020-06-24 13:46:51 +03:00
& cli . StringFlag {
Name : "router" ,
EnvVars : [ ] string { "MICRO_ROUTER" } ,
Usage : "Router used for client requests" ,
} ,
2020-06-26 12:38:11 +03:00
& cli . StringFlag {
Name : "router_address" ,
Usage : "Comma-separated list of router addresses" ,
EnvVars : [ ] string { "MICRO_ROUTER_ADDRESS" } ,
} ,
2015-04-28 00:21:56 +03:00
}
2015-02-15 02:33:21 +03:00
2020-07-19 21:48:11 +03:00
DefaultBrokers = map [ string ] func ( ... broker . Option ) broker . Broker { }
2015-05-16 02:34:02 +03:00
2020-07-19 21:48:11 +03:00
DefaultClients = map [ string ] func ( ... client . Option ) client . Client { }
2016-11-18 20:29:26 +03:00
2020-07-19 21:48:11 +03:00
DefaultRegistries = map [ string ] func ( ... registry . Option ) registry . Registry { }
2015-05-16 02:34:02 +03:00
2020-07-19 21:48:11 +03:00
DefaultRouters = map [ string ] func ( ... router . Option ) router . Router { }
2020-06-24 13:46:51 +03:00
2020-07-19 21:48:11 +03:00
DefaultSelectors = map [ string ] func ( ... selector . Option ) selector . Selector { }
2015-12-09 22:32:10 +03:00
2020-07-19 21:48:11 +03:00
DefaultServers = map [ string ] func ( ... server . Option ) server . Server { }
2016-11-18 20:29:26 +03:00
2020-07-19 21:48:11 +03:00
DefaultTransports = map [ string ] func ( ... transport . Option ) transport . Transport { }
2016-01-27 04:13:31 +03:00
2020-07-19 21:48:11 +03:00
DefaultRuntimes = map [ string ] func ( ... runtime . Option ) runtime . Runtime { }
2019-11-02 16:25:10 +03:00
2020-07-19 21:48:11 +03:00
DefaultStores = map [ string ] func ( ... store . Option ) store . Store { }
2020-01-06 20:44:32 +03:00
2020-07-19 21:48:11 +03:00
DefaultTracers = map [ string ] func ( ... trace . Option ) trace . Tracer { }
2020-01-29 18:45:11 +03:00
2020-07-19 21:48:11 +03:00
DefaultAuths = map [ string ] func ( ... auth . Option ) auth . Auth { }
2020-02-25 19:42:43 +03:00
2020-07-19 21:48:11 +03:00
DefaultProfiles = map [ string ] func ( ... profile . Option ) profile . Profile { }
2020-03-12 21:13:03 +03:00
2020-07-19 21:48:11 +03:00
DefaultConfigs = map [ string ] func ( ... config . Option ) ( config . Config , error ) { }
2015-06-01 20:55:27 +03:00
)
2015-02-15 02:33:21 +03:00
2015-12-05 22:25:36 +03:00
func init ( ) {
rand . Seed ( time . Now ( ) . Unix ( ) )
}
2016-01-01 04:16:21 +03:00
func newCmd ( opts ... Option ) Cmd {
options := Options {
2020-02-10 11:26:28 +03:00
Auth : & auth . DefaultAuth ,
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 ,
2020-06-24 13:46:51 +03:00
Router : & router . DefaultRouter ,
2019-11-02 16:25:10 +03:00
Runtime : & runtime . DefaultRuntime ,
2020-01-06 20:44:32 +03:00
Store : & store . DefaultStore ,
2020-01-29 18:45:11 +03:00
Tracer : & trace . DefaultTracer ,
2020-02-25 19:42:43 +03:00
Profile : & profile . DefaultProfile ,
2020-03-12 21:13:03 +03:00
Config : & config . DefaultConfig ,
2016-01-02 03:38:57 +03:00
2016-01-01 04:16:21 +03:00
Brokers : DefaultBrokers ,
2016-11-18 20:29:26 +03:00
Clients : DefaultClients ,
2016-01-01 04:16:21 +03:00
Registries : DefaultRegistries ,
Selectors : DefaultSelectors ,
2016-11-18 20:29:26 +03:00
Servers : DefaultServers ,
2016-01-01 04:16:21 +03:00
Transports : DefaultTransports ,
2020-06-24 13:46:51 +03:00
Routers : DefaultRouters ,
2019-11-02 16:25:10 +03:00
Runtimes : DefaultRuntimes ,
2020-01-06 20:44:32 +03:00
Stores : DefaultStores ,
2020-01-29 18:45:11 +03:00
Tracers : DefaultTracers ,
2020-02-03 11:16:02 +03:00
Auths : DefaultAuths ,
2020-02-25 19:42:43 +03:00
Profiles : DefaultProfiles ,
2020-03-12 21:13:03 +03:00
Configs : DefaultConfigs ,
2016-01-01 04:16:21 +03:00
}
2015-10-18 02:01:43 +03:00
2016-01-01 04:16:21 +03:00
for _ , o := range opts {
o ( & options )
}
2015-10-18 02:01:43 +03:00
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
2020-01-25 13:41:25 +03:00
cmd . app . Action = func ( c * cli . Context ) error {
return nil
}
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 {
2020-06-18 12:14:04 +03:00
// Setup client options
2016-01-02 03:38:57 +03:00
var clientOpts [ ] client . Option
2020-06-18 12:14:04 +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 {
logger . Fatalf ( "failed to parse client_request_timeout: %v" , t )
}
clientOpts = append ( clientOpts , client . RequestTimeout ( d ) )
}
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 {
logger . Fatalf ( "failed to parse client_pool_ttl: %v" , t )
}
clientOpts = append ( clientOpts , client . PoolTTL ( d ) )
}
// Setup server options
var serverOpts [ ] server . Option
metadata := make ( map [ string ] string )
for _ , d := range ctx . StringSlice ( "server_metadata" ) {
var key , val string
parts := strings . Split ( d , "=" )
key = parts [ 0 ]
if len ( parts ) > 1 {
val = strings . Join ( parts [ 1 : ] , "=" )
}
metadata [ key ] = val
}
if len ( metadata ) > 0 {
serverOpts = append ( serverOpts , server . Metadata ( metadata ) )
}
if len ( ctx . String ( "server_name" ) ) > 0 {
serverOpts = append ( serverOpts , server . Name ( ctx . String ( "server_name" ) ) )
}
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 . Int ( "register_ttl" ) ) ; ttl >= 0 {
serverOpts = append ( serverOpts , server . RegisterTTL ( ttl * time . Second ) )
}
if val := time . Duration ( ctx . Int ( "register_interval" ) ) ; val >= 0 {
serverOpts = append ( serverOpts , server . RegisterInterval ( val * time . Second ) )
}
2020-05-22 18:52:24 +03:00
// setup a client to use when calling the runtime. It is important the auth client is wrapped
// after the cache client since the wrappers are applied in reverse order and the cache will use
// some of the headers set by the auth client.
2020-05-14 13:25:19 +03:00
authFn := func ( ) auth . Auth { return * c . opts . Auth }
2020-05-24 20:45:57 +03:00
cacheFn := func ( ) * client . Cache { return ( * c . opts . Client ) . Options ( ) . Cache }
microClient := wrapper . CacheClient ( cacheFn , grpc . NewClient ( ) )
2020-05-22 18:52:24 +03:00
microClient = wrapper . AuthClient ( authFn , microClient )
2020-05-14 13:06:22 +03:00
2020-06-24 15:47:43 +03:00
// Setup auth options
authOpts := [ ] auth . Option { auth . WithClient ( microClient ) }
if len ( ctx . String ( "auth_address" ) ) > 0 {
authOpts = append ( authOpts , auth . Addrs ( ctx . String ( "auth_address" ) ) )
}
if len ( ctx . String ( "auth_id" ) ) > 0 || len ( ctx . String ( "auth_secret" ) ) > 0 {
authOpts = append ( authOpts , auth . Credentials (
ctx . String ( "auth_id" ) , ctx . String ( "auth_secret" ) ,
) )
}
if len ( ctx . String ( "auth_public_key" ) ) > 0 {
authOpts = append ( authOpts , auth . PublicKey ( ctx . String ( "auth_public_key" ) ) )
}
if len ( ctx . String ( "auth_private_key" ) ) > 0 {
authOpts = append ( authOpts , auth . PrivateKey ( ctx . String ( "auth_private_key" ) ) )
}
if ns := ctx . String ( "service_namespace" ) ; len ( ns ) > 0 {
serverOpts = append ( serverOpts , server . Namespace ( ns ) )
authOpts = append ( authOpts , auth . Issuer ( ns ) )
}
// Set the auth
if name := ctx . String ( "auth" ) ; len ( name ) > 0 {
a , ok := c . opts . Auths [ name ]
if ! ok {
logger . Fatalf ( "Unsupported auth: %s" , name )
}
* c . opts . Auth = a ( authOpts ... )
serverOpts = append ( serverOpts , server . Auth ( * c . opts . Auth ) )
} else if len ( authOpts ) > 0 {
( * c . opts . Auth ) . Init ( authOpts ... )
}
2020-07-10 18:25:46 +03:00
// verify the auth's service account
if err := authutil . Verify ( * c . opts . Auth ) ; err != nil {
2020-07-13 14:52:54 +03:00
if logger . V ( logger . DebugLevel , logger . DefaultLogger ) {
logger . Debugf ( "Auth [%v] Error generating auth account: %v" , ( * c . opts . Auth ) , err )
}
2020-06-25 17:24:31 +03:00
}
// Setup broker options.
2020-07-02 19:54:53 +03:00
brokerOpts := [ ] broker . Option { brokerSrv . Client ( microClient ) }
2020-07-19 21:48:11 +03:00
2020-06-25 17:24:31 +03:00
if len ( ctx . String ( "broker_address" ) ) > 0 {
brokerOpts = append ( brokerOpts , broker . Addrs ( ctx . String ( "broker_address" ) ) )
}
2020-07-08 18:22:48 +03:00
// Parse broker TLS certs
2020-07-09 20:02:24 +03:00
if ctx . IsSet ( "broker_tls_cert" ) || ctx . IsSet ( "broker_tls_key" ) {
cert , err := tls . LoadX509KeyPair ( ctx . String ( "broker_tls_cert" ) , ctx . String ( "broker_tls_key" ) )
2020-07-08 18:22:48 +03:00
if err != nil {
2020-07-09 20:02:24 +03:00
logger . Fatalf ( "Error loading broker TLS cert: %v" , err )
}
// load custom certificate authority
caCertPool := x509 . NewCertPool ( )
if ctx . IsSet ( "broker_tls_ca" ) {
crt , err := ioutil . ReadFile ( ctx . String ( "broker_tls_ca" ) )
if err != nil {
logger . Fatalf ( "Error loading broker TLS certificate authority: %v" , err )
}
caCertPool . AppendCertsFromPEM ( crt )
2020-07-08 18:22:48 +03:00
}
cfg := & tls . Config { Certificates : [ ] tls . Certificate { cert } , RootCAs : caCertPool }
brokerOpts = append ( brokerOpts , broker . TLSConfig ( cfg ) )
}
2020-06-25 17:24:31 +03:00
// Setup registry options
registryOpts := [ ] registry . Option { registrySrv . WithClient ( microClient ) }
2020-07-08 10:50:08 +03:00
// Parse registry TLS certs
2020-07-09 20:02:24 +03:00
if ctx . IsSet ( "registry_tls_cert" ) || ctx . IsSet ( "registry_tls_key" ) {
cert , err := tls . LoadX509KeyPair ( ctx . String ( "registry_tls_cert" ) , ctx . String ( "registry_tls_key" ) )
2020-07-08 10:50:08 +03:00
if err != nil {
2020-07-09 20:02:24 +03:00
logger . Fatalf ( "Error loading registry tls cert: %v" , err )
}
// load custom certificate authority
caCertPool := x509 . NewCertPool ( )
if ctx . IsSet ( "registry_tls_ca" ) {
crt , err := ioutil . ReadFile ( ctx . String ( "registry_tls_ca" ) )
if err != nil {
logger . Fatalf ( "Error loading registry tls certificate authority: %v" , err )
}
caCertPool . AppendCertsFromPEM ( crt )
2020-07-08 10:50:08 +03:00
}
cfg := & tls . Config { Certificates : [ ] tls . Certificate { cert } , RootCAs : caCertPool }
registryOpts = append ( registryOpts , registry . TLSConfig ( cfg ) )
}
2020-06-25 17:24:31 +03:00
if len ( ctx . String ( "registry_address" ) ) > 0 {
addresses := strings . Split ( ctx . String ( "registry_address" ) , "," )
registryOpts = append ( registryOpts , registry . Addrs ( addresses ... ) )
}
// Set the registry
if name := ctx . String ( "registry" ) ; len ( name ) > 0 && ( * c . opts . Registry ) . String ( ) != name {
r , ok := c . opts . Registries [ name ]
if ! ok {
logger . Fatalf ( "Registry %s not found" , name )
}
* c . opts . Registry = r ( registryOpts ... )
serverOpts = append ( serverOpts , server . Registry ( * c . opts . Registry ) )
brokerOpts = append ( brokerOpts , broker . Registry ( * c . opts . Registry ) )
} else if len ( registryOpts ) > 0 {
if err := ( * c . opts . Registry ) . Init ( registryOpts ... ) ; err != nil {
logger . Fatalf ( "Error configuring registry: %v" , err )
}
}
2020-07-01 19:06:59 +03:00
// Add support for legacy selectors until v3.
if ctx . String ( "selector" ) == "static" {
ctx . Set ( "router" , "static" )
ctx . Set ( "selector" , "" )
logger . Warnf ( "DEPRECATION WARNING: router/static now provides static routing, use '--router=static'. Support for the static selector flag will be removed in v3." )
}
if ctx . String ( "selector" ) == "dns" {
ctx . Set ( "router" , "dns" )
ctx . Set ( "selector" , "" )
logger . Warnf ( "DEPRECATION WARNING: router/dns now provides dns routing, use '--router=dns'. Support for the dns selector flag will be removed in v3." )
}
2020-06-25 17:24:31 +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 {
logger . Fatalf ( "Selector %s not found" , name )
}
2020-07-01 19:06:59 +03:00
* c . opts . Selector = s ( )
2020-06-25 17:24:31 +03:00
clientOpts = append ( clientOpts , client . Selector ( * c . opts . Selector ) )
}
2020-06-24 13:46:51 +03:00
// Set the router, this must happen before the rest of the server as it'll route server requests
// such as go.micro.config if no address is specified
routerOpts := [ ] router . Option {
2020-06-26 12:38:11 +03:00
srvRouter . Client ( microClient ) ,
2020-06-24 13:46:51 +03:00
router . Network ( ctx . String ( "service_namespace" ) ) ,
router . Registry ( * c . opts . Registry ) ,
2020-06-26 12:38:11 +03:00
router . Id ( ( * c . opts . Server ) . Options ( ) . Id ) ,
}
if len ( ctx . String ( "router_address" ) ) > 0 {
routerOpts = append ( routerOpts , router . Address ( ctx . String ( "router_address" ) ) )
2020-06-24 13:46:51 +03:00
}
if name := ctx . String ( "router" ) ; len ( name ) > 0 && ( * c . opts . Router ) . String ( ) != name {
r , ok := c . opts . Routers [ name ]
if ! ok {
2020-07-12 09:22:08 +03:00
logger . Fatalf ( "Router %s not found" , name )
2020-06-26 12:38:11 +03:00
}
// close the default router before replacing it
if err := ( * c . opts . Router ) . Close ( ) ; err != nil {
logger . Fatalf ( "Error closing default router: %s" , name )
2020-06-24 13:46:51 +03:00
}
* c . opts . Router = r ( routerOpts ... )
2020-07-01 19:06:59 +03:00
clientOpts = append ( clientOpts , client . Router ( * c . opts . Router ) )
2020-06-24 13:46:51 +03:00
} else if len ( routerOpts ) > 0 {
if err := ( * c . opts . Router ) . Init ( routerOpts ... ) ; err != nil {
logger . Fatalf ( "Error configuring router: %v" , err )
}
}
2020-06-18 12:14:04 +03:00
// Setup store options
storeOpts := [ ] store . Option { store . WithClient ( microClient ) }
if len ( ctx . String ( "store_address" ) ) > 0 {
storeOpts = append ( storeOpts , store . Nodes ( strings . Split ( ctx . String ( "store_address" ) , "," ) ... ) )
}
if len ( ctx . String ( "store_database" ) ) > 0 {
storeOpts = append ( storeOpts , store . Database ( ctx . String ( "store_database" ) ) )
2020-07-15 13:31:42 +03:00
} else if len ( ctx . String ( "service_namespace" ) ) > 0 {
storeOpts = append ( storeOpts , store . Database ( ctx . String ( "service_namespace" ) ) )
2020-06-18 12:14:04 +03:00
}
if len ( ctx . String ( "store_table" ) ) > 0 {
storeOpts = append ( storeOpts , store . Table ( ctx . String ( "store_table" ) ) )
}
2020-01-29 18:45:11 +03:00
// Set the store
2020-01-06 20:44:32 +03:00
if name := ctx . String ( "store" ) ; len ( name ) > 0 {
s , ok := c . opts . Stores [ name ]
if ! ok {
2020-06-18 12:14:04 +03:00
logger . Fatalf ( "Unsupported store: %s" , name )
2020-01-06 20:44:32 +03:00
}
2020-06-18 12:14:04 +03:00
* c . opts . Store = s ( storeOpts ... )
} else if len ( storeOpts ) > 0 {
if err := ( * c . opts . Store ) . Init ( storeOpts ... ) ; err != nil {
logger . Fatalf ( "Error configuring store: %v" , err )
}
}
// Setup the runtime options
runtimeOpts := [ ] runtime . Option { runtime . WithClient ( microClient ) }
if len ( ctx . String ( "runtime_source" ) ) > 0 {
runtimeOpts = append ( runtimeOpts , runtime . WithSource ( ctx . String ( "runtime_source" ) ) )
2020-01-06 20:44:32 +03:00
}
2019-11-02 16:25:10 +03:00
// Set the runtime
if name := ctx . String ( "runtime" ) ; len ( name ) > 0 {
r , ok := c . opts . Runtimes [ name ]
if ! ok {
2020-06-18 12:14:04 +03:00
logger . Fatalf ( "Unsupported runtime: %s" , name )
2019-11-02 16:25:10 +03:00
}
2020-06-18 12:14:04 +03:00
* c . opts . Runtime = r ( runtimeOpts ... )
} else if len ( runtimeOpts ) > 0 {
if err := ( * c . opts . Runtime ) . Init ( runtimeOpts ... ) ; err != nil {
logger . Fatalf ( "Error configuring runtime: %v" , err )
}
2019-11-02 16:25:10 +03:00
}
2020-01-29 18:45:11 +03:00
// Set the tracer
if name := ctx . String ( "tracer" ) ; len ( name ) > 0 {
r , ok := c . opts . Tracers [ name ]
if ! ok {
2020-06-18 12:14:04 +03:00
logger . Fatalf ( "Unsupported tracer: %s" , name )
2020-01-29 18:45:11 +03:00
}
* c . opts . Tracer = r ( )
}
2020-02-25 19:42:43 +03:00
// Set the profile
if name := ctx . String ( "profile" ) ; len ( name ) > 0 {
p , ok := c . opts . Profiles [ name ]
if ! ok {
2020-06-18 12:14:04 +03:00
logger . Fatalf ( "Unsupported profile: %s" , name )
2020-02-25 19:42:43 +03:00
}
* c . opts . Profile = p ( )
}
2016-01-02 03:38:57 +03:00
// Set the broker
2018-08-08 20:57:29 +03:00
if name := ctx . String ( "broker" ) ; len ( name ) > 0 && ( * c . opts . Broker ) . String ( ) != name {
b , ok := c . opts . Brokers [ name ]
if ! ok {
2020-06-18 12:14:04 +03:00
logger . Fatalf ( "Broker %s not found" , name )
2016-01-02 03:38:57 +03:00
}
2020-06-18 12:14:04 +03:00
* c . opts . Broker = b ( brokerOpts ... )
2016-01-02 03:38:57 +03:00
serverOpts = append ( serverOpts , server . Broker ( * c . opts . Broker ) )
clientOpts = append ( clientOpts , client . Broker ( * c . opts . Broker ) )
2020-06-18 12:14:04 +03:00
} else if len ( brokerOpts ) > 0 {
if err := ( * c . opts . Broker ) . Init ( brokerOpts ... ) ; err != nil {
logger . Fatalf ( "Error configuring broker: %v" , err )
}
2015-02-15 02:33:21 +03:00
}
2020-06-18 12:14:04 +03:00
// Setup the transport options
var transportOpts [ ] transport . Option
if len ( ctx . String ( "transport_address" ) ) > 0 {
addresses := strings . Split ( ctx . String ( "transport_address" ) , "," )
transportOpts = append ( transportOpts , transport . Addrs ( addresses ... ) )
2015-12-09 22:32:10 +03:00
}
2016-01-02 03:38:57 +03:00
// Set the transport
2018-08-08 20:57:29 +03:00
if name := ctx . String ( "transport" ) ; len ( name ) > 0 && ( * c . opts . Transport ) . String ( ) != name {
t , ok := c . opts . Transports [ name ]
if ! ok {
2020-06-18 12:14:04 +03:00
logger . Fatalf ( "Transport %s not found" , name )
2016-01-02 03:38:57 +03:00
}
2020-06-18 12:14:04 +03:00
* c . opts . Transport = t ( transportOpts ... )
2016-01-02 03:38:57 +03:00
serverOpts = append ( serverOpts , server . Transport ( * c . opts . Transport ) )
clientOpts = append ( clientOpts , client . Transport ( * c . opts . Transport ) )
2020-06-18 12:14:04 +03:00
} else if len ( transportOpts ) > 0 {
if err := ( * c . opts . Transport ) . Init ( transportOpts ... ) ; err != nil {
2020-03-11 20:55:39 +03:00
logger . Fatalf ( "Error configuring transport: %v" , err )
2018-12-06 21:19:05 +03:00
}
2018-08-08 20:57:29 +03:00
}
2020-06-18 12:14:04 +03:00
// Setup config sources
2020-03-12 21:13:03 +03:00
if ctx . String ( "config" ) == "service" {
2020-07-02 16:07:58 +03:00
opt := config . WithSource ( configSrv . NewSource (
configSrc . WithClient ( microClient ) ,
configSrv . Namespace ( ctx . String ( "service_namespace" ) ) ,
) )
2020-03-12 21:13:03 +03:00
if err := ( * c . opts . Config ) . Init ( opt ) ; err != nil {
2020-07-13 17:08:53 +03:00
if logger . V ( logger . DebugLevel , logger . DefaultLogger ) {
logger . Debugf ( "Error configuring config: %v" , err )
}
2020-03-12 21:13:03 +03:00
}
}
2020-06-18 12:14:04 +03:00
// Set the client
if name := ctx . String ( "client" ) ; len ( name ) > 0 && ( * c . opts . Client ) . String ( ) != name {
cl , ok := c . opts . Clients [ name ]
if ! ok {
logger . Fatalf ( "Client %s not found" , name )
2016-03-29 19:18:39 +03:00
}
2020-06-18 12:14:04 +03:00
* c . opts . Client = cl ( clientOpts ... )
} else if len ( clientOpts ) > 0 {
if err := ( * c . opts . Client ) . Init ( clientOpts ... ) ; err != nil {
logger . Fatalf ( "Error configuring client: %v" , err )
}
2016-06-07 02:46:14 +03:00
}
2020-06-18 12:14:04 +03:00
// Set the server
if name := ctx . String ( "server" ) ; len ( name ) > 0 && ( * c . opts . Server ) . String ( ) != name {
s , ok := c . opts . Servers [ name ]
if ! ok {
logger . Fatalf ( "Server %s not found" , name )
2016-06-07 02:46:14 +03:00
}
2020-06-18 12:14:04 +03:00
* c . opts . Server = s ( serverOpts ... )
} else if len ( serverOpts ) > 0 {
2018-12-06 21:19:05 +03:00
if err := ( * c . opts . Server ) . Init ( serverOpts ... ) ; err != nil {
2020-03-11 20:55:39 +03:00
logger . Fatalf ( "Error configuring server: %v" , err )
2018-12-06 21:19:05 +03:00
}
2016-01-02 03:38:57 +03:00
}
2015-04-28 00:21:56 +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 )
2015-04-28 00:21:56 +03:00
}
2020-01-19 03:55:01 +03: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 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
2016-01-02 22:12:17 +03:00
return nil
2016-01-01 04:16:21 +03:00
}
2015-04-28 00:21:56 +03:00
2020-07-19 20:14:18 +03:00
func ( c * cmd ) Run ( ) error {
return c . app . Run ( os . Args )
}
2020-07-19 19:20:05 +03:00
func ( c * cmd ) String ( ) string {
return "micro/cli"
}
2016-01-04 14:25:58 +03:00
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 ... )
2015-02-15 02:33:21 +03:00
}
2020-07-19 20:43:33 +03:00
// Run the default command
func Run ( ) error {
return DefaultCmd . Run ( )
}