Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit f0449e0

Browse filesBrowse files
committed
refacto: improve Docker Compose project name determination
By using the library constants and reading the yaml files
1 parent 37bdf46 commit f0449e0
Copy full SHA for f0449e0

File tree

1 file changed

+30
-4
lines changed
Filter options

1 file changed

+30
-4
lines changed

‎envs/docker.go

Copy file name to clipboardExpand all lines: envs/docker.go
+30-4Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"bytes"
2424
"context"
2525
"fmt"
26+
"gopkg.in/yaml.v2"
2627
"net"
2728
"net/url"
2829
"os"
@@ -33,6 +34,8 @@ import (
3334
"strings"
3435
"time"
3536

37+
compose "github.com/compose-spec/compose-go/cli"
38+
composeConsts "github.com/compose-spec/compose-go/consts"
3639
"github.com/docker/docker/api/types"
3740
"github.com/docker/docker/api/types/container"
3841
docker "github.com/docker/docker/client"
@@ -483,7 +486,7 @@ func getEnvValue(env string, key string) string {
483486

484487
func (l *Local) getComposeProjectName() string {
485488
// https://docs.docker.com/compose/reference/envvars/#compose_project_name
486-
if project := os.Getenv("COMPOSE_PROJECT_NAME"); project != "" {
489+
if project := os.Getenv(composeConsts.ComposeProjectName); project != "" {
487490
return project
488491
}
489492

@@ -499,13 +502,36 @@ func (l *Local) getComposeProjectName() string {
499502
if _, err := os.Stat(filepath.Join(composeDir, ".env")); err == nil {
500503
if contents, err := os.ReadFile(filepath.Join(composeDir, ".env")); err == nil {
501504
for _, line := range bytes.Split(contents, []byte("\n")) {
502-
if bytes.HasPrefix(line, []byte("COMPOSE_PROJECT_NAME=")) {
503-
return string(line[len("COMPOSE_PROJECT_NAME="):])
505+
if bytes.HasPrefix(line, []byte(composeConsts.ComposeProjectName+"=")) {
506+
return string(line[len(composeConsts.ComposeProjectName)+1:])
504507
}
505508
}
506509
}
507510
}
508511

512+
// Compose project name can be set in every Docker Compose file
513+
for index, filename := range compose.DefaultFileNames {
514+
if _, err := os.Stat(filepath.Join(composeDir, filename)); err != nil {
515+
continue
516+
}
517+
518+
for _, filename := range []string{compose.DefaultOverrideFileNames[index], filename} {
519+
buf, err := os.ReadFile(filepath.Join(composeDir, filename))
520+
if err != nil {
521+
continue
522+
}
523+
524+
config := struct {
525+
ProjectName string `yaml:"name"`
526+
}{}
527+
528+
// unmarshall the content of the file to get the project name
529+
if err := yaml.Unmarshal(buf, &config); err == nil && config.ProjectName != "" {
530+
return config.ProjectName
531+
}
532+
}
533+
}
534+
509535
return filepath.Base(composeDir)
510536
}
511537

@@ -518,7 +544,7 @@ func (l *Local) getComposeDir() string {
518544
// look for the first dir up with a docker-composer.ya?ml file (in case of a multi-project)
519545
dir := l.Dir
520546
for {
521-
for _, filename := range []string{"compose.yaml", "compose.yml", "docker-compose.yaml", "docker-compose.yml"} {
547+
for _, filename := range compose.DefaultFileNames {
522548
if _, err := os.Stat(filepath.Join(dir, filename)); err == nil {
523549
return dir
524550
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.