mirror of
https://github.com/thegeeklab/wp-plugin-go.git
synced 2024-11-24 23:20:41 +00:00
feat: add new helper function IsDirEmpty (#75)
This commit is contained in:
parent
3cb24ef473
commit
211b38e908
23
file/dir.go
23
file/dir.go
@ -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
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user