initial commit
This commit is contained in:
73
internal/permissions/config.go
Normal file
73
internal/permissions/config.go
Normal file
@@ -0,0 +1,73 @@
|
||||
package permissions
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/url"
|
||||
|
||||
"github.com/spf13/pflag"
|
||||
"github.com/spf13/viper"
|
||||
"go.infratographer.com/x/viperx"
|
||||
)
|
||||
|
||||
// Config provides configuration for connecting to the Equinix Metal API provider.
|
||||
type Config struct {
|
||||
// BaseURL is the baseurl to use when connecting to the Equinix Metal API Provider.
|
||||
BaseURL string
|
||||
|
||||
// BearerToken is the token to interact with the Equinix Metal API
|
||||
BearerToken string
|
||||
}
|
||||
|
||||
func MustViperFlags(v *viper.Viper, flags *pflag.FlagSet) {
|
||||
flags.String("permissions-baseurl", "", "permissions base url")
|
||||
viperx.MustBindFlag(v, "permissions.baseurl", flags.Lookup("permissions-baseurl"))
|
||||
|
||||
flags.String("permissions-token", "", "permissions bearer url")
|
||||
viperx.MustBindFlag(v, "permissions.bearertoken", flags.Lookup("permissions-token"))
|
||||
}
|
||||
|
||||
// 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))
|
||||
}
|
||||
|
||||
if config.BearerToken != "" {
|
||||
options = append(options, WithBearerToken(config.BearerToken))
|
||||
}
|
||||
|
||||
for _, opt := range options {
|
||||
if err := opt(c); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
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 fmt.Errorf("failed to parse emapi base url %s: %w", baseURL, err)
|
||||
}
|
||||
|
||||
c.baseURL = u
|
||||
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
// WithBearerToken sets the bearer token to authenticate the request with.
|
||||
func WithBearerToken(token string) Option {
|
||||
return func(c *Client) error {
|
||||
c.token = token
|
||||
|
||||
return nil
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user