[WIP] v0.7.0 added functional options to configfetcher
Some checks failed
ci/woodpecker/push/go-lint Pipeline failed

This commit is contained in:
2025-01-14 19:13:05 -06:00
parent 5c2bfcc940
commit e88773e289
8 changed files with 203 additions and 56 deletions

View File

@ -47,7 +47,11 @@ func (opts *ConfigOpts) InitConfig() {
opts.ConfigFilePath = strings.TrimSpace(opts.ConfigFilePath)
// Initialize the fetcher
fetcher := configfetcher.NewConfigFetcher(opts.ConfigFilePath)
fetcher, err := configfetcher.NewConfigFetcher(opts.ConfigFilePath)
if err != nil {
logging.ExitWithMSG(fmt.Sprintf("error initializing config fetcher: %v", err), 1, nil)
}
if opts.ConfigFilePath != "" {
loadConfigFile(fetcher, opts.ConfigFilePath, backyKoanf, opts)
@ -254,7 +258,10 @@ func loadCommandLists(opts *ConfigOpts, backyKoanf *koanf.Koanf) {
}
func loadListConfigFile(filePath string, k *koanf.Koanf, opts *ConfigOpts) bool {
fetcher := configfetcher.NewConfigFetcher(filePath)
fetcher, err := configfetcher.NewConfigFetcher(filePath)
if err != nil {
logging.ExitWithMSG(fmt.Sprintf("error initializing config fetcher: %v", err), 1, nil)
}
data, err := fetcher.Fetch(filePath)
if err != nil {
@ -275,7 +282,10 @@ func loadCmdListsFile(backyKoanf *koanf.Koanf, listsConfig *koanf.Koanf, opts *C
opts.CmdListFile = path.Join(path.Dir(opts.ConfigFilePath), opts.CmdListFile)
}
fetcher := configfetcher.NewConfigFetcher(opts.CmdListFile)
fetcher, err := configfetcher.NewConfigFetcher(opts.CmdListFile)
if err != nil {
logging.ExitWithMSG(fmt.Sprintf("error initializing config fetcher: %v", err), 1, nil)
}
data, err := fetcher.Fetch(opts.CmdListFile)
if err != nil {

View File

@ -12,13 +12,16 @@ type ConfigFetcher interface {
Parse(data []byte, target interface{}) error
}
func NewConfigFetcher(source string) ConfigFetcher {
func NewConfigFetcher(source string, options ...Option) (ConfigFetcher, error) {
if strings.HasPrefix(source, "http") || strings.HasPrefix(source, "https") {
return &HTTPFetcher{}
return NewHTTPFetcher(options...), nil
} else if strings.HasPrefix(source, "s3") {
return &S3Fetcher{}
fetcher, err := NewS3Fetcher(options...)
if err != nil {
return nil, err
}
return fetcher, nil
} else {
return &LocalFetcher{}
return &LocalFetcher{}, nil
}
}

View File

@ -8,7 +8,24 @@ import (
"gopkg.in/yaml.v3"
)
type HTTPFetcher struct{}
type HTTPFetcher struct {
HTTPClient *http.Client
}
// NewHTTPFetcher creates a new instance of HTTPFetcher with the provided options.
func NewHTTPFetcher(options ...Option) *HTTPFetcher {
cfg := &FetcherConfig{}
for _, opt := range options {
opt(cfg)
}
// Initialize HTTP client if not provided
if cfg.HTTPClient == nil {
cfg.HTTPClient = http.DefaultClient
}
return &HTTPFetcher{HTTPClient: cfg.HTTPClient}
}
// Fetch retrieves the configuration from the specified URL
func (h *HTTPFetcher) Fetch(source string) ([]byte, error) {

View File

@ -0,0 +1,30 @@
package configfetcher
import (
"net/http"
"github.com/aws/aws-sdk-go-v2/service/s3"
)
// Option is a function that configures a fetcher.
type Option func(*FetcherConfig)
// FetcherConfig holds the configuration for a fetcher.
type FetcherConfig struct {
S3Client *s3.Client
HTTPClient *http.Client
}
// WithS3Client sets the S3 client for the fetcher.
func WithS3Client(client *s3.Client) Option {
return func(cfg *FetcherConfig) {
cfg.S3Client = client
}
}
// WithHTTPClient sets the HTTP client for the fetcher.
func WithHTTPClient(client *http.Client) Option {
return func(cfg *FetcherConfig) {
cfg.HTTPClient = client
}
}

View File

@ -15,14 +15,23 @@ type S3Fetcher struct {
S3Client *s3.Client
}
// NewS3Fetcher creates a new instance of S3Fetcher with an initialized S3 client
func NewS3Fetcher() (*S3Fetcher, error) {
cfg, err := config.LoadDefaultConfig(context.TODO())
if err != nil {
return nil, err
// NewS3Fetcher creates a new instance of S3Fetcher with the provided options.
func NewS3Fetcher(options ...Option) (*S3Fetcher, error) {
cfg := &FetcherConfig{}
for _, opt := range options {
opt(cfg)
}
client := s3.NewFromConfig(cfg)
return &S3Fetcher{S3Client: client}, nil
// Initialize S3 client if not provided
if cfg.S3Client == nil {
awsCfg, err := config.LoadDefaultConfig(context.TODO())
if err != nil {
return nil, err
}
cfg.S3Client = s3.NewFromConfig(awsCfg)
}
return &S3Fetcher{S3Client: cfg.S3Client}, nil
}
// Fetch retrieves the configuration from an S3 bucket