Skip to content

Commit

Permalink
always test to load config with localFileLoader
Browse files Browse the repository at this point in the history
and pass workdir to LoadConfigFiles func to setup ConfigDetails

Signed-off-by: Guillaume Lours <705411+glours@users.noreply.github.com>
  • Loading branch information
glours committed Oct 24, 2024
1 parent c558adc commit 74c1d59
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 11 deletions.
6 changes: 3 additions & 3 deletions cli/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -404,8 +404,8 @@ func (o *ProjectOptions) GetWorkingDir() (string, error) {
}

// ReadConfigFiles reads ConfigFiles and populates the content field
func (o ProjectOptions) ReadConfigFiles(ctx context.Context, options *ProjectOptions) (*types.ConfigDetails, error) {
config, err := loader.LoadConfigFiles(ctx, options.ConfigPaths, options.loadOptions...)
func (o *ProjectOptions) ReadConfigFiles(ctx context.Context, workingDir string, options *ProjectOptions) (*types.ConfigDetails, error) {
config, err := loader.LoadConfigFiles(ctx, options.ConfigPaths, workingDir, options.loadOptions...)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -477,7 +477,7 @@ func (o *ProjectOptions) prepare(ctx context.Context) (*types.ConfigDetails, err
return &types.ConfigDetails{}, err
}

configDetails, err := o.ReadConfigFiles(ctx, o)
configDetails, err := o.ReadConfigFiles(ctx, defaultDir, o)
if err != nil {
return configDetails, err
}
Expand Down
20 changes: 12 additions & 8 deletions loader/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
"strings"

"github.com/compose-spec/compose-go/v2/consts"
"github.com/compose-spec/compose-go/v2/errdefs"
interp "github.com/compose-spec/compose-go/v2/interpolation"
"github.com/compose-spec/compose-go/v2/override"
"github.com/compose-spec/compose-go/v2/paths"
Expand Down Expand Up @@ -139,9 +140,9 @@ func (l localResourceLoader) abs(p string) string {
return filepath.Join(l.WorkingDir, p)
}

func (l localResourceLoader) Accept(p string) bool {
_, err := os.Stat(l.abs(p))
return err == nil
func (l localResourceLoader) Accept(_ string) bool {
// LocalResourceLoader is the last loader tested so it always should accept the config and try to get the content.
return true
}

func (l localResourceLoader) Load(_ context.Context, p string) (string, error) {
Expand Down Expand Up @@ -301,9 +302,9 @@ func parseYAML(decoder *yaml.Decoder) (map[string]interface{}, PostProcessor, er
}

// LoadConfigFiles ingests config files with ResourceLoader and returns config details with paths to local copies
func LoadConfigFiles(ctx context.Context, configFiles []string, options ...func(*Options)) (*types.ConfigDetails, error) {
func LoadConfigFiles(ctx context.Context, configFiles []string, workingDir string, options ...func(*Options)) (*types.ConfigDetails, error) {
if len(configFiles) < 1 {
return &types.ConfigDetails{}, errors.New("no files specified")
return &types.ConfigDetails{}, fmt.Errorf("no configuration file provided: %w", errdefs.ErrNotFound)
}

opts := &Options{}
Expand All @@ -318,16 +319,16 @@ func LoadConfigFiles(ctx context.Context, configFiles []string, options ...func(

for i, p := range configFiles {
for _, loader := range opts.ResourceLoaders {
_, isLocalResourceLoader := loader.(localResourceLoader)
if !loader.Accept(p) {
continue
}
local, err := loader.Load(ctx, p)
if err != nil {
continue
return nil, err
}
if config.WorkingDir == "" {
if config.WorkingDir == "" && !isLocalResourceLoader {
config.WorkingDir = filepath.Dir(local)

}
abs, err := filepath.Abs(local)
if err != nil {
Expand All @@ -339,6 +340,9 @@ func LoadConfigFiles(ctx context.Context, configFiles []string, options ...func(
break
}
}
if config.WorkingDir == "" {
config.WorkingDir = workingDir
}
return config, nil
}

Expand Down

0 comments on commit 74c1d59

Please sign in to comment.