initial commit
This commit is contained in:
52
internal/metal/providers/emgql/options.go
Normal file
52
internal/metal/providers/emgql/options.go
Normal file
@@ -0,0 +1,52 @@
|
||||
package emgql
|
||||
|
||||
import (
|
||||
"net/url"
|
||||
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
// Option is a Client configuration Option definition.
|
||||
type Option func(c *Client) error
|
||||
|
||||
// WithLogger sets the logger for the client.
|
||||
func WithLogger(logger *zap.SugaredLogger) Option {
|
||||
return func(c *Client) error {
|
||||
c.logger = logger
|
||||
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
// WithBaseURL updates the baseurl used by the client.
|
||||
func WithBaseURL(baseURL string) Option {
|
||||
return func(c *Client) error {
|
||||
u, err := url.Parse(baseURL)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
c.baseURL = u
|
||||
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
// WithConfig applies all configurations defined in the config.
|
||||
func WithConfig(config Config) Option {
|
||||
return func(c *Client) error {
|
||||
var options []Option
|
||||
|
||||
if config.BaseURL != "" {
|
||||
options = append(options, WithBaseURL(config.BaseURL))
|
||||
}
|
||||
|
||||
for _, opt := range options {
|
||||
if err := opt(c); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user