@@ -21,9 +21,9 @@ import (
2121 "io/ioutil"
2222 "os"
2323 "path/filepath"
24- "regexp"
2524 "strings"
2625
26+ "github.com/compose-spec/compose-go/consts"
2727 "github.com/compose-spec/compose-go/dotenv"
2828 "github.com/compose-spec/compose-go/errdefs"
2929 "github.com/compose-spec/compose-go/loader"
@@ -87,11 +87,11 @@ func WithConfigFileEnv(o *ProjectOptions) error {
8787 if len (o .ConfigPaths ) > 0 {
8888 return nil
8989 }
90- sep := o .Environment [ComposePathSeparator ]
90+ sep := o .Environment [consts . ComposePathSeparator ]
9191 if sep == "" {
9292 sep = string (os .PathListSeparator )
9393 }
94- f , ok := o .Environment [ComposeFilePath ]
94+ f , ok := o .Environment [consts . ComposeFilePath ]
9595 if ok {
9696 paths , err := absolutePaths (strings .Split (f , sep ))
9797 o .ConfigPaths = paths
@@ -276,12 +276,6 @@ var DefaultFileNames = []string{"compose.yaml", "compose.yml", "docker-compose.y
276276// DefaultOverrideFileNames defines the Compose override file names for auto-discovery (in order of preference)
277277var DefaultOverrideFileNames = []string {"compose.override.yml" , "compose.override.yaml" , "docker-compose.override.yml" , "docker-compose.override.yaml" }
278278
279- const (
280- ComposeProjectName = "COMPOSE_PROJECT_NAME"
281- ComposePathSeparator = "COMPOSE_PATH_SEPARATOR"
282- ComposeFilePath = "COMPOSE_FILE"
283- )
284-
285279func (o ProjectOptions ) GetWorkingDir () (string , error ) {
286280 if o .WorkingDir != "" {
287281 return o .WorkingDir , nil
@@ -338,17 +332,7 @@ func ProjectFromOptions(options *ProjectOptions) (*types.Project, error) {
338332 return nil , err
339333 }
340334
341- var nameLoadOpt = func (opts * loader.Options ) {
342- if options .Name != "" {
343- opts .Name = options .Name
344- } else if nameFromEnv , ok := options .Environment [ComposeProjectName ]; ok && nameFromEnv != "" {
345- opts .Name = nameFromEnv
346- } else {
347- opts .Name = filepath .Base (absWorkingDir )
348- }
349- opts .Name = normalizeName (opts .Name )
350- }
351- options .loadOptions = append (options .loadOptions , nameLoadOpt )
335+ options .loadOptions = append (options .loadOptions , withNamePrecedenceLoad (absWorkingDir , options ))
352336
353337 project , err := loader .Load (types.ConfigDetails {
354338 ConfigFiles : configs ,
@@ -363,11 +347,16 @@ func ProjectFromOptions(options *ProjectOptions) (*types.Project, error) {
363347 return project , nil
364348}
365349
366- func normalizeName (s string ) string {
367- r := regexp .MustCompile ("[a-z0-9_-]" )
368- s = strings .ToLower (s )
369- s = strings .Join (r .FindAllString (s , - 1 ), "" )
370- return strings .TrimLeft (s , "_-" )
350+ func withNamePrecedenceLoad (absWorkingDir string , options * ProjectOptions ) func (* loader.Options ) {
351+ return func (opts * loader.Options ) {
352+ if options .Name != "" {
353+ opts .SetProjectName (options .Name , true )
354+ } else if nameFromEnv , ok := options .Environment [consts .ComposeProjectName ]; ok && nameFromEnv != "" {
355+ opts .SetProjectName (nameFromEnv , true )
356+ } else {
357+ opts .SetProjectName (filepath .Base (absWorkingDir ), false )
358+ }
359+ }
371360}
372361
373362// getConfigPathsFromOptions retrieves the config files for project based on project options
0 commit comments