From e16daf2bee61973c3a404d56a51da798f0191eb0 Mon Sep 17 00:00:00 2001 From: Guillaume Lours <705411+glours@users.noreply.github.com> Date: Tue, 22 Oct 2024 17:26:44 +0200 Subject: [PATCH] always test to load config with localFileLoader and pass workdir to LoadConfigFiles func to setup ConfigDetails Signed-off-by: Guillaume Lours <705411+glours@users.noreply.github.com> --- cli/options.go | 6 +++--- loader/loader.go | 20 ++++++++++++-------- 2 files changed, 15 insertions(+), 11 deletions(-) diff --git a/cli/options.go b/cli/options.go index 71a15cf5..b2ab60e8 100644 --- a/cli/options.go +++ b/cli/options.go @@ -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 } @@ -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 } diff --git a/loader/loader.go b/loader/loader.go index c17d96f7..26e9d4d0 100644 --- a/loader/loader.go +++ b/loader/loader.go @@ -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" @@ -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) { @@ -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{} @@ -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 { @@ -339,6 +340,9 @@ func LoadConfigFiles(ctx context.Context, configFiles []string, options ...func( break } } + if config.WorkingDir == "" { + config.WorkingDir = workingDir + } return config, nil }