From 25469ebab3f65a54252a2db9ebac6ec27e773e62 Mon Sep 17 00:00:00 2001 From: Robert Kaussow Date: Sun, 5 May 2024 14:56:07 +0200 Subject: [PATCH] feat: add new helper function IsDir (#72) --- file/dir.go | 31 +++++++++++++++++++++++++++++++ file/file.go | 11 ----------- 2 files changed, 31 insertions(+), 11 deletions(-) create mode 100644 file/dir.go diff --git a/file/dir.go b/file/dir.go new file mode 100644 index 0000000..d1e154d --- /dev/null +++ b/file/dir.go @@ -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 +} diff --git a/file/file.go b/file/file.go index ae0015a..7a50c03 100644 --- a/file/file.go +++ b/file/file.go @@ -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