Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[release/1.7] Fix config import relative path glob #9834

Merged
merged 1 commit into from
Apr 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 6 additions & 6 deletions services/server/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)
}
}
Expand Down
11 changes: 11 additions & 0 deletions services/server/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down