* chore(io): migrate pkg/devops to Medium abstraction This commit migrates the pkg/devops package to use the io.Medium abstraction instead of direct calls to io.Local or the os package. Changes: - Updated DevOps, ImageManager, and Manifest structs to hold an io.Medium. - Updated New, NewImageManager, and LoadConfig to accept an io.Medium. - Updated ImageSource interface and its implementations (GitHubSource, CDNSource) to accept io.Medium in Download method. - Refactored internal helper functions (hasFile, hasPackageScript, etc.) to use io.Medium. - Updated all unit tests and CLI entry points to pass the appropriate io.Medium. This migration improves the testability and flexibility of the devops package by allowing for different storage backends. * chore(io): migrate pkg/devops to Medium abstraction This commit completes the migration of the pkg/devops package to the io.Medium abstraction. Changes: - Refactored DevOps, ImageManager, and Manifest structs to use io.Medium for storage operations. - Updated New, NewImageManager, and LoadConfig to accept an io.Medium. - Updated ImageSource interface and its implementations (GitHubSource, CDNSource) to accept io.Medium in Download method. - Refactored internal helper functions (hasFile, hasPackageScript, etc.) to use io.Medium. - Updated all unit tests and CLI entry points to pass the appropriate io.Medium. - Fixed formatting issues in test files. This migration enables easier testing and supports alternative storage backends.
113 lines
2.6 KiB
Go
113 lines
2.6 KiB
Go
package sources
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
goio "io"
|
|
"net/http"
|
|
"os"
|
|
"path/filepath"
|
|
|
|
"github.com/host-uk/core/pkg/io"
|
|
)
|
|
|
|
// CDNSource downloads images from a CDN or S3 bucket.
|
|
type CDNSource struct {
|
|
config SourceConfig
|
|
}
|
|
|
|
// Compile-time interface check.
|
|
var _ ImageSource = (*CDNSource)(nil)
|
|
|
|
// NewCDNSource creates a new CDN source.
|
|
func NewCDNSource(cfg SourceConfig) *CDNSource {
|
|
return &CDNSource{config: cfg}
|
|
}
|
|
|
|
// Name returns "cdn".
|
|
func (s *CDNSource) Name() string {
|
|
return "cdn"
|
|
}
|
|
|
|
// Available checks if CDN URL is configured.
|
|
func (s *CDNSource) Available() bool {
|
|
return s.config.CDNURL != ""
|
|
}
|
|
|
|
// LatestVersion fetches version from manifest or returns "latest".
|
|
func (s *CDNSource) LatestVersion(ctx context.Context) (string, error) {
|
|
// Try to fetch manifest.json for version info
|
|
url := fmt.Sprintf("%s/manifest.json", s.config.CDNURL)
|
|
req, err := http.NewRequestWithContext(ctx, "GET", url, nil)
|
|
if err != nil {
|
|
return "latest", nil
|
|
}
|
|
|
|
resp, err := http.DefaultClient.Do(req)
|
|
if err != nil || resp.StatusCode != 200 {
|
|
return "latest", nil
|
|
}
|
|
defer func() { _ = resp.Body.Close() }()
|
|
|
|
// For now, just return latest - could parse manifest for version
|
|
return "latest", nil
|
|
}
|
|
|
|
// Download downloads the image from CDN.
|
|
func (s *CDNSource) Download(ctx context.Context, m io.Medium, dest string, progress func(downloaded, total int64)) error {
|
|
url := fmt.Sprintf("%s/%s", s.config.CDNURL, s.config.ImageName)
|
|
|
|
req, err := http.NewRequestWithContext(ctx, "GET", url, nil)
|
|
if err != nil {
|
|
return fmt.Errorf("cdn.Download: %w", err)
|
|
}
|
|
|
|
resp, err := http.DefaultClient.Do(req)
|
|
if err != nil {
|
|
return fmt.Errorf("cdn.Download: %w", err)
|
|
}
|
|
defer func() { _ = resp.Body.Close() }()
|
|
|
|
if resp.StatusCode != 200 {
|
|
return fmt.Errorf("cdn.Download: HTTP %d", resp.StatusCode)
|
|
}
|
|
|
|
// Ensure dest directory exists
|
|
if err := m.EnsureDir(dest); err != nil {
|
|
return fmt.Errorf("cdn.Download: %w", err)
|
|
}
|
|
|
|
// Create destination file
|
|
destPath := filepath.Join(dest, s.config.ImageName)
|
|
f, err := os.Create(destPath)
|
|
if err != nil {
|
|
return fmt.Errorf("cdn.Download: %w", err)
|
|
}
|
|
defer func() { _ = f.Close() }()
|
|
|
|
// Copy with progress
|
|
total := resp.ContentLength
|
|
var downloaded int64
|
|
|
|
buf := make([]byte, 32*1024)
|
|
for {
|
|
n, err := resp.Body.Read(buf)
|
|
if n > 0 {
|
|
if _, werr := f.Write(buf[:n]); werr != nil {
|
|
return fmt.Errorf("cdn.Download: %w", werr)
|
|
}
|
|
downloaded += int64(n)
|
|
if progress != nil {
|
|
progress(downloaded, total)
|
|
}
|
|
}
|
|
if err == goio.EOF {
|
|
break
|
|
}
|
|
if err != nil {
|
|
return fmt.Errorf("cdn.Download: %w", err)
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|