diff --git a/internal/config/template/parser.go b/internal/config/template/parser.go index d4bc213421ee..e61dd15f3af3 100644 --- a/internal/config/template/parser.go +++ b/internal/config/template/parser.go @@ -6,6 +6,7 @@ import ( "io/fs" "os" "path/filepath" + "sort" "strings" "text/template" @@ -227,6 +228,31 @@ func (p *Parser) matchPaths(pattern string) []map[interface{}]interface{} { return nil }) + // sort the matches by dir + sort.Slice(matches, func(i, j int) bool { + // Perform type assertions and handle the case where the key might not exist or the value is not a string + dirI, okI := matches[i]["_dir"].(string) + dirJ, okJ := matches[j]["_dir"].(string) + if !okI || !okJ { + return false // In a real-world scenario, you might want to handle this case differently + } + return dirI < dirJ + }) + + // mark any directories that are leaf directories + for i := 0; i < len(matches); i++ { + if (i+1) >= len(matches) { + matches[i]["_isLeafDir"] = true + continue + } + + currentDir, _ := matches[i]["_dir"].(string) + nextDir, _ := matches[i+1]["_dir"].(string) + if !strings.HasPrefix(nextDir, currentDir) { + matches[i]["_isLeafDir"] = true + } + } + return matches }