From 62e9535f295f4026d69280107c08c6b6a4eb5417 Mon Sep 17 00:00:00 2001 From: Angelos Kolaitis Date: Sat, 3 Feb 2024 15:07:31 +0200 Subject: [PATCH] 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 --- services/server/config/config.go | 12 ++++++------ services/server/config/config_test.go | 11 +++++++++++ 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/services/server/config/config.go b/services/server/config/config.go index 9a6f39d432..970daba67f 100644 --- a/services/server/config/config.go +++ b/services/server/config/config.go @@ -251,13 +251,18 @@ func loadConfigFile(path string) (*Config, error) { } // resolveImports resolves import strings list to absolute paths list: -// - If path contains *, glob pattern matching applied // - Non abs path is relative to parent config file directory +// - If path contains *, glob pattern matching applied // - Abs paths returned as is func resolveImports(parent string, imports []string) ([]string, error) { var out []string for _, path := range imports { + path := filepath.Clean(path) + if !filepath.IsAbs(path) { + path = filepath.Join(filepath.Dir(parent), path) + } + if strings.Contains(path, "*") { matches, err := filepath.Glob(path) if err != nil { @@ -266,11 +271,6 @@ func resolveImports(parent string, imports []string) ([]string, error) { out = append(out, matches...) } else { - path = filepath.Clean(path) - if !filepath.IsAbs(path) { - path = filepath.Join(filepath.Dir(parent), path) - } - out = append(out, path) } } diff --git a/services/server/config/config_test.go b/services/server/config/config_test.go index a589ecc4c9..18da5a3d2b 100644 --- a/services/server/config/config_test.go +++ b/services/server/config/config_test.go @@ -96,6 +96,17 @@ func TestResolveImports(t *testing.T) { filepath.Join(tempDir, "test.toml"), filepath.Join(tempDir, "current.toml"), }) + + t.Run("GlobRelativePath", func(t *testing.T) { + imports, err := resolveImports(filepath.Join(tempDir, "root.toml"), []string{ + "config_*.toml", // Glob files from working dir + }) + assert.NoError(t, err) + assert.Equal(t, imports, []string{ + filepath.Join(tempDir, "config_1.toml"), + filepath.Join(tempDir, "config_2.toml"), + }) + }) } func TestLoadSingleConfig(t *testing.T) {