0
0
mirror of https://github.com/thegeeklab/wp-plugin-go.git synced 2024-11-21 14:10:39 +00:00

feat: add new helper function IsDirEmpty (#75)

This commit is contained in:
Robert Kaussow 2024-05-05 16:33:21 +02:00 committed by GitHub
parent 3cb24ef473
commit 211b38e908
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -1,6 +1,8 @@
package file
import (
"errors"
"io"
"os"
)
@ -29,3 +31,24 @@ func IsDir(path string) (bool, error) {
return false, err
}
// IsDirEmpty returns whether the given directory path is empty.
// If the path does not exist or is not a directory, it returns (false, err).
func IsDirEmpty(path string) (bool, error) {
f, err := os.Open(path)
if err != nil {
return false, err
}
defer f.Close()
_, err = f.Readdir(1)
if err == nil {
return true, nil
}
if errors.Is(err, io.EOF) {
return true, nil
}
return false, err
}