gitee.com/liuxuezhan/go-micro-v1.18.0@v1.0.0/config/cmd/options.go (about)

     1  package cmd
     2  
     3  import (
     4  	"context"
     5  
     6  	"gitee.com/liuxuezhan/go-micro-v1.18.0/broker"
     7  	"gitee.com/liuxuezhan/go-micro-v1.18.0/client"
     8  	"gitee.com/liuxuezhan/go-micro-v1.18.0/client/selector"
     9  	"gitee.com/liuxuezhan/go-micro-v1.18.0/registry"
    10  	"gitee.com/liuxuezhan/go-micro-v1.18.0/runtime"
    11  	"gitee.com/liuxuezhan/go-micro-v1.18.0/server"
    12  	"gitee.com/liuxuezhan/go-micro-v1.18.0/transport"
    13  )
    14  
    15  type Options struct {
    16  	// For the Command Line itself
    17  	Name        string
    18  	Description string
    19  	Version     string
    20  
    21  	// We need pointers to things so we can swap them out if needed.
    22  	Broker    *broker.Broker
    23  	Registry  *registry.Registry
    24  	Selector  *selector.Selector
    25  	Transport *transport.Transport
    26  	Client    *client.Client
    27  	Server    *server.Server
    28  	Runtime   *runtime.Runtime
    29  
    30  	Brokers    map[string]func(...broker.Option) broker.Broker
    31  	Clients    map[string]func(...client.Option) client.Client
    32  	Registries map[string]func(...registry.Option) registry.Registry
    33  	Selectors  map[string]func(...selector.Option) selector.Selector
    34  	Servers    map[string]func(...server.Option) server.Server
    35  	Transports map[string]func(...transport.Option) transport.Transport
    36  	Runtimes   map[string]func(...runtime.Option) runtime.Runtime
    37  
    38  	// Other options for implementations of the interface
    39  	// can be stored in a context
    40  	Context context.Context
    41  }
    42  
    43  // Command line Name
    44  func Name(n string) Option {
    45  	return func(o *Options) {
    46  		o.Name = n
    47  	}
    48  }
    49  
    50  // Command line Description
    51  func Description(d string) Option {
    52  	return func(o *Options) {
    53  		o.Description = d
    54  	}
    55  }
    56  
    57  // Command line Version
    58  func Version(v string) Option {
    59  	return func(o *Options) {
    60  		o.Version = v
    61  	}
    62  }
    63  
    64  func Broker(b *broker.Broker) Option {
    65  	return func(o *Options) {
    66  		o.Broker = b
    67  	}
    68  }
    69  
    70  func Selector(s *selector.Selector) Option {
    71  	return func(o *Options) {
    72  		o.Selector = s
    73  	}
    74  }
    75  
    76  func Registry(r *registry.Registry) Option {
    77  	return func(o *Options) {
    78  		o.Registry = r
    79  	}
    80  }
    81  
    82  func Transport(t *transport.Transport) Option {
    83  	return func(o *Options) {
    84  		o.Transport = t
    85  	}
    86  }
    87  
    88  func Client(c *client.Client) Option {
    89  	return func(o *Options) {
    90  		o.Client = c
    91  	}
    92  }
    93  
    94  func Server(s *server.Server) Option {
    95  	return func(o *Options) {
    96  		o.Server = s
    97  	}
    98  }
    99  
   100  // New broker func
   101  func NewBroker(name string, b func(...broker.Option) broker.Broker) Option {
   102  	return func(o *Options) {
   103  		o.Brokers[name] = b
   104  	}
   105  }
   106  
   107  // New client func
   108  func NewClient(name string, b func(...client.Option) client.Client) Option {
   109  	return func(o *Options) {
   110  		o.Clients[name] = b
   111  	}
   112  }
   113  
   114  // New registry func
   115  func NewRegistry(name string, r func(...registry.Option) registry.Registry) Option {
   116  	return func(o *Options) {
   117  		o.Registries[name] = r
   118  	}
   119  }
   120  
   121  // New selector func
   122  func NewSelector(name string, s func(...selector.Option) selector.Selector) Option {
   123  	return func(o *Options) {
   124  		o.Selectors[name] = s
   125  	}
   126  }
   127  
   128  // New server func
   129  func NewServer(name string, s func(...server.Option) server.Server) Option {
   130  	return func(o *Options) {
   131  		o.Servers[name] = s
   132  	}
   133  }
   134  
   135  // New transport func
   136  func NewTransport(name string, t func(...transport.Option) transport.Transport) Option {
   137  	return func(o *Options) {
   138  		o.Transports[name] = t
   139  	}
   140  }
   141  
   142  // New runtime func
   143  func NewRuntime(name string, r func(...runtime.Option) runtime.Runtime) Option {
   144  	return func(o *Options) {
   145  		o.Runtimes[name] = r
   146  	}
   147  }