Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Always set COMPOSE_PROJECT_NAME #628

Merged
merged 1 commit into from
May 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 9 additions & 13 deletions loader/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -329,19 +329,11 @@ func loadModelWithContext(ctx context.Context, configDetails *types.ConfigDetail
return nil, errors.New("No files specified")
}

err := projectName(*configDetails, opts)
err := projectName(configDetails, opts)
if err != nil {
return nil, err
}

// TODO(milas): this should probably ALWAYS set (overriding any existing)
if _, ok := configDetails.Environment[consts.ComposeProjectName]; !ok && opts.projectName != "" {
if configDetails.Environment == nil {
configDetails.Environment = map[string]string{}
}
configDetails.Environment[consts.ComposeProjectName] = opts.projectName
}

return load(ctx, *configDetails, opts, nil)
}

Expand Down Expand Up @@ -601,10 +593,14 @@ func InvalidProjectNameErr(v string) error {
// projectName determines the canonical name to use for the project considering
// the loader Options as well as `name` fields in Compose YAML fields (which
// also support interpolation).
//
// TODO(milas): restructure loading so that we don't need to re-parse the YAML
// here, as it's both wasteful and makes this code error-prone.
func projectName(details types.ConfigDetails, opts *Options) error {
func projectName(details *types.ConfigDetails, opts *Options) error {
defer func() {
if details.Environment == nil {
details.Environment = map[string]string{}
}
details.Environment[consts.ComposeProjectName] = opts.projectName
}()

if opts.projectNameImperativelySet {
if NormalizeProjectName(opts.projectName) != opts.projectName {
return InvalidProjectNameErr(opts.projectName)
Expand Down
11 changes: 6 additions & 5 deletions loader/loader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2376,20 +2376,21 @@ services:
assert.Equal(t, "overrided-proxy", svc.ContainerName)
})

t.Run("project name env variable interpolation", func(t *testing.T) {
t.Run("project name override", func(t *testing.T) {
yaml := `
name: interpolated
name: another_name
services:
web:
image: web
container_name: ${COMPOSE_PROJECT_NAME}-web
`
configDetails := buildConfigDetails(yaml, map[string]string{"COMPOSE_PROJECT_NAME": "env-var"})
actual, err := LoadWithContext(context.TODO(), configDetails)
configDetails := buildConfigDetails(yaml, map[string]string{})

actual, err := Load(configDetails, withProjectName("interpolated", true))
assert.NilError(t, err)
svc, err := actual.GetService("web")
assert.NilError(t, err)
assert.Equal(t, "env-var-web", svc.ContainerName)
assert.Equal(t, "interpolated-web", svc.ContainerName)
})
}

Expand Down