github.com/annwntech/go-micro/v2@v2.9.5/runtime/options.go (about)

     1  package runtime
     2  
     3  import (
     4  	"context"
     5  	"io"
     6  
     7  	"github.com/annwntech/go-micro/v2/client"
     8  )
     9  
    10  type Option func(o *Options)
    11  
    12  // Options configure runtime
    13  type Options struct {
    14  	// Scheduler for updates
    15  	Scheduler Scheduler
    16  	// Service type to manage
    17  	Type string
    18  	// Source of the services repository
    19  	Source string
    20  	// Base image to use
    21  	Image string
    22  	// Client to use when making requests
    23  	Client client.Client
    24  }
    25  
    26  // WithSource sets the base image / repository
    27  func WithSource(src string) Option {
    28  	return func(o *Options) {
    29  		o.Source = src
    30  	}
    31  }
    32  
    33  // WithScheduler specifies a scheduler for updates
    34  func WithScheduler(n Scheduler) Option {
    35  	return func(o *Options) {
    36  		o.Scheduler = n
    37  	}
    38  }
    39  
    40  // WithType sets the service type to manage
    41  func WithType(t string) Option {
    42  	return func(o *Options) {
    43  		o.Type = t
    44  	}
    45  }
    46  
    47  // WithImage sets the image to use
    48  func WithImage(t string) Option {
    49  	return func(o *Options) {
    50  		o.Image = t
    51  	}
    52  }
    53  
    54  // WithClient sets the client to use
    55  func WithClient(c client.Client) Option {
    56  	return func(o *Options) {
    57  		o.Client = c
    58  	}
    59  }
    60  
    61  type CreateOption func(o *CreateOptions)
    62  
    63  type ReadOption func(o *ReadOptions)
    64  
    65  // CreateOptions configure runtime services
    66  type CreateOptions struct {
    67  	// Command to execut
    68  	Command []string
    69  	// Args to pass into command
    70  	Args []string
    71  	// Environment to configure
    72  	Env []string
    73  	// Log output
    74  	Output io.Writer
    75  	// Type of service to create
    76  	Type string
    77  	// Retries before failing deploy
    78  	Retries int
    79  	// Specify the image to use
    80  	Image string
    81  	// Namespace to create the service in
    82  	Namespace string
    83  	// Specify the context to use
    84  	Context context.Context
    85  }
    86  
    87  // ReadOptions queries runtime services
    88  type ReadOptions struct {
    89  	// Service name
    90  	Service string
    91  	// Version queries services with given version
    92  	Version string
    93  	// Type of service
    94  	Type string
    95  	// Namespace the service is running in
    96  	Namespace string
    97  	// Specify the context to use
    98  	Context context.Context
    99  }
   100  
   101  // CreateType sets the type of service to create
   102  func CreateType(t string) CreateOption {
   103  	return func(o *CreateOptions) {
   104  		o.Type = t
   105  	}
   106  }
   107  
   108  // CreateImage sets the image to use
   109  func CreateImage(img string) CreateOption {
   110  	return func(o *CreateOptions) {
   111  		o.Image = img
   112  	}
   113  }
   114  
   115  // CreateNamespace sets the namespace
   116  func CreateNamespace(ns string) CreateOption {
   117  	return func(o *CreateOptions) {
   118  		o.Namespace = ns
   119  	}
   120  }
   121  
   122  // CreateContext sets the context
   123  func CreateContext(ctx context.Context) CreateOption {
   124  	return func(o *CreateOptions) {
   125  		o.Context = ctx
   126  	}
   127  }
   128  
   129  // WithCommand specifies the command to execute
   130  func WithCommand(cmd ...string) CreateOption {
   131  	return func(o *CreateOptions) {
   132  		// set command
   133  		o.Command = cmd
   134  	}
   135  }
   136  
   137  // WithArgs specifies the command to execute
   138  func WithArgs(args ...string) CreateOption {
   139  	return func(o *CreateOptions) {
   140  		// set command
   141  		o.Args = args
   142  	}
   143  }
   144  
   145  // WithRetries sets the max retries attemps
   146  func WithRetries(retries int) CreateOption {
   147  	return func(o *CreateOptions) {
   148  		o.Retries = retries
   149  	}
   150  }
   151  
   152  // WithEnv sets the created service environment
   153  func WithEnv(env []string) CreateOption {
   154  	return func(o *CreateOptions) {
   155  		o.Env = env
   156  	}
   157  }
   158  
   159  // WithOutput sets the arg output
   160  func WithOutput(out io.Writer) CreateOption {
   161  	return func(o *CreateOptions) {
   162  		o.Output = out
   163  	}
   164  }
   165  
   166  // ReadService returns services with the given name
   167  func ReadService(service string) ReadOption {
   168  	return func(o *ReadOptions) {
   169  		o.Service = service
   170  	}
   171  }
   172  
   173  // ReadVersion confifgures service version
   174  func ReadVersion(version string) ReadOption {
   175  	return func(o *ReadOptions) {
   176  		o.Version = version
   177  	}
   178  }
   179  
   180  // ReadType returns services of the given type
   181  func ReadType(t string) ReadOption {
   182  	return func(o *ReadOptions) {
   183  		o.Type = t
   184  	}
   185  }
   186  
   187  // ReadNamespace sets the namespace
   188  func ReadNamespace(ns string) ReadOption {
   189  	return func(o *ReadOptions) {
   190  		o.Namespace = ns
   191  	}
   192  }
   193  
   194  // ReadContext sets the context
   195  func ReadContext(ctx context.Context) ReadOption {
   196  	return func(o *ReadOptions) {
   197  		o.Context = ctx
   198  	}
   199  }
   200  
   201  type UpdateOption func(o *UpdateOptions)
   202  
   203  type UpdateOptions struct {
   204  	// Namespace the service is running in
   205  	Namespace string
   206  	// Specify the context to use
   207  	Context context.Context
   208  }
   209  
   210  // UpdateNamespace sets the namespace
   211  func UpdateNamespace(ns string) UpdateOption {
   212  	return func(o *UpdateOptions) {
   213  		o.Namespace = ns
   214  	}
   215  }
   216  
   217  // UpdateContext sets the context
   218  func UpdateContext(ctx context.Context) UpdateOption {
   219  	return func(o *UpdateOptions) {
   220  		o.Context = ctx
   221  	}
   222  }
   223  
   224  type DeleteOption func(o *DeleteOptions)
   225  
   226  type DeleteOptions struct {
   227  	// Namespace the service is running in
   228  	Namespace string
   229  	// Specify the context to use
   230  	Context context.Context
   231  }
   232  
   233  // DeleteNamespace sets the namespace
   234  func DeleteNamespace(ns string) DeleteOption {
   235  	return func(o *DeleteOptions) {
   236  		o.Namespace = ns
   237  	}
   238  }
   239  
   240  // DeleteContext sets the context
   241  func DeleteContext(ctx context.Context) DeleteOption {
   242  	return func(o *DeleteOptions) {
   243  		o.Context = ctx
   244  	}
   245  }
   246  
   247  // LogsOption configures runtime logging
   248  type LogsOption func(o *LogsOptions)
   249  
   250  // LogsOptions configure runtime logging
   251  type LogsOptions struct {
   252  	// How many existing lines to show
   253  	Count int64
   254  	// Stream new lines?
   255  	Stream bool
   256  	// Namespace the service is running in
   257  	Namespace string
   258  	// Specify the context to use
   259  	Context context.Context
   260  }
   261  
   262  // LogsExistingCount confiures how many existing lines to show
   263  func LogsCount(count int64) LogsOption {
   264  	return func(l *LogsOptions) {
   265  		l.Count = count
   266  	}
   267  }
   268  
   269  // LogsStream configures whether to stream new lines
   270  func LogsStream(stream bool) LogsOption {
   271  	return func(l *LogsOptions) {
   272  		l.Stream = stream
   273  	}
   274  }
   275  
   276  // LogsNamespace sets the namespace
   277  func LogsNamespace(ns string) LogsOption {
   278  	return func(o *LogsOptions) {
   279  		o.Namespace = ns
   280  	}
   281  }
   282  
   283  // LogsContext sets the context
   284  func LogsContext(ctx context.Context) LogsOption {
   285  	return func(o *LogsOptions) {
   286  		o.Context = ctx
   287  	}
   288  }