Skip to content

Commit 84fa55e

Browse files
ndeloofglours
authored andcommitted
name set in yaml is equivalent to --project-name
Signed-off-by: Nicolas De Loof <[email protected]>
1 parent 9642b85 commit 84fa55e

File tree

1 file changed

+23
-3
lines changed

1 file changed

+23
-3
lines changed

cli/options.go

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
"strings"
2727

2828
"github.com/sirupsen/logrus"
29+
"gopkg.in/yaml.v3"
2930

3031
"github.com/compose-spec/compose-go/v2/consts"
3132
"github.com/compose-spec/compose-go/v2/dotenv"
@@ -482,8 +483,27 @@ func (o *ProjectOptions) prepare(ctx context.Context) (*types.ConfigDetails, err
482483
return configDetails, err
483484
}
484485

486+
isNamed := false
487+
if o.Name == "" {
488+
type named struct {
489+
Name string `yaml:"name,omitempty"`
490+
}
491+
// if any of the compose file is named, this is equivalent to user passing --project-name
492+
for _, cfg := range configDetails.ConfigFiles {
493+
var n named
494+
err = yaml.Unmarshal(cfg.Content, &n)
495+
if err != nil {
496+
return nil, err
497+
}
498+
if n.Name != "" {
499+
isNamed = true
500+
break
501+
}
502+
}
503+
}
504+
485505
o.loadOptions = append(o.loadOptions,
486-
withNamePrecedenceLoad(defaultDir, o),
506+
withNamePrecedenceLoad(defaultDir, isNamed, o),
487507
withConvertWindowsPaths(o),
488508
withListeners(o))
489509

@@ -496,13 +516,13 @@ func ProjectFromOptions(ctx context.Context, options *ProjectOptions) (*types.Pr
496516
return options.LoadProject(ctx)
497517
}
498518

499-
func withNamePrecedenceLoad(absWorkingDir string, options *ProjectOptions) func(*loader.Options) {
519+
func withNamePrecedenceLoad(absWorkingDir string, namedInYaml bool, options *ProjectOptions) func(*loader.Options) {
500520
return func(opts *loader.Options) {
501521
if options.Name != "" {
502522
opts.SetProjectName(options.Name, true)
503523
} else if nameFromEnv, ok := options.Environment[consts.ComposeProjectName]; ok && nameFromEnv != "" {
504524
opts.SetProjectName(nameFromEnv, true)
505-
} else {
525+
} else if !namedInYaml {
506526
dirname := filepath.Base(absWorkingDir)
507527
symlink, err := filepath.EvalSymlinks(absWorkingDir)
508528
if err == nil && filepath.Base(symlink) != dirname {

0 commit comments

Comments
 (0)