Skip to content

Commit

Permalink
feat: add new helper function IsDir (#72)
Browse files Browse the repository at this point in the history
xoxys authored May 5, 2024
1 parent 7143086 commit 25469eb
Showing 2 changed files with 31 additions and 11 deletions.
31 changes: 31 additions & 0 deletions file/dir.go
Original file line number Diff line number Diff line change
@@ -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 changes: 0 additions & 11 deletions file/file.go
Original file line number Diff line number Diff line change
@@ -34,17 +34,6 @@ func ReadStringOrFile(input string) (string, bool, error) {
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
// 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

0 comments on commit 25469eb

Please sign in to comment.