Go: Allow FindAllFilesWithName to use predicate functions for dirsToSkip

This commit is contained in:
Michael B. Gale
2024-11-04 12:16:41 +00:00
parent 2cd9bd8a43
commit b372af51b6
2 changed files with 15 additions and 6 deletions

View File

@@ -184,12 +184,12 @@ func RemoveTemporaryExtractorFiles() {
// Find all go.work files in the working directory and its subdirectories
func findGoWorkFiles() []string {
return util.FindAllFilesWithName(".", "go.work", "vendor")
return util.FindAllFilesWithName(".", "go.work", util.SkipVendorChecks...)
}
// Find all go.mod files in the specified directory and its subdirectories
func findGoModFiles(root string) []string {
return util.FindAllFilesWithName(root, "go.mod", "vendor")
return util.FindAllFilesWithName(root, "go.mod", util.SkipVendorChecks...)
}
// A regular expression for the Go toolchain version syntax.
@@ -547,8 +547,8 @@ func startsWithAnyOf(str string, prefixes []string) bool {
// Finds Go workspaces in the current working directory.
func GetWorkspaceInfo(emitDiagnostics bool) []GoWorkspace {
bazelPaths := slices.Concat(
util.FindAllFilesWithName(".", "BUILD", "vendor"),
util.FindAllFilesWithName(".", "BUILD.bazel", "vendor"),
util.FindAllFilesWithName(".", "BUILD", util.SkipVendorChecks...),
util.FindAllFilesWithName(".", "BUILD.bazel", util.SkipVendorChecks...),
)
if len(bazelPaths) > 0 {
// currently not supported

View File

@@ -152,7 +152,16 @@ func FindGoFiles(root string) bool {
return found
}
func FindAllFilesWithName(root string, name string, dirsToSkip ...string) []string {
// The type of check function used by `FindAllFilesWithName` to decide whether to skip the directory named by `path`.
type FindAllFilesWithNameSkipCheck func(path string) bool
// Commonly we only want to skip `vendor` directories in `FindAllFilesWithName`. This array is a suitable
// argument for `dirsToSkip` which skips `vendor` directories.
var SkipVendorChecks = []FindAllFilesWithNameSkipCheck{IsGolangVendorDirectory}
// Returns an array of all files matching `name` within the path at `root`.
// The `dirsToSkip` array contains check functions used to decide which directories to skip.
func FindAllFilesWithName(root string, name string, dirsToSkip ...FindAllFilesWithNameSkipCheck) []string {
paths := make([]string, 0, 1)
filepath.WalkDir(root, func(path string, d fs.DirEntry, err error) error {
if err != nil {
@@ -160,7 +169,7 @@ func FindAllFilesWithName(root string, name string, dirsToSkip ...string) []stri
}
if d.IsDir() {
for _, dirToSkip := range dirsToSkip {
if path == dirToSkip {
if dirToSkip(path) {
return filepath.SkipDir
}
}