Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 32 additions & 5 deletions internal/backend/apple.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ func RunArgs(project string, svc types.ServiceConfig) ([]string, error) {
}

for _, p := range svc.Ports {
args = append(args, "--publish", fmt.Sprintf("%s:%d", p.Published, p.Target))
args = append(args, "--publish", publishSpec(p))
}

for k, v := range svc.Environment {
Expand All @@ -176,11 +176,14 @@ func RunArgs(project string, svc types.ServiceConfig) ([]string, error) {
}
}

var entryArgs []string
if svc.Entrypoint != nil {
if len(svc.Entrypoint) == 0 {
args = append(args, "--entrypoint", "")
} else {
args = append(args, "--entrypoint", strings.Join(svc.Entrypoint, " "))
// CLI takes one executable. Remaining entrypoint elements are args, prepended to command.
args = append(args, "--entrypoint", svc.Entrypoint[0])
entryArgs = svc.Entrypoint[1:]
}
}

Expand Down Expand Up @@ -228,14 +231,14 @@ func RunArgs(project string, svc types.ServiceConfig) ([]string, error) {
for _, v := range svc.Volumes {
switch v.Type {
case "bind":
args = append(args, "--volume", fmt.Sprintf("%s:%s", v.Source, v.Target))
args = append(args, "--volume", volumeSpec(v.Source, v.Target, v.ReadOnly))
case "volume", "":
if v.Source != "" {
hostPath := namedVolumePath(project, v.Source)
if err := os.MkdirAll(hostPath, 0o755); err != nil {
return nil, fmt.Errorf("creating volume dir %q: %w", hostPath, err)
}
args = append(args, "--volume", fmt.Sprintf("%s:%s", hostPath, v.Target))
args = append(args, "--volume", volumeSpec(hostPath, v.Target, v.ReadOnly))
}
}
}
Expand Down Expand Up @@ -268,14 +271,38 @@ func RunArgs(project string, svc types.ServiceConfig) ([]string, error) {
}

args = append(args, svc.Image)

args = append(args, entryArgs...)
if len(svc.Command) > 0 {
args = append(args, svc.Command...)
}

return args, nil
}

// publishSpec is [host-ip:]host-port:container-port[/protocol]. TCP is the CLI default.
func publishSpec(p types.ServicePortConfig) string {
spec := fmt.Sprintf("%s:%d", p.Published, p.Target)
if p.HostIP != "" {
host := p.HostIP
if strings.Contains(host, ":") && !strings.HasPrefix(host, "[") {
host = "[" + host + "]"
}
spec = host + ":" + spec
}
if p.Protocol != "" && !strings.EqualFold(p.Protocol, "tcp") {
spec += "/" + p.Protocol
}
return spec
}

func volumeSpec(source, target string, readOnly bool) string {
spec := source + ":" + target
if readOnly {
spec += ":ro"
}
return spec
}

func namedVolumePath(project, volume string) string {
home, _ := os.UserHomeDir()
return filepath.Join(home, ".apple-compose", "volumes", project, volume)
Expand Down
75 changes: 75 additions & 0 deletions internal/backend/apple_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,38 @@ func TestRunArgs_PortMapping(t *testing.T) {
assertContains(t, args, "--publish", "8080:80")
}

func TestRunArgs_PublishHostIP(t *testing.T) {
svc := types.ServiceConfig{
Name: "web",
Image: "nginx:alpine",
Ports: []types.ServicePortConfig{
{HostIP: "127.0.0.1", Published: "8080", Target: 80},
{HostIP: "::1", Published: "8080", Target: 80},
},
}
args, err := RunArgs("myapp", svc)
if err != nil {
t.Fatal(err)
}
assertContains(t, args, "--publish", "127.0.0.1:8080:80")
assertContains(t, args, "--publish", "[::1]:8080:80")
}

func TestRunArgs_PublishProtocol(t *testing.T) {
svc := types.ServiceConfig{
Name: "dns",
Image: "nginx:alpine",
Ports: []types.ServicePortConfig{
{Published: "5353", Target: 53, Protocol: "udp"},
},
}
args, err := RunArgs("myapp", svc)
if err != nil {
t.Fatal(err)
}
assertContains(t, args, "--publish", "5353:53/udp")
}

func TestRunArgs_EnvVars(t *testing.T) {
svc := types.ServiceConfig{
Name: "db",
Expand Down Expand Up @@ -96,6 +128,21 @@ func TestRunArgs_BindMount(t *testing.T) {
assertContains(t, args, "--volume", "/host/data:/var/lib/postgresql/data")
}

func TestRunArgs_BindMountReadOnly(t *testing.T) {
svc := types.ServiceConfig{
Name: "db",
Image: "postgres:16",
Volumes: []types.ServiceVolumeConfig{
{Type: "bind", Source: "/host/data", Target: "/var/lib/postgresql/data", ReadOnly: true},
},
}
args, err := RunArgs("myapp", svc)
if err != nil {
t.Fatal(err)
}
assertContains(t, args, "--volume", "/host/data:/var/lib/postgresql/data:ro")
}

func TestRunArgs_Entrypoint(t *testing.T) {
svc := types.ServiceConfig{
Name: "web",
Expand All @@ -109,6 +156,25 @@ func TestRunArgs_Entrypoint(t *testing.T) {
assertContains(t, args, "--entrypoint", "/docker-entrypoint.sh")
}

func TestRunArgs_EntrypointArgsPrependedToCommand(t *testing.T) {
svc := types.ServiceConfig{
Name: "web",
Image: "nginx:alpine",
Entrypoint: types.ShellCommand{"sh", "-c", "echo hi"},
Command: types.ShellCommand{"nginx"},
}
args, err := RunArgs("myapp", svc)
if err != nil {
t.Fatal(err)
}
assertContains(t, args, "--entrypoint", "sh")
got := argsAfter(args, "nginx:alpine")
want := []string{"-c", "echo hi", "nginx"}
if strings.Join(got, "\x00") != strings.Join(want, "\x00") {
t.Fatalf("args after image: got %q want %q", got, want)
}
}

func TestRunArgs_UserAndWorkdir(t *testing.T) {
svc := types.ServiceConfig{
Name: "web",
Expand Down Expand Up @@ -423,6 +489,15 @@ func TestAppleContainer_UnmarshalContainerCLI141JSON(t *testing.T) {
}
}

func argsAfter(args []string, token string) []string {
for i, a := range args {
if a == token {
return args[i+1:]
}
}
return nil
}

// assertContains checks that key and value appear consecutively in args.
func assertContains(t *testing.T, args []string, key, value string) {
t.Helper()
Expand Down
Loading