Skip to content

Commit f0df7cd

Browse files
committed
Go: Add GoFilesOutsideDirs function
1 parent d4ea45b commit f0df7cd

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

go/extractor/util/util.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,28 @@ func AnyGoFilesOutsideDirs(root string, dirsToSkip ...string) bool {
340340
return found
341341
}
342342

343+
// Returns an array of any Go source files in locations which do not have a Go.mod
344+
// file in the same directory or higher up in the file hierarchy, relative to the `root`.
345+
func GoFilesOutsideDirs(root string, dirsToSkip ...string) []string {
346+
result := []string{}
347+
348+
filepath.WalkDir(root, func(path string, d fs.DirEntry, err error) error {
349+
if err != nil {
350+
return err
351+
}
352+
if d.IsDir() && slices.Contains(dirsToSkip, path) {
353+
return filepath.SkipDir
354+
}
355+
if filepath.Ext(d.Name()) == ".go" {
356+
log.Printf("Found stray Go source file in %s.\n", path)
357+
result = append(result, path)
358+
}
359+
return nil
360+
})
361+
362+
return result
363+
}
364+
343365
// For every file path in the input array, return the parent directory.
344366
func GetParentDirs(paths []string) []string {
345367
dirs := make([]string, len(paths))

0 commit comments

Comments
 (0)