github.com/inturn/pre-commit-gobuild@v1.0.12/internal/helpers/file.go (about)

     1  package helpers
     2  
     3  import (
     4  	"os"
     5  	"path/filepath"
     6  	"regexp"
     7  )
     8  
     9  // DirsWith finds all the directories in the root (including the root) containing files matching regex
    10  func DirsWith(root, mask string) []string {
    11  	var dirs []string
    12  	filepath.Walk(root, func(path string, f os.FileInfo, _ error) error {
    13  		if !f.IsDir() {
    14  			r, err := regexp.MatchString(mask, f.Name())
    15  			if err == nil && r {
    16  				d := filepath.Dir(path)
    17  				if !Contains(dirs, d) {
    18  					dirs = append(dirs, filepath.Dir(path))
    19  				}
    20  			}
    21  		}
    22  		return nil
    23  	})
    24  	return dirs
    25  }