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

refacto: improve Docker Compose project name determination #515

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

Merged
Merged
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
34 changes: 30 additions & 4 deletions 34 envs/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,13 @@ import (
"strings"
"time"

compose "github.com/compose-spec/compose-go/cli"
composeConsts "github.com/compose-spec/compose-go/consts"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
docker "github.com/docker/docker/client"
"github.com/symfony-cli/terminal"
"gopkg.in/yaml.v2"
)

var (
Expand Down Expand Up @@ -491,7 +494,7 @@ func getEnvValue(env string, key string) string {

func (l *Local) getComposeProjectName() string {
// https://docs.docker.com/compose/reference/envvars/#compose_project_name
if project := os.Getenv("COMPOSE_PROJECT_NAME"); project != "" {
if project := os.Getenv(composeConsts.ComposeProjectName); project != "" {
return project
}

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

// Compose project name can be set in every Docker Compose file
for index, filename := range compose.DefaultFileNames {
if _, err := os.Stat(filepath.Join(composeDir, filename)); err != nil {
continue
}

for _, filename := range []string{compose.DefaultOverrideFileNames[index], filename} {
buf, err := os.ReadFile(filepath.Join(composeDir, filename))
if err != nil {
continue
}

config := struct {
ProjectName string `yaml:"name"`
}{}

// unmarshall the content of the file to get the project name
if err := yaml.Unmarshal(buf, &config); err == nil && config.ProjectName != "" {
return config.ProjectName
}
}
}

return filepath.Base(composeDir)
}

Expand All @@ -526,7 +552,7 @@ func (l *Local) getComposeDir() string {
// look for the first dir up with a docker-composer.ya?ml file (in case of a multi-project)
dir := l.Dir
for {
for _, filename := range []string{"compose.yaml", "compose.yml", "docker-compose.yaml", "docker-compose.yml"} {
for _, filename := range compose.DefaultFileNames {
if _, err := os.Stat(filepath.Join(dir, filename)); err == nil {
return dir
}
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.