filesContent returns a map with all direct files within the specified dir and their content. If directory with dirPath is missing or no files matching the pattern were found, it returns an empty map and no error. If pattern is empty string it matches all root files.
(dirPath string, pattern string)
| 535 | // |
| 536 | // If pattern is empty string it matches all root files. |
| 537 | func filesContent(dirPath string, pattern string) (map[string][]byte, error) { |
| 538 | files, err := os.ReadDir(dirPath) |
| 539 | if err != nil { |
| 540 | if errors.Is(err, fs.ErrNotExist) { |
| 541 | return map[string][]byte{}, nil |
| 542 | } |
| 543 | return nil, err |
| 544 | } |
| 545 | |
| 546 | var exp *regexp.Regexp |
| 547 | if pattern != "" { |
| 548 | var err error |
| 549 | if exp, err = regexp.Compile(pattern); err != nil { |
| 550 | return nil, err |
| 551 | } |
| 552 | } |
| 553 | |
| 554 | result := map[string][]byte{} |
| 555 | |
| 556 | for _, f := range files { |
| 557 | if f.IsDir() || (exp != nil && !exp.MatchString(f.Name())) { |
| 558 | continue |
| 559 | } |
| 560 | |
| 561 | raw, err := os.ReadFile(filepath.Join(dirPath, f.Name())) |
| 562 | if err != nil { |
| 563 | return nil, err |
| 564 | } |
| 565 | |
| 566 | result[f.Name()] = raw |
| 567 | } |
| 568 | |
| 569 | return result, nil |
| 570 | } |
no test coverage detected
searching dependent graphs…