Skip to content

Commit 62e9535

Browse files
committed
Fix config import relative path glob
Previously, resolveImports would apply a glob filter if the path contained any '*', or otherwise convert relative paths to absolute. This meant that it was impossible to specify globs with paths relative to the main config file. This commit first resolves relative to absolute paths, then applies the glob filter (if any). A test case is added to ensure that this now works as expected. Signed-off-by: Angelos Kolaitis <[email protected]>
1 parent 7c3aca7 commit 62e9535

2 files changed

Lines changed: 17 additions & 6 deletions

File tree

services/server/config/config.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -251,13 +251,18 @@ func loadConfigFile(path string) (*Config, error) {
251251
}
252252

253253
// resolveImports resolves import strings list to absolute paths list:
254-
// - If path contains *, glob pattern matching applied
255254
// - Non abs path is relative to parent config file directory
255+
// - If path contains *, glob pattern matching applied
256256
// - Abs paths returned as is
257257
func resolveImports(parent string, imports []string) ([]string, error) {
258258
var out []string
259259

260260
for _, path := range imports {
261+
path := filepath.Clean(path)
262+
if !filepath.IsAbs(path) {
263+
path = filepath.Join(filepath.Dir(parent), path)
264+
}
265+
261266
if strings.Contains(path, "*") {
262267
matches, err := filepath.Glob(path)
263268
if err != nil {
@@ -266,11 +271,6 @@ func resolveImports(parent string, imports []string) ([]string, error) {
266271

267272
out = append(out, matches...)
268273
} else {
269-
path = filepath.Clean(path)
270-
if !filepath.IsAbs(path) {
271-
path = filepath.Join(filepath.Dir(parent), path)
272-
}
273-
274274
out = append(out, path)
275275
}
276276
}

services/server/config/config_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,17 @@ func TestResolveImports(t *testing.T) {
9696
filepath.Join(tempDir, "test.toml"),
9797
filepath.Join(tempDir, "current.toml"),
9898
})
99+
100+
t.Run("GlobRelativePath", func(t *testing.T) {
101+
imports, err := resolveImports(filepath.Join(tempDir, "root.toml"), []string{
102+
"config_*.toml", // Glob files from working dir
103+
})
104+
assert.NoError(t, err)
105+
assert.Equal(t, imports, []string{
106+
filepath.Join(tempDir, "config_1.toml"),
107+
filepath.Join(tempDir, "config_2.toml"),
108+
})
109+
})
99110
}
100111

101112
func TestLoadSingleConfig(t *testing.T) {

0 commit comments

Comments
 (0)