1
0
mirror of https://github.com/go-micro/go-micro.git synced 2024-12-30 10:10:44 +02:00
go-micro/runtime/options.go

310 lines
6.0 KiB
Go
Raw Normal View History

2019-09-24 19:32:35 +02:00
package runtime
2019-09-24 20:00:11 +02:00
import (
"context"
2019-09-24 20:00:11 +02:00
"io"
2020-05-11 18:09:28 +02:00
2024-06-04 22:40:43 +02:00
"go-micro.dev/v5/client"
"go-micro.dev/v5/logger"
2019-09-24 20:00:11 +02:00
)
type Option func(o *Options)
2022-09-30 16:27:07 +02:00
// Options configure runtime.
type Options struct {
2020-01-16 15:34:04 +02:00
// Scheduler for updates
Scheduler Scheduler
2023-04-26 02:16:34 +02:00
// Client to use when making requests
Client client.Client
// Logger underline logger
Logger logger.Logger
2019-11-29 13:35:00 +02:00
// Service type to manage
Type string
// Source of the services repository
Source string
// Base image to use
Image string
}
func NewOptions(opts ...Option) *Options {
options := &Options{
Logger: logger.DefaultLogger,
}
for _, o := range opts {
o(options)
}
return options
}
2022-09-30 16:27:07 +02:00
// WithSource sets the base image / repository.
func WithSource(src string) Option {
return func(o *Options) {
o.Source = src
}
}
2022-09-30 16:27:07 +02:00
// WithScheduler specifies a scheduler for updates.
2020-01-16 15:34:04 +02:00
func WithScheduler(n Scheduler) Option {
return func(o *Options) {
2020-01-16 15:34:04 +02:00
o.Scheduler = n
}
}
2022-09-30 16:27:07 +02:00
// WithType sets the service type to manage.
2019-11-29 13:35:00 +02:00
func WithType(t string) Option {
return func(o *Options) {
o.Type = t
}
}
2022-09-30 16:27:07 +02:00
// WithImage sets the image to use.
func WithImage(t string) Option {
return func(o *Options) {
o.Image = t
}
}
2022-09-30 16:27:07 +02:00
// WithClient sets the client to use.
2020-05-11 18:09:28 +02:00
func WithClient(c client.Client) Option {
return func(o *Options) {
o.Client = c
}
}
2022-09-30 16:27:07 +02:00
// WithLogger sets the underline logger.
func WithLogger(l logger.Logger) Option {
return func(o *Options) {
o.Logger = l
}
}
2019-09-24 19:32:35 +02:00
type CreateOption func(o *CreateOptions)
2019-11-29 13:35:00 +02:00
type ReadOption func(o *ReadOptions)
2022-09-30 16:27:07 +02:00
// CreateOptions configure runtime services.
2019-09-24 19:32:35 +02:00
type CreateOptions struct {
2019-09-24 20:00:11 +02:00
// Log output
Output io.Writer
2023-04-26 02:16:34 +02:00
// Specify the context to use
Context context.Context
2019-11-29 13:35:00 +02:00
// Type of service to create
Type string
// Specify the image to use
Image string
// Namespace to create the service in
Namespace string
2023-04-26 02:16:34 +02:00
// Command to execut
Command []string
// Args to pass into command
Args []string
// Environment to configure
Env []string
// Retries before failing deploy
Retries int
2019-11-29 13:35:00 +02:00
}
2022-09-30 16:27:07 +02:00
// ReadOptions queries runtime services.
2019-11-29 13:35:00 +02:00
type ReadOptions struct {
2023-04-26 02:16:34 +02:00
// Specify the context to use
Context context.Context
2019-11-29 13:35:00 +02:00
// Service name
Service string
// Version queries services with given version
Version string
// Type of service
Type string
// Namespace the service is running in
Namespace string
2019-09-24 19:32:35 +02:00
}
2022-09-30 16:27:07 +02:00
// CreateType sets the type of service to create.
2020-01-17 16:14:47 +02:00
func CreateType(t string) CreateOption {
return func(o *CreateOptions) {
o.Type = t
}
}
2022-09-30 16:27:07 +02:00
// CreateImage sets the image to use.
func CreateImage(img string) CreateOption {
return func(o *CreateOptions) {
o.Image = img
}
}
2022-09-30 16:27:07 +02:00
// CreateNamespace sets the namespace.
func CreateNamespace(ns string) CreateOption {
return func(o *CreateOptions) {
o.Namespace = ns
}
}
2022-09-30 16:27:07 +02:00
// CreateContext sets the context.
func CreateContext(ctx context.Context) CreateOption {
return func(o *CreateOptions) {
o.Context = ctx
2020-04-23 13:27:36 +02:00
}
}
2022-09-30 16:27:07 +02:00
// WithCommand specifies the command to execute.
func WithCommand(cmd ...string) CreateOption {
return func(o *CreateOptions) {
// set command
o.Command = cmd
}
}
2022-09-30 16:27:07 +02:00
// WithArgs specifies the command to execute.
func WithArgs(args ...string) CreateOption {
2019-09-24 19:32:35 +02:00
return func(o *CreateOptions) {
// set command
o.Args = args
2019-09-24 19:32:35 +02:00
}
}
func WithRetries(retries int) CreateOption {
return func(o *CreateOptions) {
o.Retries = retries
}
}
2022-09-30 16:27:07 +02:00
// WithEnv sets the created service environment.
2019-09-24 19:32:35 +02:00
func WithEnv(env []string) CreateOption {
return func(o *CreateOptions) {
o.Env = env
}
}
2019-09-24 20:00:11 +02:00
2022-09-30 16:27:07 +02:00
// WithOutput sets the arg output.
2019-09-24 20:00:11 +02:00
func WithOutput(out io.Writer) CreateOption {
return func(o *CreateOptions) {
o.Output = out
}
}
2022-09-30 16:27:07 +02:00
// ReadService returns services with the given name.
2019-11-29 13:35:00 +02:00
func ReadService(service string) ReadOption {
return func(o *ReadOptions) {
o.Service = service
}
}
2022-09-30 16:27:07 +02:00
// ReadVersion confifgures service version.
2019-11-29 13:35:00 +02:00
func ReadVersion(version string) ReadOption {
return func(o *ReadOptions) {
o.Version = version
}
}
2019-11-29 13:35:00 +02:00
2022-09-30 16:27:07 +02:00
// ReadType returns services of the given type.
2019-11-29 13:35:00 +02:00
func ReadType(t string) ReadOption {
return func(o *ReadOptions) {
o.Type = t
}
}
2022-09-30 16:27:07 +02:00
// ReadNamespace sets the namespace.
func ReadNamespace(ns string) ReadOption {
return func(o *ReadOptions) {
o.Namespace = ns
}
}
2022-09-30 16:27:07 +02:00
// ReadContext sets the context.
func ReadContext(ctx context.Context) ReadOption {
return func(o *ReadOptions) {
o.Context = ctx
}
}
type UpdateOption func(o *UpdateOptions)
type UpdateOptions struct {
// Specify the context to use
Context context.Context
2023-04-26 02:16:34 +02:00
// Namespace the service is running in
Namespace string
}
2022-09-30 16:27:07 +02:00
// UpdateNamespace sets the namespace.
func UpdateNamespace(ns string) UpdateOption {
return func(o *UpdateOptions) {
o.Namespace = ns
}
}
2022-09-30 16:27:07 +02:00
// UpdateContext sets the context.
func UpdateContext(ctx context.Context) UpdateOption {
return func(o *UpdateOptions) {
o.Context = ctx
}
}
type DeleteOption func(o *DeleteOptions)
type DeleteOptions struct {
// Specify the context to use
Context context.Context
2023-04-26 02:16:34 +02:00
// Namespace the service is running in
Namespace string
}
2022-09-30 16:27:07 +02:00
// DeleteNamespace sets the namespace.
func DeleteNamespace(ns string) DeleteOption {
return func(o *DeleteOptions) {
o.Namespace = ns
}
}
2022-09-30 16:27:07 +02:00
// DeleteContext sets the context.
func DeleteContext(ctx context.Context) DeleteOption {
return func(o *DeleteOptions) {
o.Context = ctx
}
}
2022-09-30 16:27:07 +02:00
// LogsOption configures runtime logging.
type LogsOption func(o *LogsOptions)
2022-09-30 16:27:07 +02:00
// LogsOptions configure runtime logging.
type LogsOptions struct {
2023-04-26 02:16:34 +02:00
// Specify the context to use
Context context.Context
// Namespace the service is running in
Namespace string
// How many existing lines to show
Count int64
// Stream new lines?
Stream bool
}
2022-09-30 16:27:07 +02:00
// LogsExistingCount confiures how many existing lines to show.
func LogsCount(count int64) LogsOption {
return func(l *LogsOptions) {
l.Count = count
}
}
2022-09-30 16:27:07 +02:00
// LogsStream configures whether to stream new lines.
func LogsStream(stream bool) LogsOption {
return func(l *LogsOptions) {
l.Stream = stream
}
}
2022-09-30 16:27:07 +02:00
// LogsNamespace sets the namespace.
func LogsNamespace(ns string) LogsOption {
return func(o *LogsOptions) {
o.Namespace = ns
}
}
2022-09-30 16:27:07 +02:00
// LogsContext sets the context.
func LogsContext(ctx context.Context) LogsOption {
return func(o *LogsOptions) {
o.Context = ctx
}
}