Skip to content

Commit

Permalink
Correctly generate Compose project name if not specified
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobwgillespie committed Jul 24, 2024
1 parent df2c6a6 commit 154a9dd
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions pkg/compose/compose.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ package compose

import (
"errors"
"fmt"
"os"
"path/filepath"
"regexp"
"strings"

"github.com/compose-spec/compose-go/v2/consts"
"github.com/compose-spec/compose-go/v2/dotenv"
"github.com/compose-spec/compose-go/v2/loader"
"github.com/compose-spec/compose-go/v2/types"
Expand Down Expand Up @@ -36,6 +38,7 @@ func TargetTags(files []bake.File) (map[string][]string, error) {
}

if len(configFiles) == 0 {
fmt.Println("no config files")
return nil, nil
}

Expand All @@ -49,6 +52,19 @@ func TargetTags(files []bake.File) (map[string][]string, error) {
Environment: envs,
}
opts := func(options *loader.Options) {
if nameFromEnv, ok := envs[consts.ComposeProjectName]; ok && nameFromEnv != "" {
options.SetProjectName(nameFromEnv, true)
} else {
path, err := filepath.Abs(files[0].Name)
if err != nil {
return
}
absWorkingDir := filepath.Dir(path)
options.SetProjectName(
loader.NormalizeProjectName(filepath.Base(absWorkingDir)),
false,
)
}
options.SkipNormalization = true
}

Expand Down Expand Up @@ -130,6 +146,11 @@ func isComposeFile(file string, content []byte) bool {
}

opts := func(options *loader.Options) {
projectName := "bake"
if v, ok := envs[consts.ComposeProjectName]; ok && v != "" {
projectName = v
}
options.SetProjectName(projectName, false)
options.SkipNormalization = true
options.SkipConsistencyCheck = true
}
Expand Down

0 comments on commit 154a9dd

Please sign in to comment.