Skip to content

Commit 903639a

Browse files
committed
refacto: improve Docker Compose project name determination
By using the library constants and reading the yaml files
1 parent 799c80f commit 903639a

File tree

1 file changed

+30
-4
lines changed

1 file changed

+30
-4
lines changed

envs/docker.go

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,13 @@ import (
3232
"strings"
3333
"time"
3434

35+
compose "github.com/compose-spec/compose-go/cli"
36+
composeConsts "github.com/compose-spec/compose-go/consts"
3537
"github.com/docker/docker/api/types"
3638
"github.com/docker/docker/api/types/container"
3739
docker "github.com/docker/docker/client"
3840
"github.com/symfony-cli/terminal"
41+
"gopkg.in/yaml.v2"
3942
)
4043

4144
var (
@@ -491,7 +494,7 @@ func getEnvValue(env string, key string) string {
491494

492495
func (l *Local) getComposeProjectName() string {
493496
// https://docs.docker.com/compose/reference/envvars/#compose_project_name
494-
if project := os.Getenv("COMPOSE_PROJECT_NAME"); project != "" {
497+
if project := os.Getenv(composeConsts.ComposeProjectName); project != "" {
495498
return project
496499
}
497500

@@ -507,13 +510,36 @@ func (l *Local) getComposeProjectName() string {
507510
if _, err := os.Stat(filepath.Join(composeDir, ".env")); err == nil {
508511
if contents, err := os.ReadFile(filepath.Join(composeDir, ".env")); err == nil {
509512
for _, line := range bytes.Split(contents, []byte("\n")) {
510-
if bytes.HasPrefix(line, []byte("COMPOSE_PROJECT_NAME=")) {
511-
return string(line[len("COMPOSE_PROJECT_NAME="):])
513+
if bytes.HasPrefix(line, []byte(composeConsts.ComposeProjectName+"=")) {
514+
return string(line[len(composeConsts.ComposeProjectName)+1:])
512515
}
513516
}
514517
}
515518
}
516519

520+
// Compose project name can be set in every Docker Compose file
521+
for index, filename := range compose.DefaultFileNames {
522+
if _, err := os.Stat(filepath.Join(composeDir, filename)); err != nil {
523+
continue
524+
}
525+
526+
for _, filename := range []string{compose.DefaultOverrideFileNames[index], filename} {
527+
buf, err := os.ReadFile(filepath.Join(composeDir, filename))
528+
if err != nil {
529+
continue
530+
}
531+
532+
config := struct {
533+
ProjectName string `yaml:"name"`
534+
}{}
535+
536+
// unmarshall the content of the file to get the project name
537+
if err := yaml.Unmarshal(buf, &config); err == nil && config.ProjectName != "" {
538+
return config.ProjectName
539+
}
540+
}
541+
}
542+
517543
return filepath.Base(composeDir)
518544
}
519545

@@ -526,7 +552,7 @@ func (l *Local) getComposeDir() string {
526552
// look for the first dir up with a docker-composer.ya?ml file (in case of a multi-project)
527553
dir := l.Dir
528554
for {
529-
for _, filename := range []string{"compose.yaml", "compose.yml", "docker-compose.yaml", "docker-compose.yml"} {
555+
for _, filename := range compose.DefaultFileNames {
530556
if _, err := os.Stat(filepath.Join(dir, filename)); err == nil {
531557
return dir
532558
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy