fix: Resolve repository URL for install/update (registry + install fallbacks)

This commit is contained in:
2026-08-01 23:59:39 +01:00
parent 678356bd5b
commit 836153ccb5
4 changed files with 64 additions and 9 deletions
+37 -2
View File
@@ -7,6 +7,7 @@ import (
"fmt"
"io"
"net/http"
"net/url"
"os"
"path/filepath"
"strconv"
@@ -34,11 +35,14 @@ func InstallContainer(ctx context.Context, item types.RegistryContainer) error {
}
// 2. Fetch raw docker-compose.yaml from the Git URL
rawURL := fmt.Sprintf("%s/raw/branch/master/docker-compose.yaml", item.URL)
rawURL, err := composeRawURL(item)
if err != nil {
return fmt.Errorf("failed to resolve repository URL: %w", err)
}
composePath := filepath.Join(targetDir, "docker-compose.yaml")
logger.Info("Fetching compose file from %s...", rawURL)
if err := downloadFile(ctx, rawURL, composePath); err != nil {
if err := downloadCompose(ctx, rawURL, composePath); err != nil {
return fmt.Errorf("failed to download docker-compose.yaml: %w", err)
}
@@ -347,6 +351,37 @@ func getContainerDataDir(serviceName string) (string, error) {
return filepath.Join(root, serviceName), nil
}
// composeRawURL resolves the raw docker-compose.yaml URL for a container,
// preferring the repository URL in item.URL (falling back to item.Source) and
// rejecting URLs without an http/https scheme.
func composeRawURL(item types.RegistryContainer) (string, error) {
base := strings.TrimRight(item.URL, "/")
if base == "" {
base = strings.TrimRight(item.Source, "/")
}
u, err := url.Parse(base)
if err != nil || (u.Scheme != "http" && u.Scheme != "https") {
return "", fmt.Errorf("invalid repository URL %q", base)
}
return fmt.Sprintf("%s/raw/branch/master/docker-compose.yaml", base), nil
}
// downloadCompose fetches the compose file, retrying on the main branch when
// the repository's default branch is not master.
func downloadCompose(ctx context.Context, url, destPath string) error {
err := downloadFile(ctx, url, destPath)
if err != nil {
mainURL := strings.Replace(url, "/raw/branch/master/", "/raw/branch/main/", 1)
if mainURL != url {
if retryErr := downloadFile(ctx, mainURL, destPath); retryErr == nil {
return nil
}
}
return err
}
return nil
}
func downloadFile(ctx context.Context, url string, destPath string) error {
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
if err != nil {