Skip to content

Commit

Permalink
Adding support for multiple compose files using env vars #90
Browse files Browse the repository at this point in the history
  • Loading branch information
David Numan authored and davenuman committed Jun 5, 2020
1 parent fd9aee2 commit 9a21b51
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 2 deletions.
1 change: 1 addition & 0 deletions activate
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ eval "$(docker run --rm --privileged \
-v //var/run/docker.sock:/var/run/docker.sock \
-v "$(cygpath -w "$(pwd)" 2> /dev/null || pwd)":/src \
-e COMPOSE_PROJECT_NAME="${COMPOSE_PROJECT_NAME:-${PWD##*/}}" \
-e BOWLINE_COMPOSE_FILE -e COMPOSE_FILE \
-e DOCKER_API_VERSION "civicactions/bowline$BOWLINE_IMAGE_SUFFIX" "$0" "$@")"
30 changes: 28 additions & 2 deletions cmd/bowline/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,39 @@ package main
import (
"fmt"
"os"
"path/filepath"
"strings"

"github.com/CivicActions/bowline/pkg/exposedcmd"
)

func parseComposeFilesEnv() (string, []string) {
envBCF := os.Getenv("BOWLINE_COMPOSE_FILE")
envCF := os.Getenv("COMPOSE_FILE")

// Default if no env vars
var composeFileString string

switch {
case envBCF != "":
// BOWLINE_COMPOSE_FILE is the preferred env var
composeFileString = envBCF
case envCF != "":
// Use COMPOSE_FILE as second choice
composeFileString = envCF
default:
// Default to the docker-compose default
composeFileString = "docker-compose.yml"
}

composeFiles := filepath.SplitList(composeFileString)
return composeFileString, composeFiles
}

func main() {
composeFiles := []string{"docker-compose.yml"}
composeFileString, composeFiles := parseComposeFilesEnv()
fmt.Println("# composeFiles: ", composeFiles)

composeProjectName := os.Getenv("COMPOSE_PROJECT_NAME")
if composeProjectName == "" {
fmt.Printf("echo -e 'Error getting composer project name: ensure COMPOSE_PROJECT_NAME is set'")
Expand All @@ -21,7 +47,7 @@ func main() {
os.Exit(1)
}
for alias, command := range commands {
fmt.Printf("alias %s='%s'\n", alias, command)
fmt.Printf("alias %s='COMPOSE_FILE=\"%s\" %s'\n", alias, composeFileString, command)
}
fmt.Println("export BOWLINE_ACTIVATED=1")
// Print some info to user.
Expand Down
41 changes: 41 additions & 0 deletions cmd/bowline/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package main

import (
"os"
"testing"
)

func TestParseComposeFilesEnv(t *testing.T) {
var testSets = []struct {
name string
envVars map[string]string
want string
}{
{"BOWLINE_COMPOSE_FILE one file",
map[string]string{"BOWLINE_COMPOSE_FILE": "docker-compose.yml"}, "docker-compose.yml"},
{"BOWLINE_COMPOSE_FILE two files",
map[string]string{"BOWLINE_COMPOSE_FILE": "docker-compose.yml:file2"}, "docker-compose.yml:file2"},
{"COMPOSE_FILE",
map[string]string{"COMPOSE_FILE": "docker-compose.yml"}, "docker-compose.yml"},
{"No var set",
map[string]string{}, "docker-compose.yml"},
{"Both vars set",
map[string]string{"COMPOSE_FILE": "wrong value", "BOWLINE_COMPOSE_FILE": "correct"}, "correct"},
}

for _, ts := range testSets {
ts := ts
t.Run(ts.name, func(t *testing.T) {
os.Unsetenv("BOWLINE_COMPOSE_FILE")
os.Unsetenv("COMPOSE_FILE")
for key, val := range ts.envVars {
os.Setenv(key, val)
}
composeFileString, _ := parseComposeFilesEnv()
if composeFileString != ts.want {
t.Errorf("Unexpected results: %s does not match %s", composeFileString, ts.want)
}
})
}

}

0 comments on commit 9a21b51

Please sign in to comment.