Skip to content

Commit

Permalink
let resourceloader compute relative working dir
Browse files Browse the repository at this point in the history
Signed-off-by: Nicolas De Loof <nicolas.deloof@gmail.com>
  • Loading branch information
ndeloof committed Feb 29, 2024
1 parent a0507e9 commit dc6d618
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 24 deletions.
34 changes: 16 additions & 18 deletions loader/include.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ func ApplyInclude(ctx context.Context, configDetails types.ConfigDetails, model
})
}

var relworkingdir string
for i, p := range r.Path {
for _, loader := range options.ResourceLoaders {
if loader.Accept(p) {

Check failure on line 66 in loader/include.go

View workflow job for this annotation

GitHub Actions / test (1.22, ubuntu-latest)

nestingReduce: invert if cond, replace body with `continue`, move old body after the statement (gocritic)

Check failure on line 66 in loader/include.go

View workflow job for this annotation

GitHub Actions / test (1.21, ubuntu-latest)

nestingReduce: invert if cond, replace body with `continue`, move old body after the statement (gocritic)
Expand All @@ -68,35 +69,32 @@ func ApplyInclude(ctx context.Context, configDetails types.ConfigDetails, model
return err
}
p = path

if i == 0 { // This is the "main" file, used to define project-directory. Others are overrides
relworkingdir = loader.Dir(path)
if r.ProjectDirectory == "" {
r.ProjectDirectory = filepath.Dir(path)
}

for _, f := range included {
if f == path {
included = append(included, path)
return fmt.Errorf("include cycle detected:\n%s\n include %s", included[0], strings.Join(included[1:], "\n include "))
}
}
}
break
}
}
r.Path[i] = p
}

mainFile := r.Path[0]
for _, f := range included {
if f == mainFile {
included = append(included, mainFile)
return fmt.Errorf("include cycle detected:\n%s\n include %s", included[0], strings.Join(included[1:], "\n include "))
}
}

if r.ProjectDirectory == "" {
r.ProjectDirectory = filepath.Dir(mainFile)
}
relworkingdir, err := filepath.Rel(configDetails.WorkingDir, r.ProjectDirectory)
if err != nil {
// included file path is not inside project working directory => use absolute path
relworkingdir = r.ProjectDirectory
}

loadOptions := options.clone()
loadOptions.ResolvePaths = true
loadOptions.SkipNormalization = true
loadOptions.SkipConsistencyCheck = true
loadOptions.ResourceLoaders = append(loadOptions.RemoteResourceLoaders(), localResourceLoader{
WorkingDir: relworkingdir,
WorkingDir: r.ProjectDirectory,
})

if len(r.EnvFile) == 0 {
Expand Down
25 changes: 21 additions & 4 deletions loader/include_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,21 @@ import (
"gotest.tools/v3/assert"
)

func TestLoadIncludeExtendsCombined(t *testing.T) {
_, err := LoadWithContext(context.Background(), types.ConfigDetails{
WorkingDir: "testdata/combined",
ConfigFiles: []types.ConfigFile{
{
Filename: "testdata/combined/compose.yaml",
},
},
}, withProjectName("test-load-combined", true))
assert.NilError(t, err)
}

func TestLoadWithMultipleInclude(t *testing.T) {
// include same service twice should not trigger an error
p, err := Load(buildConfigDetails(`
details := buildConfigDetails(`
name: 'test-multi-include'
include:
Expand All @@ -41,17 +53,21 @@ services:
image: busybox
depends_on:
- imported
`, map[string]string{"SOURCE": "override"}), func(options *Options) {
`, map[string]string{"SOURCE": "override"})

p, err := Load(details, func(options *Options) {
options.SkipNormalization = true
options.ResolvePaths = true
})
assert.NilError(t, err)
imported, err := p.GetService("imported")
assert.NilError(t, err)
assert.Equal(t, imported.ContainerName, "override")
}

func TestLoadWithMultipleIncludeConflict(t *testing.T) {
// include 2 different services with same name should trigger an error
_, err = Load(buildConfigDetails(`
details := buildConfigDetails(`
name: 'test-multi-include'
include:
Expand All @@ -64,7 +80,8 @@ include:
services:
bar:
image: busybox
`, map[string]string{"SOURCE": "override"}), func(options *Options) {
`, map[string]string{"SOURCE": "override"})
_, err := Load(details, func(options *Options) {
options.SkipNormalization = true
options.ResolvePaths = true
})
Expand Down
3 changes: 1 addition & 2 deletions loader/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,7 @@ func (l localResourceLoader) abs(p string) string {
}

func (l localResourceLoader) Accept(p string) bool {

Check warning on line 132 in loader/loader.go

View workflow job for this annotation

GitHub Actions / test (1.22, ubuntu-latest)

unused-parameter: parameter 'p' seems to be unused, consider removing or renaming it as _ (revive)

Check warning on line 132 in loader/loader.go

View workflow job for this annotation

GitHub Actions / test (1.21, ubuntu-latest)

unused-parameter: parameter 'p' seems to be unused, consider removing or renaming it as _ (revive)
_, err := os.Stat(l.abs(p))
return err == nil
return true
}

func (l localResourceLoader) Load(_ context.Context, p string) (string, error) {
Expand Down
3 changes: 3 additions & 0 deletions loader/testdata/combined/compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
include:
- path:
- dir/included.yaml
3 changes: 3 additions & 0 deletions loader/testdata/combined/dir/extended.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
services:
service:
build: .
5 changes: 5 additions & 0 deletions loader/testdata/combined/dir/included.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
services:
service:
extends:
file: extended.yaml
service: service

0 comments on commit dc6d618

Please sign in to comment.