Skip to content

Commit

Permalink
Keep only walkFilesByExts
Browse files Browse the repository at this point in the history
Keeping a single implementation avoid walking the filesystem multiple times.
  • Loading branch information
rykov authored and Guilhem Bonnefille committed Nov 4, 2024
1 parent 8685c36 commit 21dee61
Showing 1 changed file with 2 additions and 12 deletions.
14 changes: 2 additions & 12 deletions config/fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package config
import (
"os"
"path/filepath"
"slices"

"github.com/spf13/afero"
)
Expand Down Expand Up @@ -71,24 +72,13 @@ func (fs *Fs) WalkLists(walkFn func(path, key string, fi os.FileInfo, err error)

// Iteration helper to find all files with multiple possible extensions in a directory
func (fs *Fs) walkFilesByExts(dir string, exts []string, walkFn func(path, key string, fi os.FileInfo, err error)) error {
for _, e := range exts {
err := fs.walkFilesByExt(dir, e, walkFn)
if err != nil {
return err
}
}
return nil
}

// Iteration helper to find all files with a certain extension in a directory
func (fs *Fs) walkFilesByExt(dir, ext string, walkFn func(path, key string, fi os.FileInfo, err error)) error {
return afero.Walk(fs, dir, func(path string, fi os.FileInfo, err error) error {
if err != nil || fi.IsDir() {
return nil
}

pathExt := filepath.Ext(path)
if pathExt != ext {
if !slices.Contains(exts, pathExt) {
return nil
}

Expand Down

0 comments on commit 21dee61

Please sign in to comment.