Skip to content

Commit

Permalink
(#25) Support passing multiple docker-compose files
Browse files Browse the repository at this point in the history
  • Loading branch information
mdelapenya committed Sep 18, 2019
1 parent f7027b2 commit a6d823e
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 15 deletions.
44 changes: 31 additions & 13 deletions compose.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,23 +27,31 @@ type DockerCompose interface {
// LocalDockerCompose represents a Docker Compose execution using local binary
// docker-compose or docker-compose.exe, depending on the underlying platform
type LocalDockerCompose struct {
Executable string
ComposeFilePath string
Identifier string
Cmd []string
Env map[string]string
Executable string
ComposeFilePaths []string
absComposeFilePaths []string
Identifier string
Cmd []string
Env map[string]string
}

// NewLocalDockerCompose returns an instance of the local Docker Compose
func NewLocalDockerCompose(filePath string, identifier string) *LocalDockerCompose {
func NewLocalDockerCompose(filePaths []string, identifier string) *LocalDockerCompose {
dc := &LocalDockerCompose{}

dc.Executable = "docker-compose"
if runtime.GOOS == "windows" {
dc.Executable = "docker-compose.exe"
}

dc.ComposeFilePath = filePath
dc.ComposeFilePaths = filePaths

dc.absComposeFilePaths = make([]string, len(filePaths))
for i, cfp := range dc.ComposeFilePaths {
abs, _ := filepath.Abs(cfp)
dc.absComposeFilePaths[i] = abs
}

dc.Identifier = strings.ToLower(identifier)

return dc
Expand All @@ -57,8 +65,13 @@ func (dc *LocalDockerCompose) Down() ExecError {
func (dc *LocalDockerCompose) getDockerComposeEnvironment() map[string]string {
environment := map[string]string{}

composeFileEnvVariableValue := ""
for _, abs := range dc.absComposeFilePaths {
composeFileEnvVariableValue += abs + string(os.PathListSeparator)
}

environment[envProjectName] = dc.Identifier
environment[envComposeFile] = dc.ComposeFilePath
environment[envComposeFile] = composeFileEnvVariableValue

return environment
}
Expand Down Expand Up @@ -148,16 +161,21 @@ func executeCompose(dc *LocalDockerCompose, args []string) ExecError {
environment[k] = v
}

abs, err := filepath.Abs(dc.ComposeFilePath)
pwd, name := filepath.Split(abs)
cmds := []string{}
pwd := "."
if len(dc.absComposeFilePaths) > 0 {
pwd, _ = filepath.Split(dc.absComposeFilePaths[0])

cmds := []string{
"-f", name,
for _, abs := range dc.absComposeFilePaths {
cmds = append(cmds, "-f", abs)
}
} else {
cmds = append(cmds, "-f", "docker-compose.yml")
}
cmds = append(cmds, args...)

execErr := execute(pwd, environment, dc.Executable, cmds)
err = execErr.Error
err := execErr.Error
if err != nil {
args := strings.Join(dc.Cmd, " ")
panic(
Expand Down
31 changes: 29 additions & 2 deletions compose_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ func TestLocalDockerCompose(t *testing.T) {

identifier := strings.ToLower(RandomString(6))

compose := NewLocalDockerCompose(path, identifier)
compose := NewLocalDockerCompose([]string{path}, identifier)

err := compose.
WithCommand([]string{"up", "-d"}).
Expand All @@ -34,7 +34,7 @@ func TestLocalDockerComposeWithEnvironment(t *testing.T) {

identifier := strings.ToLower(RandomString(6))

compose := NewLocalDockerCompose(path, identifier)
compose := NewLocalDockerCompose([]string{path}, identifier)
destroyFn := func() {
err := compose.Down()
checkIfError(t, err)
Expand All @@ -52,6 +52,33 @@ func TestLocalDockerComposeWithEnvironment(t *testing.T) {
assertContainerEnvContainsKeyValue(t, compose.Identifier+"_nginx_1", "bar", "BAR")
}

func TestLocalDockerComposeWithMultipleComposeFiles(t *testing.T) {
composeFiles := []string{
"testresources/docker-compose.yml",
"testresources/docker-compose-override.yml",
}

identifier := strings.ToLower(RandomString(6))

compose := NewLocalDockerCompose(composeFiles, identifier)
destroyFn := func() {
err := compose.Down()
checkIfError(t, err)
}
defer destroyFn()

err := compose.
WithCommand([]string{"up", "-d"}).
WithEnv(map[string]string{
"foo": "BAR",
"bar": "FOO",
}).
Invoke()
checkIfError(t, err)

assertContainerEnvContainsKeyValue(t, compose.Identifier+"_nginx_1", "foo", "FOO")
}

func assertContainerEnvContainsKeyValue(t *testing.T, identifier string, key string, value string) {
args := []string{"exec", identifier, "env"}

Expand Down
7 changes: 7 additions & 0 deletions testresources/docker-compose-override.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
version: '3'
services:
nginx:
image: nginx:stable-alpine
environment:
bar: ${foo}
foo: ${bar}

0 comments on commit a6d823e

Please sign in to comment.