[WIP] v0.7.0
Some checks failed
ci/woodpecker/push/go-lint Pipeline failed

This commit is contained in:
2025-01-14 09:42:43 -06:00
parent aee513f786
commit 5c2bfcc940
34 changed files with 1072 additions and 353 deletions

View File

@ -0,0 +1,24 @@
package configfetcher
import "strings"
type ConfigFetcher interface {
// Fetch retrieves the configuration from the specified URL or source
// Returns the raw data as bytes or an error
Fetch(source string) ([]byte, error)
// Parse decodes the raw data into a Go structure (e.g., Commands, CommandLists)
// Takes the raw data as input and populates the target interface
Parse(data []byte, target interface{}) error
}
func NewConfigFetcher(source string) ConfigFetcher {
if strings.HasPrefix(source, "http") || strings.HasPrefix(source, "https") {
return &HTTPFetcher{}
} else if strings.HasPrefix(source, "s3") {
return &S3Fetcher{}
} else {
return &LocalFetcher{}
}
}

31
pkg/configfetcher/http.go Normal file
View File

@ -0,0 +1,31 @@
package configfetcher
import (
"errors"
"io"
"net/http"
"gopkg.in/yaml.v3"
)
type HTTPFetcher struct{}
// Fetch retrieves the configuration from the specified URL
func (h *HTTPFetcher) Fetch(source string) ([]byte, error) {
resp, err := http.Get(source)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, errors.New("failed to fetch remote config: " + resp.Status)
}
return io.ReadAll(resp.Body)
}
// Parse decodes the raw data into the provided target structure
func (h *HTTPFetcher) Parse(data []byte, target interface{}) error {
return yaml.Unmarshal(data, target)
}

View File

@ -0,0 +1,26 @@
package configfetcher
import (
"io"
"os"
"gopkg.in/yaml.v3"
)
type LocalFetcher struct{}
// Fetch retrieves the configuration from the specified local file path
func (l *LocalFetcher) Fetch(source string) ([]byte, error) {
file, err := os.Open(source)
if err != nil {
return nil, err
}
defer file.Close()
return io.ReadAll(file)
}
// Parse decodes the raw data into the provided target structure
func (l *LocalFetcher) Parse(data []byte, target interface{}) error {
return yaml.Unmarshal(data, target)
}

66
pkg/configfetcher/s3.go Normal file
View File

@ -0,0 +1,66 @@
package configfetcher
import (
"bytes"
"context"
"errors"
"strings"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/s3"
"gopkg.in/yaml.v3"
)
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
}
client := s3.NewFromConfig(cfg)
return &S3Fetcher{S3Client: client}, nil
}
// Fetch retrieves the configuration from an S3 bucket
// Source should be in the format "bucket-name/object-key"
func (s *S3Fetcher) Fetch(source string) ([]byte, error) {
bucket, key, err := parseS3Source(source)
if err != nil {
return nil, err
}
resp, err := s.S3Client.GetObject(context.TODO(), &s3.GetObjectInput{
Bucket: &bucket,
Key: &key,
})
if err != nil {
return nil, err
}
defer resp.Body.Close()
buf := new(bytes.Buffer)
_, err = buf.ReadFrom(resp.Body)
if err != nil {
return nil, err
}
return buf.Bytes(), nil
}
// Parse decodes the raw data into the provided target structure
func (s *S3Fetcher) Parse(data []byte, target interface{}) error {
return yaml.Unmarshal(data, target)
}
// Helper function to parse S3 source into bucket and key
func parseS3Source(source string) (bucket, key string, err error) {
parts := strings.SplitN(source, "/", 2)
if len(parts) != 2 {
return "", "", errors.New("invalid S3 source format, expected bucket-name/object-key")
}
return parts[0], parts[1], nil
}