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
|
||||
})
|
||||
}
|
||||
|
||||
@@ -11,7 +11,7 @@ func TestCaddyExtension(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
composeYAML string
|
||||
expectedConfig string
|
||||
wantConfig string
|
||||
wantErr bool
|
||||
}{
|
||||
{
|
||||
@@ -25,7 +25,7 @@ services:
|
||||
reverse_proxy web:80
|
||||
}
|
||||
`,
|
||||
expectedConfig: `example.com {
|
||||
wantConfig: `example.com {
|
||||
reverse_proxy web:80
|
||||
}
|
||||
`,
|
||||
@@ -42,12 +42,24 @@ services:
|
||||
reverse_proxy web:80
|
||||
}
|
||||
`,
|
||||
expectedConfig: `example.com {
|
||||
wantConfig: `example.com {
|
||||
reverse_proxy web:80
|
||||
}
|
||||
`,
|
||||
},
|
||||
|
||||
{
|
||||
name: "x-caddy with path to Caddyfile",
|
||||
composeYAML: `
|
||||
services:
|
||||
web:
|
||||
image: nginx
|
||||
x-caddy: testdata/Caddyfile
|
||||
`,
|
||||
wantConfig: `test.example.com {
|
||||
reverse_proxy test:8000
|
||||
}
|
||||
`,
|
||||
},
|
||||
{
|
||||
name: "x-caddy with empty object",
|
||||
composeYAML: `
|
||||
@@ -56,7 +68,7 @@ services:
|
||||
image: nginx
|
||||
x-caddy: {}
|
||||
`,
|
||||
expectedConfig: "",
|
||||
wantConfig: "",
|
||||
},
|
||||
{
|
||||
name: "x-caddy with empty string",
|
||||
@@ -66,7 +78,7 @@ services:
|
||||
image: nginx
|
||||
x-caddy: ""
|
||||
`,
|
||||
expectedConfig: "",
|
||||
wantConfig: "",
|
||||
},
|
||||
{
|
||||
name: "x-caddy with extra unknown field should fail",
|
||||
@@ -117,7 +129,40 @@ services:
|
||||
caddy, ok := caddyExt.(Caddy)
|
||||
require.True(t, ok, "x-caddy extension is not Caddy type")
|
||||
|
||||
assert.Equal(t, tt.expectedConfig, caddy.Config)
|
||||
assert.Equal(t, tt.wantConfig, caddy.Config)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestIsCaddyfilePath(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
input string
|
||||
want bool
|
||||
}{
|
||||
// Should be detected as file paths.
|
||||
{"relative path with slash", "./Caddyfile", true},
|
||||
{"relative path parent", "../Caddyfile", true},
|
||||
{"relative path", "relative/path/to/file", true},
|
||||
{"absolute path", "/etc/caddy/Caddyfile", true},
|
||||
{"just Caddyfile", "Caddyfile", true},
|
||||
{"Caddyfile with suffix", "Caddyfile.app", true},
|
||||
{"caddyfile lowercase", "caddyfile", true},
|
||||
{"with .caddyfile extension", "my.caddyfile", true},
|
||||
{"with .Caddyfile extension", "my.Caddyfile", true},
|
||||
{"with .caddy extension", "config.caddy", true},
|
||||
{"with .conf extension", "caddy.conf", true},
|
||||
{"simple filename", "config", true},
|
||||
|
||||
// Should NOT be detected as file paths.
|
||||
{"multiline config", "example.com {\n reverse_proxy :8080\n}", false},
|
||||
{"empty string", "", false},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
result := isCaddyfilePath(tt.input)
|
||||
assert.Equal(t, tt.want, result, "isCaddyfilePath(%q) should be %v", tt.input, tt.want)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -25,8 +25,8 @@ func LoadProject(ctx context.Context, paths []string, opts ...composecli.Project
|
||||
// If none was selected, get default Compose file names from current or parent folders.
|
||||
composecli.WithDefaultConfigPath,
|
||||
composecli.WithExtension(CaddyExtensionKey, Caddy{}),
|
||||
composecli.WithExtension(PortsExtensionKey, PortsSource{}),
|
||||
composecli.WithExtension(MachinesExtensionKey, MachinesSource{}),
|
||||
composecli.WithExtension(PortsExtensionKey, PortsSource{}),
|
||||
}
|
||||
|
||||
options, err := composecli.NewProjectOptions(
|
||||
@@ -42,6 +42,9 @@ func LoadProject(ctx context.Context, paths []string, opts ...composecli.Project
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if project, err = transformServicesCaddyExtension(project); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if project, err = transformServicesPortsExtension(project); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -46,7 +46,10 @@ func loadProjectFromContent(t *testing.T, content string) (*types.Project, error
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Apply ports extension transformation since we're not using LoadProject
|
||||
// Apply extension transformations since we're not using LoadProject.
|
||||
if project, err = transformServicesCaddyExtension(project); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if project, err = transformServicesPortsExtension(project); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
test.example.com {
|
||||
reverse_proxy test:8000
|
||||
}
|
||||
Reference in New Issue
Block a user