GetFilesWithSuffix returns all files under a given base directory that have a specific suffix The operation is performed recursively on subdirectories as well
(baseDir string, suffixes ...string)
| 33 | // GetFilesWithSuffix returns all files under a given base directory that have a specific suffix |
| 34 | // The operation is performed recursively on subdirectories as well |
| 35 | func GetFilesWithSuffix(baseDir string, suffixes ...string) ([]string, error) { |
| 36 | var files []string |
| 37 | err := filepath.Walk(baseDir, func(path string, info os.FileInfo, err error) error { |
| 38 | // Error during traversal |
| 39 | if err != nil { |
| 40 | return err |
| 41 | } |
| 42 | |
| 43 | if info.IsDir() { |
| 44 | return nil |
| 45 | } |
| 46 | |
| 47 | // Skip non suffix files |
| 48 | base := info.Name() |
| 49 | for _, s := range suffixes { |
| 50 | if strings.HasSuffix(base, s) { |
| 51 | files = append(files, path) |
| 52 | } |
| 53 | } |
| 54 | return nil |
| 55 | }) |
| 56 | |
| 57 | if err != nil { |
| 58 | return nil, fmt.Errorf("error traversing directory tree: %w", err) |
| 59 | } |
| 60 | return files, nil |
| 61 | } |
| 62 | |
| 63 | var spewPrinter = spew.ConfigState{ |
| 64 | Indent: " ", |