2025-01-28 15:42:50 -06:00
|
|
|
package remotefetcher
|
2025-01-14 19:13:05 -06:00
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
2025-02-08 15:17:34 -06:00
|
|
|
"strings"
|
2025-01-14 19:13:05 -06:00
|
|
|
|
|
|
|
"github.com/aws/aws-sdk-go-v2/service/s3"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Option is a function that configures a fetcher.
|
2025-02-08 15:17:34 -06:00
|
|
|
type FetcherOption func(*FetcherConfig)
|
2025-01-14 19:13:05 -06:00
|
|
|
|
|
|
|
// FetcherConfig holds the configuration for a fetcher.
|
|
|
|
type FetcherConfig struct {
|
2025-01-28 15:42:50 -06:00
|
|
|
S3Client *s3.Client
|
|
|
|
HTTPClient *http.Client
|
2025-02-08 15:17:34 -06:00
|
|
|
FileType string
|
2025-01-28 15:42:50 -06:00
|
|
|
IgnoreFileNotFound bool
|
2025-01-14 19:13:05 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// WithS3Client sets the S3 client for the fetcher.
|
2025-02-08 15:17:34 -06:00
|
|
|
func WithS3Client(client *s3.Client) FetcherOption {
|
2025-01-14 19:13:05 -06:00
|
|
|
return func(cfg *FetcherConfig) {
|
|
|
|
cfg.S3Client = client
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithHTTPClient sets the HTTP client for the fetcher.
|
2025-02-08 15:17:34 -06:00
|
|
|
func WithHTTPClient(client *http.Client) FetcherOption {
|
2025-01-14 19:13:05 -06:00
|
|
|
return func(cfg *FetcherConfig) {
|
|
|
|
cfg.HTTPClient = client
|
|
|
|
}
|
|
|
|
}
|
2025-01-28 15:42:50 -06:00
|
|
|
|
2025-02-08 15:17:34 -06:00
|
|
|
func IgnoreFileNotFound() FetcherOption {
|
2025-01-28 15:42:50 -06:00
|
|
|
return func(cfg *FetcherConfig) {
|
|
|
|
cfg.IgnoreFileNotFound = true
|
|
|
|
}
|
|
|
|
}
|
2025-02-08 15:17:34 -06:00
|
|
|
|
|
|
|
// WithFileType ensures the default FileType will be yaml
|
|
|
|
func WithFileType(fileType string) FetcherOption {
|
|
|
|
return func(cfg *FetcherConfig) {
|
|
|
|
cfg.FileType = fileType
|
|
|
|
if strings.TrimSpace(fileType) == "" {
|
|
|
|
cfg.FileType = "yaml"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|