Skip to content
This repository was archived by the owner on Jun 26, 2023. It is now read-only.

Commit 0a14182

Browse files
authored
feat(command): support multi file paths EE-782
1 parent d457f9a commit 0a14182

File tree

5 files changed

+63
-31
lines changed

5 files changed

+63
-31
lines changed

go.mod

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
11
module github.com/portainer/docker-compose-wrapper
22

33
go 1.15
4-
5-
require github.com/stretchr/testify v1.7.0 // indirect

go.sum

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +0,0 @@
1-
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
2-
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
3-
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
4-
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
5-
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
6-
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
7-
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
8-
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
9-
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
10-
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

utils_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@ func TestIsBinaryPresent(t *testing.T) {
1111
Expected bool
1212
}
1313
testCases := []testCase{
14-
testCase{
14+
{
1515
Name: "not existing",
1616
Binary: "qwgq-er-gerw",
1717
Expected: false,
1818
},
19-
testCase{
19+
{
2020
Name: "docker-compose exists",
2121
Binary: "docker-compose",
2222
Expected: true,

wrapper.go

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"fmt"
77
"os"
88
"os/exec"
9+
"strings"
910
)
1011

1112
var (
@@ -28,13 +29,13 @@ func NewComposeWrapper(binaryPath string) (*ComposeWrapper, error) {
2829
}
2930

3031
// Up create and start containers
31-
func (wrapper *ComposeWrapper) Up(filePath, url, projectName, envFilePath, configPath string) ([]byte, error) {
32-
return wrapper.Command(newUpCommand(filePath), url, projectName, envFilePath, configPath)
32+
func (wrapper *ComposeWrapper) Up(filePaths []string, url, projectName, envFilePath, configPath string) ([]byte, error) {
33+
return wrapper.Command(newUpCommand(filePaths), url, projectName, envFilePath, configPath)
3334
}
3435

3536
// Down stop and remove containers
36-
func (wrapper *ComposeWrapper) Down(filePath, url, projectName string) ([]byte, error) {
37-
return wrapper.Command(newDownCommand(filePath), url, projectName, "", "")
37+
func (wrapper *ComposeWrapper) Down(filePaths []string, url, projectName string) ([]byte, error) {
38+
return wrapper.Command(newDownCommand(filePaths), url, projectName, "", "")
3839
}
3940

4041
// Command exectue a docker-compose commanåd
@@ -76,19 +77,24 @@ type composeCommand struct {
7677
args []string
7778
}
7879

79-
func newCommand(command []string, filePath string) composeCommand {
80+
func newCommand(command []string, filePaths []string) composeCommand {
81+
var args []string
82+
for _, path := range filePaths {
83+
args = append(args, "-f")
84+
args = append(args, strings.TrimSpace(path))
85+
}
8086
return composeCommand{
81-
args: []string{"-f", filePath},
87+
args: args,
8288
command: command,
8389
}
8490
}
8591

86-
func newUpCommand(filePath string) composeCommand {
87-
return newCommand([]string{"up", "-d"}, filePath)
92+
func newUpCommand(filePaths []string) composeCommand {
93+
return newCommand([]string{"up", "-d"}, filePaths)
8894
}
8995

90-
func newDownCommand(filePath string) composeCommand {
91-
return newCommand([]string{"down", "--remove-orphans"}, filePath)
96+
func newDownCommand(filePaths []string) composeCommand {
97+
return newCommand([]string{"down", "--remove-orphans"}, filePaths)
9298
}
9399

94100
func (command *composeCommand) WithProjectName(projectName string) {

wrapper_test.go

Lines changed: 45 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"os"
77
"os/exec"
88
"path/filepath"
9+
"reflect"
910
"strings"
1011
"testing"
1112
)
@@ -19,39 +20,76 @@ func setup(t *testing.T) *ComposeWrapper {
1920
return w
2021
}
2122

23+
func Test_NewCommand_SingleFilePath(t *testing.T) {
24+
cmd := newCommand([]string{"up", "-d"}, []string{"docker-compose.yml"})
25+
expected := []string{"-f", "docker-compose.yml"}
26+
if !reflect.DeepEqual(cmd.args, expected) {
27+
t.Errorf("wrong output args, want: %v, got: %v", expected, cmd.args)
28+
}
29+
}
30+
31+
func Test_NewCommand_MultiFilePaths(t *testing.T) {
32+
cmd := newCommand([]string{"up", "-d"}, []string{"docker-compose.yml", "docker-compose-override.yml"})
33+
expected := []string{"-f", "docker-compose.yml", "-f", "docker-compose-override.yml"}
34+
if !reflect.DeepEqual(cmd.args, expected) {
35+
t.Errorf("wrong output args, want: %v, got: %v", expected, cmd.args)
36+
}
37+
}
38+
39+
func Test_NewCommand_MultiFilePaths_WithSpaces(t *testing.T) {
40+
cmd := newCommand([]string{"up", "-d"}, []string{" docker-compose.yml", "docker-compose-override.yml "})
41+
expected := []string{"-f", "docker-compose.yml", "-f", "docker-compose-override.yml"}
42+
if !reflect.DeepEqual(cmd.args, expected) {
43+
t.Errorf("wrong output args, want: %v, got: %v", expected, cmd.args)
44+
}
45+
}
46+
2247
func Test_UpAndDown(t *testing.T) {
2348

2449
const composeFileContent = `version: "3.9"
50+
services:
51+
busybox:
52+
image: "alpine:3.7"
53+
container_name: "test_container_one"`
54+
55+
const overrideComposeFileContent = `version: "3.9"
2556
services:
2657
busybox:
2758
image: "alpine:latest"
28-
container_name: "compose_wrapper_test"`
29-
const composedContainerName = "compose_wrapper_test"
59+
container_name: "test_container_two"`
60+
61+
const composeContainerName = "test_container_two"
3062

3163
w := setup(t)
3264

3365
dir := os.TempDir()
66+
defer os.RemoveAll(dir)
67+
68+
filePathOriginal, err := createFile(dir, "docker-compose.yml", composeFileContent)
69+
if err != nil {
70+
t.Fatal(err)
71+
}
3472

35-
filePath, err := createComposeFile(dir, composeFileContent)
73+
filePathOverride, err := createFile(dir, "docker-compose-override.yml", overrideComposeFileContent)
3674
if err != nil {
3775
t.Fatal(err)
3876
}
3977

40-
_, err = w.Up(filePath, "", "test1", "")
78+
_, err = w.Up([]string{filePathOriginal, filePathOverride}, "", "test1", "", "")
4179
if err != nil {
4280
t.Fatal(err)
4381
}
4482

45-
if !containerExists(composedContainerName) {
83+
if !containerExists(composeContainerName) {
4684
t.Fatal("container should exist")
4785
}
4886

49-
_, err = w.Down(filePath, "", "test1")
87+
_, err = w.Down([]string{filePathOriginal, filePathOverride}, "", "test1")
5088
if err != nil {
5189
t.Fatal(err)
5290
}
5391

54-
if containerExists(composedContainerName) {
92+
if containerExists(composeContainerName) {
5593
t.Fatal("container should be removed")
5694
}
5795
}

0 commit comments

Comments
 (0)