diff --git a/activate b/activate index a58a470..0e4c8a8 100755 --- a/activate +++ b/activate @@ -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" "$@")" diff --git a/cmd/bowline/main.go b/cmd/bowline/main.go index 1923932..c2a9b40 100644 --- a/cmd/bowline/main.go +++ b/cmd/bowline/main.go @@ -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'") @@ -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. diff --git a/cmd/bowline/main_test.go b/cmd/bowline/main_test.go new file mode 100644 index 0000000..367aa49 --- /dev/null +++ b/cmd/bowline/main_test.go @@ -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) + } + }) + } + +}