Skip to content

Commit bcc2d94

Browse files
fix: Including Compose Files both globally and locally at the same time
1 parent cb7949e commit bcc2d94

File tree

2 files changed

+44
-25
lines changed

2 files changed

+44
-25
lines changed

main_test.go

+33-1
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,38 @@ services:
112112
assert.Contains(t, output, "CUSTOM_123456")
113113
}
114114

115+
func TestUserCanIncludeGlobalAndProjectComposeFile(t *testing.T) {
116+
projectPath := dockerized.GetDockerizedRoot() + "/test/project" + strconv.Itoa(rand.Int())
117+
118+
context := context().
119+
WithTempHome().
120+
WithHomeEnvFile(`COMPOSE_FILE="${COMPOSE_FILE};${HOME}/docker-compose.dockerized.yml"`).
121+
WithHomeFile("docker-compose.dockerized.yml", `
122+
version: "3"
123+
services:
124+
home_cmd:
125+
image: "alpine"
126+
entrypoint: [ "echo", "HOME123" ]
127+
`).
128+
WithDir(projectPath).
129+
WithCwd(projectPath).
130+
WithFile(projectPath+"/dockerized.env", `COMPOSE_FILE="${COMPOSE_FILE};${DOCKERIZED_PROJECT_ROOT}/docker-compose.dockerized.yml"`).
131+
WithFile(projectPath+"/docker-compose.dockerized.yml", `
132+
version: "3"
133+
services:
134+
project_cmd:
135+
image: "alpine"
136+
entrypoint: [ "echo", "PROJECT123" ]
137+
`)
138+
defer context.Restore()
139+
var outputProjectCmd = testDockerized(t, []string{"-v", "project_cmd"})
140+
assert.Contains(t, outputProjectCmd, "PROJECT123")
141+
142+
var outputHomeCmd = testDockerized(t, []string{"-v", "home_cmd"})
143+
assert.Contains(t, outputHomeCmd, "HOME123")
144+
145+
}
146+
115147
func (c *Context) WithEnv(key string, value string) *Context {
116148
_ = os.Setenv(key, value)
117149
return c
@@ -144,7 +176,7 @@ func (c *Context) WithCwd(path string) *Context {
144176
}
145177

146178
func (c *Context) WithHomeEnvFile(content string) *Context {
147-
return c.WithFile(c.homePath+"/dockerized.env", content)
179+
return c.WithHomeFile("dockerized.env", content)
148180
}
149181

150182
func (c *Context) WithFile(path string, content string) *Context {

pkg/dockerized.go

+11-24
Original file line numberDiff line numberDiff line change
@@ -293,30 +293,8 @@ func LoadEnvFiles(hostCwd string, optionVerbose bool) error {
293293
}
294294
}
295295

296-
dockerizedEnvMap, err := dotenv.ReadWithLookup(func(key string) (string, bool) {
297-
if os.Getenv(key) != "" {
298-
return os.Getenv(key), true
299-
} else {
300-
return "", false
301-
}
302-
}, defaultEnvFile)
303-
if err != nil {
304-
return err
305-
}
306-
307-
var lookupEnvOrDefault = func(key string) (string, bool) {
308-
var envValue = os.Getenv(key)
309-
if envValue != "" {
310-
return envValue, true
311-
}
312-
if dockerizedEnvMap[key] != "" {
313-
return dockerizedEnvMap[key], true
314-
}
315-
return "", false
316-
}
317-
318296
var envMap = make(map[string]string)
319-
err = func() error {
297+
err := func() error {
320298
for _, envFilePath := range envFiles {
321299
file, err := os.Open(envFilePath)
322300
if err != nil {
@@ -325,7 +303,16 @@ func LoadEnvFiles(hostCwd string, optionVerbose bool) error {
325303
defer file.Close()
326304

327305
envFileMap, err := dotenv.ParseWithLookup(file, func(key string) (string, bool) {
328-
return lookupEnvOrDefault(key)
306+
// 1. Lookup in the environment
307+
var envValue = os.Getenv(key)
308+
if envValue != "" {
309+
return envValue, true
310+
}
311+
// 2. Lookup in previous env files
312+
if envMap[key] != "" {
313+
return envMap[key], true
314+
}
315+
return "", false
329316
})
330317
if err != nil {
331318
return err

0 commit comments

Comments
 (0)