Skip to content

Commit

Permalink
enhance: mark leaf dirs for matched paths
Browse files Browse the repository at this point in the history
  • Loading branch information
aliscott committed Nov 15, 2023
1 parent 2eb9d68 commit d5f9488
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions internal/config/template/parser.go
Expand Up @@ -6,6 +6,7 @@ import (
"io/fs"
"os"
"path/filepath"
"sort"
"strings"
"text/template"

Expand Down Expand Up @@ -227,6 +228,30 @@ func (p *Parser) matchPaths(pattern string) []map[interface{}]interface{} {
return nil
})

// sort the matches by dir
sort.Slice(matches, func(i, j int) bool {
dirI, okI := matches[i]["_dir"].(string)
dirJ, okJ := matches[j]["_dir"].(string)
if !okI || !okJ {
return false
}
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
}

Expand Down

0 comments on commit d5f9488

Please sign in to comment.