Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 24 additions & 18 deletions fileutil/fileutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
}
7 changes: 3 additions & 4 deletions fileutil/fileutil_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package fileutil

import (
"os"
"path/filepath"
"testing"

Expand All @@ -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))
}
Expand All @@ -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))
}
Expand All @@ -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))
}
Expand Down
13 changes: 13 additions & 0 deletions pathutil/pathutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
Expand All @@ -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")
Expand Down