diff --git a/fileutil/fileutil.go b/fileutil/fileutil.go index 4b2bfa5..5b7d287 100644 --- a/fileutil/fileutil.go +++ b/fileutil/fileutil.go @@ -12,6 +12,7 @@ import ( type FileManager interface { Open(path string) (*os.File, error) OpenReaderIfExists(path string) (io.Reader, error) + ReadFile(name string) ([]byte, error) ReadDirEntryNames(path string) ([]string, error) Remove(path string) error RemoveAll(path string) error @@ -28,19 +29,6 @@ func NewFileManager() FileManager { return fileManager{} } -// ReadDirEntryNames reads the named directory using os.ReadDir and returns the dir entries' names. -func (fileManager) ReadDirEntryNames(path string) ([]string, error) { - entries, err := os.ReadDir(path) - if err != nil { - return nil, err - } - var names []string - for _, entry := range entries { - names = append(names, entry.Name()) - } - return names, nil -} - // Open ... func (fileManager) Open(path string) (*os.File, error) { return os.Open(path) @@ -60,6 +48,24 @@ func (fileManager) OpenReaderIfExists(path string) (io.Reader, error) { return file, nil } +// ReadFile ... +func (fileManager) ReadFile(name string) ([]byte, error) { + return os.ReadFile(name) +} + +// ReadDirEntryNames reads the named directory using os.ReadDir and returns the dir entries' names. +func (fileManager) ReadDirEntryNames(path string) ([]string, error) { + entries, err := os.ReadDir(path) + if err != nil { + return nil, err + } + var names []string + for _, entry := range entries { + names = append(names, entry.Name()) + } + return names, nil +} + // Remove ... func (fileManager) Remove(path string) error { return os.Remove(path) @@ -81,11 +87,6 @@ func (f fileManager) Write(path string, value string, mode os.FileMode) error { return os.Chmod(path, mode) } -func (fileManager) ensureSavePath(savePath string) error { - dirPath := filepath.Dir(savePath) - return os.MkdirAll(dirPath, 0700) -} - // WriteBytes ... func (f fileManager) WriteBytes(path string, value []byte) error { return os.WriteFile(path, value, 0600) @@ -103,3 +104,8 @@ func (fileManager) FileSizeInBytes(pth string) (int64, error) { return fileInf.Size(), nil } + +func (fileManager) ensureSavePath(savePath string) error { + dirPath := filepath.Dir(savePath) + return os.MkdirAll(dirPath, 0700) +} diff --git a/fileutil/fileutil_test.go b/fileutil/fileutil_test.go index 41fdc25..b460198 100644 --- a/fileutil/fileutil_test.go +++ b/fileutil/fileutil_test.go @@ -1,7 +1,6 @@ package fileutil import ( - "os" "path/filepath" "testing" @@ -21,7 +20,7 @@ func TestWrite(t *testing.T) { tmpFilePath := filepath.Join(tmpDirPath, "WriteStringToFile-success.txt") require.NoError(t, manager.Write(tmpFilePath, content, 0600)) - fileContent, err := os.ReadFile(tmpFilePath) + fileContent, err := manager.ReadFile(tmpFilePath) require.NoError(t, err) require.Equal(t, content, string(fileContent)) } @@ -31,7 +30,7 @@ func TestWrite(t *testing.T) { tmpFilePath := filepath.Join(tmpDirPath, "dir-does-not-exist", "WriteStringToFile-success.txt") require.NoError(t, manager.Write(tmpFilePath, content, 0600)) - fileContent, err := os.ReadFile(tmpFilePath) + fileContent, err := manager.ReadFile(tmpFilePath) require.NoError(t, err) require.Equal(t, content, string(fileContent)) } @@ -41,7 +40,7 @@ func TestWrite(t *testing.T) { tmpFilePath := filepath.Join(tmpDirPath, "WriteBytesToFile-success.txt") require.NoError(t, manager.WriteBytes(tmpFilePath, []byte("test string"))) - fileContent, err := os.ReadFile(tmpFilePath) + fileContent, err := manager.ReadFile(tmpFilePath) require.NoError(t, err) require.Equal(t, "test string", string(fileContent)) } diff --git a/pathutil/pathutil.go b/pathutil/pathutil.go index f173693..aff9bed 100644 --- a/pathutil/pathutil.go +++ b/pathutil/pathutil.go @@ -75,6 +75,7 @@ func (pathChecker) genericIsPathExists(pth string) (os.FileInfo, bool, error) { // PathModifier ... type PathModifier interface { AbsPath(pth string) (string, error) + EscapeGlobPath(path string) string } type pathModifier struct{} @@ -98,6 +99,18 @@ func (p pathModifier) AbsPath(pth string) (string, error) { return filepath.Abs(os.ExpandEnv(pth)) } +// EscapeGlobPath escapes a partial path, determined at runtime, used as a parameter for filepath.Glob +func (pathModifier) EscapeGlobPath(path string) string { + var escaped string + for _, ch := range path { + if ch == '[' || ch == ']' || ch == '-' || ch == '*' || ch == '?' || ch == '\\' { + escaped += "\\" + } + escaped += string(ch) + } + return escaped +} + func (pathModifier) expandTilde(pth string) (string, error) { if pth == "" { return "", errors.New("No Path provided")