mirror of
https://github.com/thegeeklab/wp-plugin-go.git
synced 2024-11-22 00:20:38 +00:00
feat: add new helper function IsDir (#72)
This commit is contained in:
parent
7143086616
commit
25469ebab3
31
file/dir.go
Normal file
31
file/dir.go
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
package file
|
||||||
|
|
||||||
|
import (
|
||||||
|
"os"
|
||||||
|
)
|
||||||
|
|
||||||
|
// DeleteDir deletes the directory at the given path.
|
||||||
|
// It returns nil if the deletion succeeds, or the deletion error otherwise.
|
||||||
|
// If the directory does not exist, DeleteDir returns nil.
|
||||||
|
func DeleteDir(path string) error {
|
||||||
|
if _, err := os.Stat(path); os.IsNotExist(err) {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
return os.Remove(path)
|
||||||
|
}
|
||||||
|
|
||||||
|
// IsDir returns whether the given path is a directory. If the path does not exist, it returns (false, nil).
|
||||||
|
// If there is an error checking the path, it returns (false, err).
|
||||||
|
func IsDir(path string) (bool, error) {
|
||||||
|
_, err := os.Stat(path)
|
||||||
|
if err == nil {
|
||||||
|
return true, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
if os.IsNotExist(err) {
|
||||||
|
return false, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
return false, err
|
||||||
|
}
|
11
file/file.go
11
file/file.go
@ -34,17 +34,6 @@ func ReadStringOrFile(input string) (string, bool, error) {
|
|||||||
return string(result), true, nil
|
return string(result), true, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// DeleteDir deletes the directory at the given path.
|
|
||||||
// It returns nil if the deletion succeeds, or the deletion error otherwise.
|
|
||||||
// If the directory does not exist, DeleteDir returns nil.
|
|
||||||
func DeleteDir(path string) error {
|
|
||||||
if _, err := os.Stat(path); os.IsNotExist(err) {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
return os.Remove(path)
|
|
||||||
}
|
|
||||||
|
|
||||||
// ExpandFileList takes a list of file globs and expands them into a list
|
// ExpandFileList takes a list of file globs and expands them into a list
|
||||||
// of matching file paths. It returns the expanded file list and any errors
|
// of matching file paths. It returns the expanded file list and any errors
|
||||||
// from glob matching. This allows safely passing user input globs through to
|
// from glob matching. This allows safely passing user input globs through to
|
||||||
|
Loading…
Reference in New Issue
Block a user