Skip to content

Commit 0103a38

Browse files
committed
Introduce WithDiscardExtends
Signed-off-by: Nicolas De Loof <[email protected]>
1 parent e508c72 commit 0103a38

File tree

4 files changed

+39
-2
lines changed

4 files changed

+39
-2
lines changed

cli/options.go

+6
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,12 @@ func WithDiscardEnvFile(o *ProjectOptions) error {
204204
return nil
205205
}
206206

207+
// WithDiscardExtends sets discards the `extends` section after merge
208+
func WithDiscardExtends(o *ProjectOptions) error {
209+
o.loadOptions = append(o.loadOptions, loader.WithDiscardExtends)
210+
return nil
211+
}
212+
207213
// WithLoadOptions provides a hook to control how compose files are loaded
208214
func WithLoadOptions(loadOptions ...func(*loader.Options)) ProjectOptionsFn {
209215
return func(o *ProjectOptions) error {

loader/extends.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,9 @@ func applyServiceExtends(ctx context.Context, name string, services map[string]a
127127
return nil, err
128128
}
129129

130-
delete(merged, "extends")
130+
if opts.discardExtends {
131+
delete(merged, "extends")
132+
}
131133
services[name] = merged
132134
return merged, nil
133135
}

loader/loader.go

+8-1
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@ type Options struct {
7272
Interpolate *interp.Options
7373
// Discard 'env_file' entries after resolving to 'environment' section
7474
discardEnvFiles bool
75+
// Discard 'extends' entries after merge
76+
discardExtends bool
7577
// Set project projectName
7678
projectName string
7779
// Indicates when the projectName was imperatively set or guessed from path
@@ -245,6 +247,11 @@ func WithDiscardEnvFiles(opts *Options) {
245247
opts.discardEnvFiles = true
246248
}
247249

250+
// WithDiscardExtends sets the Options to discard the `extends` section after merge
251+
func WithDiscardExtends(opts *Options) {
252+
opts.discardExtends = true
253+
}
254+
248255
// WithSkipValidation sets the Options to skip validation when loading sections
249256
func WithSkipValidation(opts *Options) {
250257
opts.SkipValidation = true
@@ -542,7 +549,7 @@ func load(ctx context.Context, configDetails types.ConfigDetails, opts *Options,
542549
return nil, errors.New("empty compose file")
543550
}
544551

545-
if opts.projectName == "" {
552+
if opts.projectName == "" && !opts.SkipValidation {
546553
return nil, errors.New("project name must not be empty")
547554
}
548555

loader/loader_test.go

+22
Original file line numberDiff line numberDiff line change
@@ -940,6 +940,28 @@ services:
940940
assert.NilError(t, err)
941941
}
942942

943+
func TestDiscardExtendsOption(t *testing.T) {
944+
dict := `name: test
945+
services:
946+
base:
947+
image: base
948+
web:
949+
extends: base
950+
`
951+
configDetails := buildConfigDetails(dict, nil)
952+
953+
// Default behavior keeps the `env_file` entries
954+
model, err := LoadWithContext(context.TODO(), configDetails, func(options *Options) {
955+
options.SkipNormalization = true
956+
options.ResolvePaths = false
957+
})
958+
assert.NilError(t, err)
959+
assert.DeepEqual(t, *model.Services["web"].Extends, types.ExtendsConfig{
960+
Service: "base",
961+
})
962+
963+
}
964+
943965
func TestDiscardEnvFileOption(t *testing.T) {
944966
dict := `name: test
945967
services:

0 commit comments

Comments
 (0)