@@ -231,16 +231,22 @@ func ListFiles(dirPath string) ([]string, error) {
231
231
return files , nil
232
232
}
233
233
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
+
234
240
// ReadFilesFromDir reads all files in a directory and returns a map of file names to their contents.
235
241
// paths can be a relative or absolute.
236
242
func ReadFilesFromDir (dirPath string ) (map [string ]string , error ) {
237
- if ! FileExists (dirPath ) {
243
+ if ! DirExists (dirPath ) {
238
244
return nil , fmt .Errorf ("directory %q does not exist" , dirPath )
239
245
}
240
246
241
247
dir , err := os .ReadDir (dirPath )
242
248
if err != nil {
243
- return nil , err
249
+ return nil , fmt . Errorf ( "failed to read directory %q: %w" , dirPath , err )
244
250
}
245
251
246
252
files := make (map [string ]string )
@@ -252,7 +258,7 @@ func ReadFilesFromDir(dirPath string) (map[string]string, error) {
252
258
filePath := filepath .Join (dirPath , entry .Name ())
253
259
fileData , err := os .ReadFile (filePath )
254
260
if err != nil {
255
- return nil , err
261
+ return nil , fmt . Errorf ( "failed to read file %q: %w" , filePath , err )
256
262
}
257
263
258
264
files [entry .Name ()] = string (fileData )
0 commit comments