Skip to content

Commit df362b2

Browse files
committed
fix: ReadFilesFrom Dir now checks for directory not files
1 parent 507620a commit df362b2

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

fileutil/fileutil.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -231,16 +231,22 @@ func ListFiles(dirPath string) ([]string, error) {
231231
return files, nil
232232
}
233233

234+
// DirExists checks whether a directory exists.
235+
func DirExists(dirPath string) bool {
236+
info, err := os.Stat(dirPath)
237+
return err == nil && info.IsDir()
238+
}
239+
234240
// ReadFilesFromDir reads all files in a directory and returns a map of file names to their contents.
235241
// paths can be a relative or absolute.
236242
func ReadFilesFromDir(dirPath string) (map[string]string, error) {
237-
if !FileExists(dirPath) {
243+
if !DirExists(dirPath) {
238244
return nil, fmt.Errorf("directory %q does not exist", dirPath)
239245
}
240246

241247
dir, err := os.ReadDir(dirPath)
242248
if err != nil {
243-
return nil, err
249+
return nil, fmt.Errorf("failed to read directory %q: %w", dirPath, err)
244250
}
245251

246252
files := make(map[string]string)
@@ -252,7 +258,7 @@ func ReadFilesFromDir(dirPath string) (map[string]string, error) {
252258
filePath := filepath.Join(dirPath, entry.Name())
253259
fileData, err := os.ReadFile(filePath)
254260
if err != nil {
255-
return nil, err
261+
return nil, fmt.Errorf("failed to read file %q: %w", filePath, err)
256262
}
257263

258264
files[entry.Name()] = string(fileData)

0 commit comments

Comments
 (0)