mirror of
https://github.com/containerd/containerd.git
synced 2026-08-11 01:26:26 +00:00
Merge pull request #9834 from neoaggelos/fix/config-relative
[release/1.7] Fix config import relative path glob
This commit is contained in:
@@ -253,13 +253,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 {
|
||||
@@ -268,11 +273,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)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user