Skip to content

Commit 9936908

Browse files
authored
Merge pull request #351 from milas/env-extends
2 parents 99db19a + 8f90dd8 commit 9936908

File tree

8 files changed

+45
-7
lines changed

8 files changed

+45
-7
lines changed

cli/options.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,11 @@ func WithOsEnv(o *ProjectOptions) error {
190190
// WithEnvFile set an alternate env file
191191
// deprecated - use WithEnvFiles
192192
func WithEnvFile(file string) ProjectOptionsFn {
193-
return WithEnvFiles(file)
193+
var files []string
194+
if file != "" {
195+
files = []string{file}
196+
}
197+
return WithEnvFiles(files...)
194198
}
195199

196200
// WithEnvFiles set alternate env files

loader/loader.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -583,6 +583,10 @@ func loadServiceWithExtends(filename, name string, servicesDict map[string]inter
583583
}
584584
baseService.Volumes[i].Source = resolveMaybeUnixPath(vol.Source, baseFileParent, lookupEnv)
585585
}
586+
587+
for i, envFile := range baseService.EnvFile {
588+
baseService.EnvFile[i] = resolveMaybeUnixPath(envFile, baseFileParent, lookupEnv)
589+
}
586590
}
587591

588592
serviceConfig, err = _merge(baseService, serviceConfig)

loader/loader_test.go

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,12 @@ import (
2626
"testing"
2727
"time"
2828

29-
"github.com/compose-spec/compose-go/types"
3029
"github.com/google/go-cmp/cmp/cmpopts"
3130
"github.com/sirupsen/logrus"
3231
"gotest.tools/v3/assert"
3332
is "gotest.tools/v3/assert/cmp"
33+
34+
"github.com/compose-spec/compose-go/types"
3435
)
3536

3637
func buildConfigDetails(yaml string, env map[string]string) types.ConfigDetails {
@@ -1853,16 +1854,29 @@ func TestLoadWithExtends(t *testing.T) {
18531854
actual, err := Load(configDetails)
18541855
assert.NilError(t, err)
18551856

1857+
extendsDir, err := filepath.Abs(filepath.Join("testdata", "subdir"))
1858+
assert.NilError(t, err)
1859+
1860+
expectedEnvFilePath := filepath.Join(extendsDir, "extra.env")
1861+
1862+
expectedExtendsPath := filepath.Join(
1863+
extendsDir,
1864+
"compose-test-extends-imported.yaml",
1865+
)
1866+
18561867
expServices := types.Services{
18571868
{
18581869
Name: "importer",
18591870
Image: "nginx",
18601871
Extends: types.ExtendsConfig{
1861-
"file": strPtr("compose-test-extends-imported.yaml"),
1872+
"file": &expectedExtendsPath,
18621873
"service": strPtr("imported"),
18631874
},
1864-
Environment: types.MappingWithEquals{},
1865-
Networks: map[string]*types.ServiceNetworkConfig{"default": nil},
1875+
Environment: types.MappingWithEquals{
1876+
"SOURCE": strPtr("extends"),
1877+
},
1878+
EnvFile: []string{expectedEnvFilePath},
1879+
Networks: map[string]*types.ServiceNetworkConfig{"default": nil},
18661880
Volumes: []types.ServiceVolumeConfig{{
18671881
Type: "bind",
18681882
Source: "/opt/data",
@@ -1889,6 +1903,13 @@ func TestLoadWithExtendsWithContextUrl(t *testing.T) {
18891903
actual, err := Load(configDetails)
18901904
assert.NilError(t, err)
18911905

1906+
expectedExtendsPath, err := filepath.Abs(
1907+
filepath.Join(
1908+
"testdata",
1909+
"compose-test-extends-with-context-url-imported.yaml",
1910+
),
1911+
)
1912+
assert.NilError(t, err)
18921913
expServices := types.Services{
18931914
{
18941915
Name: "importer-with-https-url",
@@ -1897,7 +1918,7 @@ func TestLoadWithExtendsWithContextUrl(t *testing.T) {
18971918
Dockerfile: "Dockerfile",
18981919
},
18991920
Extends: types.ExtendsConfig{
1900-
"file": strPtr("compose-test-extends-with-context-url-imported.yaml"),
1921+
"file": &expectedExtendsPath,
19011922
"service": strPtr("imported-with-https-url"),
19021923
},
19031924
Environment: types.MappingWithEquals{},

loader/normalize.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,11 @@ func normalize(project *types.Project, resolvePaths bool) error {
9090
}
9191
s.Environment = s.Environment.Resolve(fn)
9292

93+
if extendFile := s.Extends["file"]; extendFile != nil && *extendFile != "" {
94+
p := absPath(project.WorkingDir, *extendFile)
95+
s.Extends["file"] = &p
96+
}
97+
9398
err := relocateLogDriver(&s)
9499
if err != nil {
95100
return err
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
services:
22
importer:
33
extends:
4-
file: compose-test-extends-imported.yaml
4+
file: subdir/compose-test-extends-imported.yaml
55
service: imported
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
services:
22
imported:
33
image: nginx
4+
env_file:
5+
- extra.env
46
volumes:
57
- /opt/data:/var/lib/mysql

loader/testdata/subdir/extra.env

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
SOURCE=extends

types/types_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,7 @@ func TestMarshalServiceEntrypoint(t *testing.T) {
313313
}
314314

315315
for _, tc := range tcs {
316+
tc := tc
316317
t.Run(tc.name, func(t *testing.T) {
317318
t.Parallel()
318319

0 commit comments

Comments
 (0)