Skip to content

Commit 74c1d59

Browse files
committed
always test to load config with localFileLoader
and pass workdir to LoadConfigFiles func to setup ConfigDetails Signed-off-by: Guillaume Lours <[email protected]>
1 parent c558adc commit 74c1d59

File tree

2 files changed

+15
-11
lines changed

2 files changed

+15
-11
lines changed

cli/options.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -404,8 +404,8 @@ func (o *ProjectOptions) GetWorkingDir() (string, error) {
404404
}
405405

406406
// ReadConfigFiles reads ConfigFiles and populates the content field
407-
func (o ProjectOptions) ReadConfigFiles(ctx context.Context, options *ProjectOptions) (*types.ConfigDetails, error) {
408-
config, err := loader.LoadConfigFiles(ctx, options.ConfigPaths, options.loadOptions...)
407+
func (o *ProjectOptions) ReadConfigFiles(ctx context.Context, workingDir string, options *ProjectOptions) (*types.ConfigDetails, error) {
408+
config, err := loader.LoadConfigFiles(ctx, options.ConfigPaths, workingDir, options.loadOptions...)
409409
if err != nil {
410410
return nil, err
411411
}
@@ -477,7 +477,7 @@ func (o *ProjectOptions) prepare(ctx context.Context) (*types.ConfigDetails, err
477477
return &types.ConfigDetails{}, err
478478
}
479479

480-
configDetails, err := o.ReadConfigFiles(ctx, o)
480+
configDetails, err := o.ReadConfigFiles(ctx, defaultDir, o)
481481
if err != nil {
482482
return configDetails, err
483483
}

loader/loader.go

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import (
3030
"strings"
3131

3232
"github.com/compose-spec/compose-go/v2/consts"
33+
"github.com/compose-spec/compose-go/v2/errdefs"
3334
interp "github.com/compose-spec/compose-go/v2/interpolation"
3435
"github.com/compose-spec/compose-go/v2/override"
3536
"github.com/compose-spec/compose-go/v2/paths"
@@ -139,9 +140,9 @@ func (l localResourceLoader) abs(p string) string {
139140
return filepath.Join(l.WorkingDir, p)
140141
}
141142

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

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

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

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

319320
for i, p := range configFiles {
320321
for _, loader := range opts.ResourceLoaders {
322+
_, isLocalResourceLoader := loader.(localResourceLoader)
321323
if !loader.Accept(p) {
322324
continue
323325
}
324326
local, err := loader.Load(ctx, p)
325327
if err != nil {
326-
continue
328+
return nil, err
327329
}
328-
if config.WorkingDir == "" {
330+
if config.WorkingDir == "" && !isLocalResourceLoader {
329331
config.WorkingDir = filepath.Dir(local)
330-
331332
}
332333
abs, err := filepath.Abs(local)
333334
if err != nil {
@@ -339,6 +340,9 @@ func LoadConfigFiles(ctx context.Context, configFiles []string, options ...func(
339340
break
340341
}
341342
}
343+
if config.WorkingDir == "" {
344+
config.WorkingDir = workingDir
345+
}
342346
return config, nil
343347
}
344348

0 commit comments

Comments
 (0)