mirror of
https://github.com/psviderski/uncloud.git
synced 2026-08-26 11:03:34 +00:00
chore: handle x-caddy: path/to/Caddyfile to read Caddy config in compose from file
This commit is contained in:
@@ -2,7 +2,11 @@ package compose
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/compose-spec/compose-go/v2/types"
|
||||
"github.com/mitchellh/mapstructure"
|
||||
)
|
||||
|
||||
@@ -41,3 +45,48 @@ func (c *Caddy) DecodeMapstructure(value any) error {
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// isCaddyfilePath determines if a string is likely a file path rather than inline Caddyfile config.
|
||||
func isCaddyfilePath(s string) bool {
|
||||
s = strings.TrimSpace(s)
|
||||
if s == "" {
|
||||
return false
|
||||
}
|
||||
|
||||
// For simplicity, multi-line string is considered an inline Caddyfile content.
|
||||
return !strings.Contains(s, "\n")
|
||||
}
|
||||
|
||||
// transformServicesCaddyExtension processes Caddy extensions to load configs from files if needed.
|
||||
func transformServicesCaddyExtension(project *types.Project) (*types.Project, error) {
|
||||
return project.WithServicesTransform(func(name string, service types.ServiceConfig) (types.ServiceConfig, error) {
|
||||
ext, ok := service.Extensions[CaddyExtensionKey]
|
||||
if !ok {
|
||||
return service, nil
|
||||
}
|
||||
|
||||
caddy, ok := ext.(Caddy)
|
||||
if !ok {
|
||||
return service, nil
|
||||
}
|
||||
|
||||
// Load the Caddyfile config from file if it's a path and replace the path with its content.
|
||||
if isCaddyfilePath(caddy.Config) {
|
||||
configPath := caddy.Config
|
||||
if !filepath.IsAbs(configPath) {
|
||||
configPath = filepath.Join(project.WorkingDir, configPath)
|
||||
}
|
||||
|
||||
content, err := os.ReadFile(configPath)
|
||||
if err != nil {
|
||||
return service, fmt.Errorf("read Caddy config (Caddyfile) from file '%s' for service '%s': %w",
|
||||
caddy.Config, name, err)
|
||||
}
|
||||
|
||||
caddy.Config = string(content)
|
||||
service.Extensions[CaddyExtensionKey] = caddy
|
||||
}
|
||||
|
||||
return service, nil
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user